X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdata%2Ffilename.c;h=323a78c54ec32a67f9dac784ca5d25a18d212dad;hb=7a39f11cfc29e817188f4fa4d2eec708e8b15de8;hp=569d185d223a1313e756cb50ac2b7f7563a7bfd8;hpb=a269ac443ece11402969c661efae4c13cbbdea0a;p=pspp diff --git a/src/data/filename.c b/src/data/filename.c index 569d185d22..323a78c54e 100644 --- a/src/data/filename.c +++ b/src/data/filename.c @@ -69,69 +69,91 @@ fn_init (void) /* Functions for performing operations on filenames. */ -/* Substitutes $variables as defined by GETENV into INPUT and returns - a copy of the resultant string. Supports $var and ${var} syntaxes; - $$ substitutes as $. */ -char * -fn_interp_vars (const char *input, const char *(*getenv) (const char *)) + +/* Substitutes $variables as defined by GETENV into TARGET. + TARGET must be a string containing the text for which substitution + is required. + Supports $var and ${var} syntaxes; + $$ substitutes as $. +*/ +void +fn_interp_vars (struct string *target, + const char *(*getenv) (const char *)) { - struct string output; + char *input ; + char *s ; - if (NULL == strchr (input, '$')) - return xstrdup (input); + assert (target); - ds_init (&output, strlen (input)); + input = xmalloc(ds_length(target) + 1); + s = input; + + strcpy(input, ds_c_str(target)); + + if (NULL == strchr (ds_c_str(target), '$')) + goto done; + + ds_clear(target); for (;;) - switch (*input) - { - case '\0': - return ds_c_str (&output); + { + switch (*s) + { + case '\0': + goto done ; - case '$': - input++; + case '$': + s++; - if (*input == '$') - { - ds_putc (&output, '$'); - input++; - } - else - { - int stop; - int start; - const char *value; - - start = ds_length (&output); - - if (*input == '(') - { - stop = ')'; - input++; - } - else if (*input == '{') - { - stop = '}'; - input++; - } - else - stop = 0; - - while (*input && *input != stop - && (stop || isalpha ((unsigned char) *input))) - ds_putc (&output, *input++); + if (*s == '$') + { + ds_putc (target, '$'); + s++; + } + else + { + int stop; + int start; + const char *value; + + start = ds_length (target); + + if (*s == '(') + { + stop = ')'; + s++; + } + else if (*s == '{') + { + stop = '}'; + s++; + } + else + stop = 0; + + while (*s && *s != stop + && (stop || isalpha ((unsigned char) *s))) + { + ds_putc (target, *s++); + } - value = getenv (ds_c_str (&output) + start); - ds_truncate (&output, start); - ds_puts (&output, value); + value = getenv (ds_c_str (target) + start); + ds_truncate (target, start); + ds_puts (target, value); - if (stop && *input == stop) - input++; - } + if (stop && *s == stop) + s++; + } + break; - default: - ds_putc (&output, *input++); - } + default: + ds_putc (target, *s++); + } + } + + done: + free(input); + return; } #ifdef unix @@ -218,9 +240,14 @@ fn_search_path (const char *basename, const char *path, const char *prepend) return fn_tilde_expand (basename); { - char *temp = fn_interp_vars (path, fn_getenv); - bp = subst_path = fn_tilde_expand (temp); - free (temp); + struct string temp; + ds_create(&temp, path); + + fn_interp_vars(&temp, fn_getenv); + + bp = subst_path = fn_tilde_expand (ds_c_str(&temp)); + + ds_destroy(&temp); } msg (VM (4), _("Searching for `%s'..."), basename); @@ -569,10 +596,10 @@ fn_get_cwd (void) int size = 2; char *buffer = xmalloc (size); if ( buffer) - { - buffer[0]='.'; - buffer[1]='\0'; - } + { + buffer[0]='.'; + buffer[1]='\0'; + } return buffer; @@ -866,10 +893,10 @@ fn_close_ext (struct file_ext *f) #ifdef unix /* A file's identity. */ struct file_identity - { - dev_t device; /* Device number. */ - ino_t inode; /* Inode number. */ - }; +{ + dev_t device; /* Device number. */ + ino_t inode; /* Inode number. */ +}; /* Returns a pointer to a dynamically allocated structure whose value can be used to tell whether two files are actually the @@ -915,9 +942,9 @@ fn_compare_file_identities (const struct file_identity *a, #else /* not unix */ /* A file's identity. */ struct file_identity - { - char *normalized_filename; /* File's normalized name. */ - }; +{ + char *normalized_filename; /* File's normalized name. */ +}; /* Returns a pointer to a dynamically allocated structure whose value can be used to tell whether two files are actually the