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" |
|
63 #include "dirfns.h" |
|
64 #include "error.h" |
|
65 #include "gripes.h" |
|
66 #include "help.h" |
|
67 #include "input.h" |
|
68 #include "mappers.h" |
1742
|
69 #include "oct-hist.h" |
1750
|
70 #include "oct-obj.h" |
1352
|
71 #include "pager.h" |
1155
|
72 #include "pathsearch.h" |
1690
|
73 #include "sysdep.h" |
1352
|
74 #include "sysdir.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 |
338
|
318 get_fcn_file_names (int& num, const char *dir, int no_suffix) |
1
|
319 { |
|
320 static int num_max = 256; |
1755
|
321 string_vector retval (num_max); |
1
|
322 int i = 0; |
|
323 |
|
324 DIR *dirp = opendir (dir); |
526
|
325 if (dirp) |
1
|
326 { |
|
327 struct dirent *entry; |
526
|
328 while ((entry = readdir (dirp)) != 0) |
1
|
329 { |
|
330 int len = NLENGTH (entry); |
1664
|
331 #if defined (WITH_DYNAMIC_LINKING) |
826
|
332 if ((len > 2 |
|
333 && entry->d_name[len-2] == '.' |
|
334 && entry->d_name[len-1] == 'm') |
|
335 || (len > 4 |
|
336 && entry->d_name[len-4] == '.' |
|
337 && entry->d_name[len-3] == 'o' |
|
338 && entry->d_name[len-2] == 'c' |
|
339 && entry->d_name[len-1] == 't')) |
|
340 #else |
1
|
341 if (len > 2 |
|
342 && entry->d_name[len-2] == '.' |
|
343 && entry->d_name[len-1] == 'm') |
826
|
344 #endif |
1
|
345 { |
1755
|
346 retval[i] = entry->d_name; |
1
|
347 if (no_suffix) |
826
|
348 { |
|
349 if (retval[i][len-1] == 'm') |
|
350 retval[i][len-2] = '\0'; |
|
351 else |
|
352 retval[i][len-4] = '\0'; |
|
353 } |
1
|
354 |
|
355 i++; |
|
356 |
|
357 if (i == num_max - 1) |
|
358 { |
|
359 num_max += 256; |
1755
|
360 retval.resize (num_max); |
1
|
361 } |
|
362 } |
|
363 } |
106
|
364 closedir (dirp); |
1
|
365 } |
|
366 |
|
367 num = i; |
1755
|
368 retval.resize (num); |
1
|
369 |
|
370 return retval; |
|
371 } |
|
372 |
1755
|
373 string_vector |
338
|
374 get_fcn_file_names (int& num, int no_suffix) |
1
|
375 { |
|
376 static int num_max = 1024; |
1755
|
377 string_vector retval (num_max); |
1
|
378 int i = 0; |
|
379 |
1755
|
380 char *path_elt = kpse_path_element (user_pref.loadpath.c_str ()); |
1
|
381 |
679
|
382 while (path_elt) |
1
|
383 { |
679
|
384 str_llist_type *elt_dirs = kpse_element_dirs (path_elt); |
|
385 |
|
386 str_llist_elt_type *dir; |
|
387 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1
|
388 { |
679
|
389 char *elt_dir = STR_LLIST (*dir); |
1
|
390 |
679
|
391 if (elt_dir) |
1
|
392 { |
679
|
393 int tmp_num; |
1755
|
394 string_vector names |
|
395 = get_fcn_file_names (tmp_num, elt_dir, no_suffix); |
679
|
396 |
|
397 if (i + tmp_num >= num_max - 1) |
|
398 { |
|
399 num_max += 1024; |
1755
|
400 retval.resize (num_max); |
679
|
401 } |
1
|
402 |
679
|
403 int k = 0; |
|
404 while (k < tmp_num) |
|
405 retval[i++] = names[k++]; |
|
406 } |
|
407 } |
1
|
408 |
679
|
409 path_elt = kpse_path_element (0); |
1
|
410 } |
|
411 |
|
412 num = i; |
1755
|
413 retval.resize (num); |
1
|
414 |
|
415 return retval; |
|
416 } |
|
417 |
1086
|
418 // Convert X to the nearest integer value. Should not pass NaN to |
|
419 // this function. |
|
420 |
1
|
421 int |
|
422 NINT (double x) |
|
423 { |
|
424 if (x > INT_MAX) |
|
425 return INT_MAX; |
|
426 else if (x < INT_MIN) |
|
427 return INT_MIN; |
|
428 else |
|
429 return (x > 0) ? ((int) (x + 0.5)) : ((int) (x - 0.5)); |
|
430 } |
|
431 |
|
432 double |
|
433 D_NINT (double x) |
|
434 { |
|
435 if (xisinf (x) || xisnan (x)) |
|
436 return x; |
|
437 else |
|
438 return floor (x + 0.5); |
|
439 } |
|
440 |
526
|
441 // XXX FIXME XXX -- put these in some file, and make them extern. |
|
442 |
|
443 static int |
|
444 all_strings (const Octave_object& args) |
|
445 { |
|
446 int n = args.length (); |
712
|
447 for (int i = 0; i < n; i++) |
610
|
448 if (! args(i).is_string ()) |
526
|
449 return 0; |
|
450 return 1; |
|
451 } |
|
452 |
1755
|
453 string_vector |
|
454 make_argv (const Octave_object& args, const string& fcn_name) |
526
|
455 { |
1755
|
456 string_vector argv; |
|
457 |
526
|
458 if (all_strings (args)) |
|
459 { |
|
460 int n = args.length (); |
1755
|
461 argv.resize (n+1); |
|
462 argv[0] = fcn_name; |
|
463 |
712
|
464 for (int i = 0; i < n; i++) |
1755
|
465 argv[i+1] = args(i).string_value (); |
526
|
466 } |
|
467 else |
1755
|
468 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); |
526
|
469 |
|
470 return argv; |
|
471 } |
|
472 |
719
|
473 // Return non-zero if either NR or NC is zero. Return -1 if this |
|
474 // should be considered fatal; return 1 if this is ok. |
|
475 |
628
|
476 int |
|
477 empty_arg (const char *name, int nr, int nc) |
|
478 { |
|
479 int is_empty = 0; |
|
480 |
|
481 if (nr == 0 || nc == 0) |
|
482 { |
|
483 int flag = user_pref.propagate_empty_matrices; |
|
484 |
|
485 if (flag < 0) |
636
|
486 { |
|
487 gripe_empty_arg (name, 0); |
|
488 is_empty = 1; |
|
489 } |
673
|
490 else if (flag == 0) |
636
|
491 { |
|
492 gripe_empty_arg (name, 1); |
|
493 is_empty = -1; |
|
494 } |
719
|
495 else |
|
496 is_empty = 1; |
628
|
497 } |
|
498 |
|
499 return is_empty; |
|
500 } |
|
501 |
1755
|
502 // Format a list in neat columns. Mostly stolen from GNU ls. |
581
|
503 |
526
|
504 ostrstream& |
1755
|
505 list_in_columns (ostrstream& os, const string_vector& list) |
526
|
506 { |
1358
|
507 // Compute the maximum name length. |
526
|
508 |
|
509 int max_name_length = 0; |
1755
|
510 int total_names = list.length (); |
|
511 |
|
512 for (int i = 0; i < total_names; i++) |
526
|
513 { |
1755
|
514 int name_length = list[i].length (); |
526
|
515 if (name_length > max_name_length) |
|
516 max_name_length = name_length; |
|
517 } |
|
518 |
1358
|
519 // Allow at least two spaces between names. |
526
|
520 |
|
521 max_name_length += 2; |
|
522 |
1358
|
523 // Calculate the maximum number of columns that will fit. |
526
|
524 |
|
525 int line_length = terminal_columns (); |
|
526 int cols = line_length / max_name_length; |
|
527 if (cols == 0) |
|
528 cols = 1; |
|
529 |
1358
|
530 // Calculate the number of rows that will be in each column except |
|
531 // possibly for a short column on the right. |
526
|
532 |
|
533 int rows = total_names / cols + (total_names % cols != 0); |
|
534 |
1358
|
535 // Recalculate columns based on rows. |
526
|
536 |
|
537 cols = total_names / rows + (total_names % rows != 0); |
|
538 |
|
539 int count; |
|
540 for (int row = 0; row < rows; row++) |
|
541 { |
|
542 count = row; |
|
543 int pos = 0; |
|
544 |
1358
|
545 // Print the next row. |
526
|
546 |
|
547 while (1) |
|
548 { |
1755
|
549 string nm = list[count]; |
|
550 |
|
551 os << nm; |
|
552 int name_length = nm.length (); |
526
|
553 |
|
554 count += rows; |
|
555 if (count >= total_names) |
|
556 break; |
|
557 |
|
558 int spaces_to_pad = max_name_length - name_length; |
|
559 for (int i = 0; i < spaces_to_pad; i++) |
|
560 os << " "; |
|
561 pos += max_name_length; |
|
562 } |
|
563 os << "\n"; |
|
564 } |
|
565 |
|
566 return os; |
|
567 } |
|
568 |
581
|
569 // See if the given file is in the path. |
|
570 |
1755
|
571 string |
|
572 search_path_for_file (const string& path, const string& name) |
686
|
573 { |
1755
|
574 string retval; |
686
|
575 |
1755
|
576 char *tmp = kpse_path_search (path.c_str (), name.c_str (), |
|
577 kpathsea_true); |
686
|
578 |
|
579 if (tmp) |
|
580 { |
|
581 retval = make_absolute (tmp, the_current_working_directory); |
|
582 free (tmp); |
|
583 } |
|
584 |
|
585 return retval; |
|
586 } |
|
587 |
1488
|
588 DEFUN ("file_in_path", Ffile_in_path, Sfile_in_path, 10, |
686
|
589 "file_in_path (PATH, NAME)") |
|
590 { |
|
591 Octave_object retval; |
|
592 |
1755
|
593 int argc = args.length () + 1; |
|
594 |
|
595 string_vector argv = make_argv (args, "file_in_path"); |
|
596 |
|
597 if (error_state) |
|
598 return retval; |
686
|
599 |
|
600 if (argc == 3) |
|
601 { |
1755
|
602 string fname = search_path_for_file (argv[1], argv[2]); |
686
|
603 |
1755
|
604 if (fname.empty ()) |
|
605 retval = Matrix (); |
|
606 else |
686
|
607 retval = fname; |
|
608 } |
|
609 else |
|
610 print_usage ("file_in_path"); |
|
611 |
|
612 return retval; |
|
613 } |
|
614 |
1755
|
615 string |
|
616 file_in_path (const string& name, const string& suffix) |
526
|
617 { |
1755
|
618 string nm = name; |
526
|
619 |
1755
|
620 if (! suffix.empty ()) |
|
621 nm.append (suffix); |
686
|
622 |
1755
|
623 if (the_current_working_directory.empty ()) |
526
|
624 get_working_directory ("file_in_path"); |
|
625 |
1755
|
626 return search_path_for_file (user_pref.loadpath, nm); |
526
|
627 } |
|
628 |
581
|
629 // See if there is an function file in the path. If so, return the |
|
630 // full path to the file. |
|
631 |
1755
|
632 string |
|
633 fcn_file_in_path (const string& name) |
526
|
634 { |
1755
|
635 string retval; |
908
|
636 |
1755
|
637 int len = name.length (); |
|
638 |
|
639 if (len > 0) |
|
640 { |
|
641 if (len > 2 && name [len - 2] == '.' && name [len - 1] == 'm') |
|
642 retval = file_in_path (name, ""); |
908
|
643 else |
1755
|
644 retval = file_in_path (name, ".m"); |
908
|
645 } |
1755
|
646 |
|
647 return retval; |
526
|
648 } |
|
649 |
581
|
650 // See if there is an octave file in the path. If so, return the |
|
651 // full path to the file. |
|
652 |
1755
|
653 string |
|
654 oct_file_in_path (const string& name) |
572
|
655 { |
1755
|
656 string retval; |
908
|
657 |
1755
|
658 int len = name.length (); |
|
659 |
|
660 if (len > 0) |
|
661 { |
|
662 if (len > 2 && name [len - 4] == '.' && name [len - 3] == 'o' |
908
|
663 && name [len - 2] == 'c' && name [len - 1] == 't') |
1755
|
664 retval = file_in_path (name, ""); |
908
|
665 else |
1755
|
666 retval = file_in_path (name, ".oct"); |
908
|
667 } |
1755
|
668 |
|
669 return retval; |
572
|
670 } |
|
671 |
1755
|
672 const char * |
801
|
673 undo_string_escape (char c) |
|
674 { |
|
675 if (! c) |
1755
|
676 return ""; |
801
|
677 |
|
678 switch (c) |
|
679 { |
|
680 case '\a': |
|
681 return "\\a"; |
|
682 |
|
683 case '\b': // backspace |
|
684 return "\\b"; |
|
685 |
|
686 case '\f': // formfeed |
|
687 return "\\f"; |
|
688 |
|
689 case '\n': // newline |
|
690 return "\\n"; |
|
691 |
|
692 case '\r': // carriage return |
|
693 return "\\r"; |
|
694 |
|
695 case '\t': // horizontal tab |
|
696 return "\\t"; |
|
697 |
|
698 case '\v': // vertical tab |
|
699 return "\\v"; |
|
700 |
|
701 case '\\': // backslash |
|
702 return "\\\\"; |
|
703 |
|
704 case '"': // double quote |
|
705 return "\\\""; |
|
706 |
|
707 default: |
1755
|
708 { |
|
709 static char retval[2]; |
|
710 retval[0] = c; |
|
711 retval[1] = '\0'; |
|
712 return retval; |
|
713 } |
801
|
714 } |
|
715 } |
|
716 |
1755
|
717 string |
|
718 undo_string_escapes (const string& s) |
801
|
719 { |
1755
|
720 string retval; |
801
|
721 |
1755
|
722 for (size_t i = 0; i < s.length (); i++) |
|
723 retval.append (undo_string_escape (s[i])); |
801
|
724 |
1755
|
725 return retval; |
801
|
726 } |
|
727 |
|
728 DEFUN ("undo_string_escapes", Fundo_string_escapes, |
1488
|
729 Sundo_string_escapes, 10, |
801
|
730 "undo_string_escapes (STRING)") |
|
731 { |
|
732 tree_constant retval; |
|
733 |
|
734 int nargin = args.length (); |
|
735 |
|
736 if (nargin == 1 && args(0).is_string ()) |
1755
|
737 retval = undo_string_escapes (args(0).string_value ()); |
801
|
738 else |
1023
|
739 print_usage ("undo_string_escapes"); |
801
|
740 |
|
741 return retval; |
|
742 } |
|
743 |
1711
|
744 // This function was adapted from xputenv from Karl Berry's kpathsearch |
|
745 // library. |
|
746 |
|
747 void |
|
748 oct_putenv (const char *var_name, const char *value) |
|
749 { |
|
750 static const char **saved_env_items = 0; |
|
751 static unsigned saved_len; |
|
752 char *old_item = 0; |
|
753 |
|
754 int new_len = strlen (var_name) + strlen (value) + 2; |
|
755 |
|
756 char *new_item = new char [new_len]; |
|
757 |
|
758 sprintf (new_item, "%s=%s", var_name, value); |
|
759 |
|
760 #ifndef SMART_PUTENV |
|
761 |
|
762 // Check if we have saved anything yet. |
|
763 |
|
764 if (! saved_env_items) |
|
765 { |
|
766 saved_env_items = new const char * [1]; |
|
767 saved_env_items[0] = var_name; |
|
768 saved_len = 1; |
|
769 } |
|
770 else |
|
771 { |
|
772 // Check if we've assigned VAR_NAME before. |
|
773 |
|
774 unsigned len = strlen (var_name); |
|
775 |
|
776 for (unsigned i = 0; i < saved_len && ! old_item; i++) |
|
777 { |
|
778 if (strcmp (saved_env_items[i], var_name) == 0) |
|
779 { |
|
780 old_item = getenv (var_name); |
|
781 |
|
782 assert (old_item); |
|
783 |
|
784 // Back up to the `NAME=' in the environment before the |
|
785 // value that getenv returns. |
|
786 |
|
787 old_item -= (len + 1); |
|
788 } |
|
789 } |
|
790 |
|
791 if (! old_item) |
|
792 { |
|
793 // If we haven't seen VAR_NAME before, save it. Assume it |
|
794 // is in safe storage. |
|
795 |
|
796 saved_len++; |
|
797 |
|
798 const char **tmp = new const char * [saved_len]; |
|
799 |
|
800 for (unsigned i = 0; i < saved_len - 1; i++) |
|
801 tmp[i] = saved_env_items[i]; |
|
802 |
|
803 tmp[saved_len - 1] = var_name; |
|
804 |
|
805 delete [] saved_env_items; |
|
806 |
|
807 saved_env_items = tmp; |
|
808 } |
|
809 } |
|
810 |
|
811 #endif |
|
812 |
|
813 // As far as I can see there's no way to distinguish between the |
|
814 // various errors; putenv doesn't have errno values. |
|
815 |
|
816 if (putenv (new_item) < 0) |
|
817 error ("putenv (%s) failed", new_item); |
|
818 |
|
819 #ifndef SMART_PUTENV |
|
820 |
|
821 // Can't free `new_item' because its contained value is now in |
|
822 // `environ', but we can free `old_item', since it's been replaced. |
|
823 |
|
824 delete [] old_item; |
|
825 |
|
826 #endif |
|
827 } |
|
828 |
572
|
829 /* |
1
|
830 ;;; Local Variables: *** |
|
831 ;;; mode: C++ *** |
|
832 ;;; page-delimiter: "^/\\*" *** |
|
833 ;;; End: *** |
|
834 */ |