1
|
1 // utils.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
1
|
26 #endif |
|
27 |
1343
|
28 #include <climits> |
1345
|
29 #include <csetjmp> |
1346
|
30 #include <cstring> |
1343
|
31 |
1728
|
32 #include <string> |
|
33 |
1349
|
34 #include <fstream.h> |
|
35 #include <iostream.h> |
|
36 #include <strstream.h> |
|
37 |
1350
|
38 #ifdef HAVE_UNISTD_H |
1
|
39 #include <sys/types.h> |
|
40 #include <unistd.h> |
|
41 #endif |
367
|
42 |
138
|
43 #if defined (HAVE_TERMIOS_H) |
|
44 #include <termios.h> |
|
45 #elif defined (HAVE_TERMIO_H) |
1
|
46 #include <termio.h> |
138
|
47 #elif defined (HAVE_SGTTY_H) |
1
|
48 #include <sgtty.h> |
|
49 #else |
|
50 LOSE! LOSE! |
|
51 #endif |
|
52 |
1465
|
53 #ifndef HAVE_STRNCASECMP |
|
54 extern "C" int strncasecmp (const char*, const char*, size_t); |
|
55 #endif |
1
|
56 |
|
57 #include "SLStack.h" |
|
58 |
1651
|
59 #include "oct-cmplx.h" |
1755
|
60 #include "str-vec.h" |
1651
|
61 |
1352
|
62 #include "defun.h" |
1781
|
63 #include "dir-ops.h" |
1352
|
64 #include "dirfns.h" |
|
65 #include "error.h" |
|
66 #include "gripes.h" |
|
67 #include "help.h" |
|
68 #include "input.h" |
|
69 #include "mappers.h" |
1742
|
70 #include "oct-hist.h" |
1750
|
71 #include "oct-obj.h" |
1352
|
72 #include "pager.h" |
1155
|
73 #include "pathsearch.h" |
1690
|
74 #include "sysdep.h" |
1750
|
75 #include "toplev.h" |
1
|
76 #include "unwind-prot.h" |
1352
|
77 #include "user-prefs.h" |
|
78 #include "utils.h" |
|
79 #include "variables.h" |
1
|
80 |
|
81 // Top level context (?) |
|
82 extern jmp_buf toplevel; |
|
83 |
581
|
84 // Save a string. |
|
85 |
1
|
86 char * |
|
87 strsave (const char *s) |
|
88 { |
526
|
89 if (! s) |
|
90 return 0; |
1
|
91 |
|
92 int len = strlen (s); |
|
93 char *tmp = new char [len+1]; |
|
94 tmp = strcpy (tmp, s); |
|
95 return tmp; |
|
96 } |
|
97 |
1755
|
98 #if 0 |
|
99 |
581
|
100 // Concatenate two strings. |
|
101 |
1
|
102 char * |
|
103 strconcat (const char *s, const char *t) |
|
104 { |
|
105 int len = strlen (s) + strlen (t); |
|
106 char *tmp = new char [len+1]; |
|
107 strcpy (tmp, s); |
1266
|
108 strcat (tmp, t); |
|
109 return tmp; |
1
|
110 } |
1755
|
111 #endif |
1
|
112 |
581
|
113 // Throw away input until a given character is read. |
|
114 |
1
|
115 void |
|
116 discard_until (istream& stream, char character) |
|
117 { |
|
118 int c; |
|
119 for (;;) |
|
120 { |
|
121 stream >> c; |
|
122 if (c == EOF || c == character) |
|
123 break; |
|
124 } |
|
125 if (c != EOF) |
|
126 stream.putback ((char) c); |
|
127 } |
|
128 |
607
|
129 #if 0 |
|
130 |
|
131 // XXX UNTESTED XXX |
|
132 |
|
133 // Read input until a given character is read. Returns characters |
|
134 // read in a new string that must be freed by the caller. |
|
135 |
|
136 char * |
|
137 read_until (istream& stream, char character) |
|
138 { |
|
139 int grow_size = 8; |
|
140 int limit = grow_size; |
|
141 char *buf = new char [limit]; |
|
142 char *bp = buf; |
|
143 |
|
144 get_more: |
|
145 is.getline (bp, limit, character); |
|
146 |
|
147 if (is.gcount () == 0) |
|
148 { |
|
149 delete [] buf; |
|
150 return 0; |
|
151 } |
|
152 |
|
153 if (is.gcount () == limit && buf[limit-1] != '\0') |
|
154 { |
|
155 char *tmp = new char [limit + grow_size]; |
|
156 strcpy (tmp, buf); |
|
157 delete [] buf; |
|
158 buf = tmp; |
|
159 bp = tmp + limit - 1; |
|
160 limit += grow_size; |
|
161 grow_size *= 2; |
|
162 goto get_more; |
|
163 } |
|
164 |
|
165 return buf; |
|
166 } |
|
167 #endif |
|
168 |
661
|
169 // Get a temporary file name. |
643
|
170 |
1755
|
171 string |
643
|
172 octave_tmp_file_name (void) |
|
173 { |
1755
|
174 string retval; |
|
175 |
|
176 char *tmp = tempnam (0, "oct-"); |
662
|
177 |
1755
|
178 if (tmp) |
|
179 { |
|
180 retval = tmp; |
662
|
181 |
1755
|
182 free (tmp); |
|
183 } |
|
184 else |
661
|
185 error ("can't open temporary file!"); |
662
|
186 |
661
|
187 return retval; |
643
|
188 } |
|
189 |
801
|
190 DEFUN ("octave_tmp_file_name", Foctave_tmp_file_name, |
1488
|
191 Soctave_tmp_file_name, 10, |
801
|
192 "octave_tmp_file_name ()") |
|
193 { |
|
194 tree_constant retval; |
|
195 |
|
196 if (args.length () == 0) |
|
197 retval = octave_tmp_file_name (); |
|
198 else |
|
199 print_usage ("octave_tmp_file_name"); |
|
200 |
|
201 return retval; |
|
202 } |
|
203 |
581
|
204 // Return to the main command loop in octave.cc. |
|
205 |
1618
|
206 extern "C" void |
1
|
207 jump_to_top_level (void) |
|
208 { |
|
209 run_all_unwind_protects (); |
|
210 |
|
211 longjmp (toplevel, 1); |
|
212 } |
|
213 |
|
214 int |
1755
|
215 almost_match (const string& std, const string& s, int min_match_len, |
526
|
216 int case_sens) |
1
|
217 { |
1755
|
218 int stdlen = std.length (); |
|
219 int slen = s.length (); |
1
|
220 |
|
221 return (slen <= stdlen |
|
222 && slen >= min_match_len |
287
|
223 && (case_sens |
1755
|
224 ? (strncmp (std.c_str (), s.c_str (), slen) == 0) |
|
225 : (strncasecmp (std.c_str (), s.c_str (), slen) == 0))); |
287
|
226 } |
|
227 |
581
|
228 // Ugh. |
|
229 |
287
|
230 int |
1755
|
231 keyword_almost_match (const char **std, int *min_len, const string& s, |
287
|
232 int min_toks_to_match, int max_toks) |
|
233 { |
|
234 int status = 0; |
|
235 int tok_count = 0; |
|
236 int toks_matched = 0; |
|
237 |
1755
|
238 if (s.empty () || max_toks < 1) |
287
|
239 return status; |
|
240 |
1755
|
241 char *kw = strsave (s.c_str ()); |
287
|
242 |
|
243 char *t = kw; |
|
244 while (*t != '\0') |
|
245 { |
|
246 if (*t == '\t') |
|
247 *t = ' '; |
|
248 t++; |
|
249 } |
|
250 |
|
251 char *beg = kw; |
|
252 while (*beg == ' ') |
|
253 beg++; |
|
254 |
|
255 if (*beg == '\0') |
|
256 return status; |
|
257 |
|
258 |
|
259 char **to_match = new char * [max_toks + 1]; |
526
|
260 const char **s1 = std; |
287
|
261 char **s2 = to_match; |
|
262 |
526
|
263 if (! s1 || ! s2) |
287
|
264 goto done; |
|
265 |
|
266 s2[tok_count] = beg; |
|
267 char *end; |
526
|
268 while ((end = strchr (beg, ' ')) != 0) |
287
|
269 { |
|
270 *end = '\0'; |
|
271 beg = end + 1; |
|
272 |
|
273 while (*beg == ' ') |
|
274 beg++; |
|
275 |
|
276 if (*beg == '\0') |
|
277 break; |
|
278 |
|
279 tok_count++; |
|
280 if (tok_count >= max_toks) |
|
281 goto done; |
|
282 |
|
283 s2[tok_count] = beg; |
|
284 } |
526
|
285 s2[tok_count+1] = 0; |
287
|
286 |
|
287 s2 = to_match; |
|
288 |
|
289 for (;;) |
|
290 { |
|
291 if (! almost_match (*s1, *s2, min_len[toks_matched], 0)) |
|
292 goto done; |
|
293 |
|
294 toks_matched++; |
|
295 |
|
296 s1++; |
|
297 s2++; |
|
298 |
|
299 if (! *s2) |
|
300 { |
|
301 status = (toks_matched >= min_toks_to_match); |
|
302 goto done; |
|
303 } |
|
304 |
|
305 if (! *s1) |
|
306 goto done; |
|
307 } |
|
308 |
|
309 done: |
|
310 |
|
311 delete [] kw; |
|
312 delete [] to_match; |
|
313 |
|
314 return status; |
1
|
315 } |
|
316 |
1755
|
317 string_vector |
1781
|
318 get_fcn_file_names (const string& name, int no_suffix) |
1
|
319 { |
1781
|
320 string_vector retval; |
|
321 |
|
322 dir_entry dir (name); |
|
323 |
|
324 if (dir) |
|
325 { |
|
326 string_vector tmp = dir.read (); |
1
|
327 |
1781
|
328 int max_len = tmp.length (); |
|
329 |
|
330 retval.resize (max_len); |
|
331 |
|
332 int k = 0; |
|
333 int i; |
|
334 for (i = 0; i < max_len; i++) |
1
|
335 { |
1781
|
336 string entry = tmp[i]; |
|
337 |
|
338 int len = entry.length (); |
|
339 |
1664
|
340 #if defined (WITH_DYNAMIC_LINKING) |
826
|
341 if ((len > 2 |
1781
|
342 && entry[len-2] == '.' && entry[len-1] == 'm') |
826
|
343 || (len > 4 |
1781
|
344 && entry[len-4] == '.' && entry[len-3] == 'o' |
|
345 && entry[len-2] == 'c' && entry[len-1] == 't')) |
826
|
346 #else |
1
|
347 if (len > 2 |
1781
|
348 && entry[len-2] == '.' && entry[len-1] == 'm') |
826
|
349 #endif |
1
|
350 { |
|
351 if (no_suffix) |
826
|
352 { |
1781
|
353 if (entry[len-1] == 'm') |
|
354 entry.resize (len-2); |
826
|
355 else |
1781
|
356 entry.resize (len-4); |
826
|
357 } |
1
|
358 |
1781
|
359 retval[k++] = entry; |
1
|
360 } |
|
361 } |
1781
|
362 |
|
363 retval.resize (i); |
1
|
364 } |
|
365 |
|
366 return retval; |
|
367 } |
|
368 |
1755
|
369 string_vector |
338
|
370 get_fcn_file_names (int& num, int no_suffix) |
1
|
371 { |
|
372 static int num_max = 1024; |
1787
|
373 |
1755
|
374 string_vector retval (num_max); |
1
|
375 |
1787
|
376 dir_path p (user_pref.loadpath); |
1
|
377 |
1787
|
378 string_vector dirs = p.all_directories (); |
679
|
379 |
1787
|
380 int len = dirs.length (); |
|
381 |
|
382 int k = 0; |
1
|
383 |
1787
|
384 for (int i = 0; i < len; i++) |
|
385 { |
|
386 string_vector names = get_fcn_file_names (dirs[i], no_suffix); |
679
|
387 |
1787
|
388 int tmp_num = names.length (); |
1
|
389 |
1787
|
390 if (k + tmp_num > num_max) |
|
391 { |
|
392 num_max += tmp_num; |
|
393 retval.resize (num_max); |
679
|
394 } |
1
|
395 |
1787
|
396 for (int j = 0; j < tmp_num; j++) |
|
397 retval[k++] = names[j++]; |
1
|
398 } |
|
399 |
1787
|
400 retval.resize (k); |
1
|
401 |
|
402 return retval; |
|
403 } |
|
404 |
1086
|
405 // Convert X to the nearest integer value. Should not pass NaN to |
|
406 // this function. |
|
407 |
1
|
408 int |
|
409 NINT (double x) |
|
410 { |
|
411 if (x > INT_MAX) |
|
412 return INT_MAX; |
|
413 else if (x < INT_MIN) |
|
414 return INT_MIN; |
|
415 else |
|
416 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
417 } |
|
418 |
|
419 double |
|
420 D_NINT (double x) |
|
421 { |
|
422 if (xisinf (x) || xisnan (x)) |
|
423 return x; |
|
424 else |
|
425 return floor (x + 0.5); |
|
426 } |
|
427 |
526
|
428 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
429 |
|
430 static int |
|
431 all_strings (const Octave_object& args) |
|
432 { |
|
433 int n = args.length (); |
712
|
434 for (int i = 0; i < n; i++) |
610
|
435 if (! args(i).is_string ()) |
526
|
436 return 0; |
|
437 return 1; |
|
438 } |
|
439 |
1755
|
440 string_vector |
|
441 make_argv (const Octave_object& args, const string& fcn_name) |
526
|
442 { |
1755
|
443 string_vector argv; |
|
444 |
526
|
445 if (all_strings (args)) |
|
446 { |
|
447 int n = args.length (); |
1755
|
448 argv.resize (n+1); |
|
449 argv[0] = fcn_name; |
|
450 |
712
|
451 for (int i = 0; i < n; i++) |
1755
|
452 argv[i+1] = args(i).string_value (); |
526
|
453 } |
|
454 else |
1755
|
455 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); |
526
|
456 |
|
457 return argv; |
|
458 } |
|
459 |
719
|
460 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
461 // should be considered fatal; return 1 if this is ok. |
|
462 |
628
|
463 int |
|
464 empty_arg (const char *name, int nr, int nc) |
|
465 { |
|
466 int is_empty = 0; |
|
467 |
|
468 if (nr == 0 || nc == 0) |
|
469 { |
|
470 int flag = user_pref.propagate_empty_matrices; |
|
471 |
|
472 if (flag < 0) |
636
|
473 { |
|
474 gripe_empty_arg (name, 0); |
|
475 is_empty = 1; |
|
476 } |
673
|
477 else if (flag == 0) |
636
|
478 { |
|
479 gripe_empty_arg (name, 1); |
|
480 is_empty = -1; |
|
481 } |
719
|
482 else |
|
483 is_empty = 1; |
628
|
484 } |
|
485 |
|
486 return is_empty; |
|
487 } |
|
488 |
1755
|
489 // Format a list in neat columns. Mostly stolen from GNU ls. |
581
|
490 |
526
|
491 ostrstream& |
1755
|
492 list_in_columns (ostrstream& os, const string_vector& list) |
526
|
493 { |
1358
|
494 // Compute the maximum name length. |
526
|
495 |
|
496 int max_name_length = 0; |
1755
|
497 int total_names = list.length (); |
|
498 |
|
499 for (int i = 0; i < total_names; i++) |
526
|
500 { |
1755
|
501 int name_length = list[i].length (); |
526
|
502 if (name_length > max_name_length) |
|
503 max_name_length = name_length; |
|
504 } |
|
505 |
1358
|
506 // Allow at least two spaces between names. |
526
|
507 |
|
508 max_name_length += 2; |
|
509 |
1358
|
510 // Calculate the maximum number of columns that will fit. |
526
|
511 |
|
512 int line_length = terminal_columns (); |
|
513 int cols = line_length / max_name_length; |
|
514 if (cols == 0) |
|
515 cols = 1; |
|
516 |
1358
|
517 // Calculate the number of rows that will be in each column except |
|
518 // possibly for a short column on the right. |
526
|
519 |
|
520 int rows = total_names / cols + (total_names % cols != 0); |
|
521 |
1358
|
522 // Recalculate columns based on rows. |
526
|
523 |
|
524 cols = total_names / rows + (total_names % rows != 0); |
|
525 |
|
526 int count; |
|
527 for (int row = 0; row < rows; row++) |
|
528 { |
|
529 count = row; |
|
530 int pos = 0; |
|
531 |
1358
|
532 // Print the next row. |
526
|
533 |
|
534 while (1) |
|
535 { |
1755
|
536 string nm = list[count]; |
|
537 |
|
538 os << nm; |
|
539 int name_length = nm.length (); |
526
|
540 |
|
541 count += rows; |
|
542 if (count >= total_names) |
|
543 break; |
|
544 |
|
545 int spaces_to_pad = max_name_length - name_length; |
|
546 for (int i = 0; i < spaces_to_pad; i++) |
|
547 os << " "; |
|
548 pos += max_name_length; |
|
549 } |
|
550 os << "\n"; |
|
551 } |
|
552 |
|
553 return os; |
|
554 } |
|
555 |
581
|
556 // See if the given file is in the path. |
|
557 |
1755
|
558 string |
|
559 search_path_for_file (const string& path, const string& name) |
686
|
560 { |
1787
|
561 dir_path p (path); |
686
|
562 |
1787
|
563 return make_absolute (p.find (name), the_current_working_directory); |
686
|
564 } |
|
565 |
1488
|
566 DEFUN ("file_in_path", Ffile_in_path, Sfile_in_path, 10, |
686
|
567 "file_in_path (PATH, NAME)") |
|
568 { |
|
569 Octave_object retval; |
|
570 |
1755
|
571 int argc = args.length () + 1; |
|
572 |
|
573 string_vector argv = make_argv (args, "file_in_path"); |
|
574 |
|
575 if (error_state) |
|
576 return retval; |
686
|
577 |
|
578 if (argc == 3) |
|
579 { |
1755
|
580 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
581 |
1755
|
582 if (fname.empty ()) |
|
583 retval = Matrix (); |
|
584 else |
686
|
585 retval = fname; |
|
586 } |
|
587 else |
|
588 print_usage ("file_in_path"); |
|
589 |
|
590 return retval; |
|
591 } |
|
592 |
1755
|
593 string |
|
594 file_in_path (const string& name, const string& suffix) |
526
|
595 { |
1755
|
596 string nm = name; |
526
|
597 |
1755
|
598 if (! suffix.empty ()) |
|
599 nm.append (suffix); |
686
|
600 |
1755
|
601 if (the_current_working_directory.empty ()) |
526
|
602 get_working_directory ("file_in_path"); |
|
603 |
1755
|
604 return search_path_for_file (user_pref.loadpath, nm); |
526
|
605 } |
|
606 |
581
|
607 // See if there is an function file in the path. If so, return the |
|
608 // full path to the file. |
|
609 |
1755
|
610 string |
|
611 fcn_file_in_path (const string& name) |
526
|
612 { |
1755
|
613 string retval; |
908
|
614 |
1755
|
615 int len = name.length (); |
|
616 |
|
617 if (len > 0) |
|
618 { |
|
619 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
620 retval = file_in_path (name, ""); |
908
|
621 else |
1755
|
622 retval = file_in_path (name, ".m"); |
908
|
623 } |
1755
|
624 |
|
625 return retval; |
526
|
626 } |
|
627 |
581
|
628 // See if there is an octave file in the path. If so, return the |
|
629 // full path to the file. |
|
630 |
1755
|
631 string |
|
632 oct_file_in_path (const string& name) |
572
|
633 { |
1755
|
634 string retval; |
908
|
635 |
1755
|
636 int len = name.length (); |
|
637 |
|
638 if (len > 0) |
|
639 { |
|
640 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
641 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
642 retval = file_in_path (name, ""); |
908
|
643 else |
1755
|
644 retval = file_in_path (name, ".oct"); |
908
|
645 } |
1755
|
646 |
|
647 return retval; |
572
|
648 } |
|
649 |
1755
|
650 const char * |
801
|
651 undo_string_escape (char c) |
|
652 { |
|
653 if (! c) |
1755
|
654 return ""; |
801
|
655 |
|
656 switch (c) |
|
657 { |
|
658 case '\a': |
|
659 return "\\a"; |
|
660 |
|
661 case '\b': // backspace |
|
662 return "\\b"; |
|
663 |
|
664 case '\f': // formfeed |
|
665 return "\\f"; |
|
666 |
|
667 case '\n': // newline |
|
668 return "\\n"; |
|
669 |
|
670 case '\r': // carriage return |
|
671 return "\\r"; |
|
672 |
|
673 case '\t': // horizontal tab |
|
674 return "\\t"; |
|
675 |
|
676 case '\v': // vertical tab |
|
677 return "\\v"; |
|
678 |
|
679 case '\\': // backslash |
|
680 return "\\\\"; |
|
681 |
|
682 case '"': // double quote |
|
683 return "\\\""; |
|
684 |
|
685 default: |
1755
|
686 { |
|
687 static char retval[2]; |
|
688 retval[0] = c; |
|
689 retval[1] = '\0'; |
|
690 return retval; |
|
691 } |
801
|
692 } |
|
693 } |
|
694 |
1755
|
695 string |
|
696 undo_string_escapes (const string& s) |
801
|
697 { |
1755
|
698 string retval; |
801
|
699 |
1755
|
700 for (size_t i = 0; i < s.length (); i++) |
|
701 retval.append (undo_string_escape (s[i])); |
801
|
702 |
1755
|
703 return retval; |
801
|
704 } |
|
705 |
|
706 DEFUN ("undo_string_escapes", Fundo_string_escapes, |
1488
|
707 Sundo_string_escapes, 10, |
801
|
708 "undo_string_escapes (STRING)") |
|
709 { |
|
710 tree_constant retval; |
|
711 |
|
712 int nargin = args.length (); |
|
713 |
|
714 if (nargin == 1 && args(0).is_string ()) |
1755
|
715 retval = undo_string_escapes (args(0).string_value ()); |
801
|
716 else |
1023
|
717 print_usage ("undo_string_escapes"); |
801
|
718 |
|
719 return retval; |
|
720 } |
|
721 |
1711
|
722 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
723 // library. |
|
724 |
|
725 void |
|
726 oct_putenv (const char *var_name, const char *value) |
|
727 { |
|
728 static const char **saved_env_items = 0; |
|
729 static unsigned saved_len; |
|
730 char *old_item = 0; |
|
731 |
|
732 int new_len = strlen (var_name) + strlen (value) + 2; |
|
733 |
|
734 char *new_item = new char [new_len]; |
|
735 |
|
736 sprintf (new_item, "%s=%s", var_name, value); |
|
737 |
|
738 #ifndef SMART_PUTENV |
|
739 |
|
740 // Check if we have saved anything yet. |
|
741 |
|
742 if (! saved_env_items) |
|
743 { |
|
744 saved_env_items = new const char * [1]; |
|
745 saved_env_items[0] = var_name; |
|
746 saved_len = 1; |
|
747 } |
|
748 else |
|
749 { |
|
750 // Check if we've assigned VAR_NAME before. |
|
751 |
|
752 unsigned len = strlen (var_name); |
|
753 |
|
754 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
755 { |
|
756 if (strcmp (saved_env_items[i], var_name) == 0) |
|
757 { |
|
758 old_item = getenv (var_name); |
|
759 |
|
760 assert (old_item); |
|
761 |
|
762 // Back up to the `NAME=' in the environment before the |
|
763 // value that getenv returns. |
|
764 |
|
765 old_item -= (len + 1); |
|
766 } |
|
767 } |
|
768 |
|
769 if (! old_item) |
|
770 { |
|
771 // If we haven't seen VAR_NAME before, save it. Assume it |
|
772 // is in safe storage. |
|
773 |
|
774 saved_len++; |
|
775 |
|
776 const char **tmp = new const char * [saved_len]; |
|
777 |
|
778 for (unsigned i = 0; i < saved_len - 1; i++) |
|
779 tmp[i] = saved_env_items[i]; |
|
780 |
|
781 tmp[saved_len - 1] = var_name; |
|
782 |
|
783 delete [] saved_env_items; |
|
784 |
|
785 saved_env_items = tmp; |
|
786 } |
|
787 } |
|
788 |
|
789 #endif |
|
790 |
|
791 // As far as I can see there's no way to distinguish between the |
|
792 // various errors; putenv doesn't have errno values. |
|
793 |
|
794 if (putenv (new_item) < 0) |
|
795 error ("putenv (%s) failed", new_item); |
|
796 |
|
797 #ifndef SMART_PUTENV |
|
798 |
|
799 // Can't free `new_item' because its contained value is now in |
|
800 // `environ', but we can free `old_item', since it's been replaced. |
|
801 |
|
802 delete [] old_item; |
|
803 |
|
804 #endif |
|
805 } |
|
806 |
572
|
807 /* |
1
|
808 ;;; Local Variables: *** |
|
809 ;;; mode: C++ *** |
|
810 ;;; page-delimiter: "^/\\*" *** |
|
811 ;;; End: *** |
|
812 */ |