OSDN Git Service

- trim any trailing whitespace
[uclinux-h8/uClibc.git] / libc / misc / wordexp / wordexp.c
index ea69bc6..145c24c 100644 (file)
    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.  */
 
-#define mempcpy __mempcpy
-#define stpcpy __stpcpy
-#define strndup __strndup
-#define strspn __strspn
-#define unsetenv __unsetenv
-#define waitpid __waitpid
-#define kill __kill
-
-#define _GNU_SOURCE
-#include <sys/cdefs.h>
+#include <features.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <fcntl.h>
 #include <glob.h>
 #include <wordexp.h>
 
+/* Experimentally off - libc_hidden_proto(mempcpy) */
+/* Experimentally off - libc_hidden_proto(stpcpy) */
+/* Experimentally off - libc_hidden_proto(strchr) */
+/* Experimentally off - libc_hidden_proto(strcpy) */
+/* Experimentally off - libc_hidden_proto(strdup) */
+/* Experimentally off - libc_hidden_proto(strlen) */
+/* Experimentally off - libc_hidden_proto(strndup) */
+/* Experimentally off - libc_hidden_proto(strspn) */
+/* Experimentally off - libc_hidden_proto(strcspn) */
+libc_hidden_proto(setenv)
+libc_hidden_proto(unsetenv)
+libc_hidden_proto(waitpid)
+libc_hidden_proto(kill)
+libc_hidden_proto(getuid)
+libc_hidden_proto(getpwnam_r)
+libc_hidden_proto(getpwuid_r)
+libc_hidden_proto(execve)
+libc_hidden_proto(dup2)
+libc_hidden_proto(atoi)
+libc_hidden_proto(fnmatch)
+libc_hidden_proto(pipe)
+libc_hidden_proto(fork)
+libc_hidden_proto(open)
+libc_hidden_proto(close)
+libc_hidden_proto(read)
+libc_hidden_proto(getenv)
+libc_hidden_proto(getpid)
+libc_hidden_proto(sprintf)
+libc_hidden_proto(fprintf)
+libc_hidden_proto(abort)
+libc_hidden_proto(glob)
+libc_hidden_proto(globfree)
+libc_hidden_proto(wordfree)
+#ifdef __UCLIBC_HAS_XLOCALE__
+libc_hidden_proto(__ctype_b_loc)
+#elif defined __UCLIBC_HAS_CTYPE_TABLES__
+libc_hidden_proto(__ctype_b)
+#endif
+
 #define __WORDEXP_FULL
 //#undef __WORDEXP_FULL
 
@@ -56,8 +86,8 @@
 //extern char **__libc_argv;
 
 /* FIXME!!!! */
-int __libc_argc;
-char **__libc_argv;
+int attribute_hidden __libc_argc;
+char attribute_hidden **__libc_argv;
 
 /* Some forward declarations */
 static int parse_dollars(char **word, size_t * word_length,
@@ -80,14 +110,14 @@ static int parse_dquote(char **word, size_t * word_length,
 #define W_CHUNK        (100)
 
 /* Result of w_newword will be ignored if it's the last word. */
-static inline char *w_newword(size_t * actlen, size_t * maxlen)
+static __inline__ char *w_newword(size_t * actlen, size_t * maxlen)
 {
        *actlen = *maxlen = 0;
        return NULL;
 }
 
 /* Add a character to the buffer, allocating room for it if needed.  */
-static inline char *w_addchar(char *buffer, size_t * actlen,
+static __inline__ char *w_addchar(char *buffer, size_t * actlen,
                                                          size_t * maxlen, char ch)
         /* (lengths exclude trailing zero) */
 {
@@ -138,7 +168,7 @@ static char *w_addstr(char *buffer, size_t * actlen, size_t * maxlen,
 {
        size_t len;
        assert(str != NULL);            /* w_addstr only called from this file */
-       len = __strlen(str);
+       len = strlen(str);
 
        return w_addmem(buffer, actlen, maxlen, str, len);
 }
@@ -153,7 +183,7 @@ static int w_addword(wordexp_t * pwordexp, char *word)
         * the caller sees them.
         */
        if (word == NULL) {
-               word = __strdup("");
+               word = strdup("");
                if (word == NULL)
                        goto no_space;
        }
@@ -255,7 +285,7 @@ parse_tilde(char **word, size_t * word_length, size_t * max_length,
        if (*word_length != 0) {
                if (!((*word)[*word_length - 1] == '=' && wordc == 0)) {
                        if (!((*word)[*word_length - 1] == ':'
-                                 && __strchr(*word, '=') && wordc == 0)) {
+                                 && strchr(*word, '=') && wordc == 0)) {
                                *word = w_addchar(*word, word_length, max_length, '~');
                                return *word ? 0 : WRDE_NOSPACE;
                        }
@@ -286,7 +316,7 @@ parse_tilde(char **word, size_t * word_length, size_t * max_length,
                   results are unspecified.  We do a lookup on the uid if
                   HOME is unset. */
 
-               home = __getenv("HOME");
+               home = getenv("HOME");
                if (home != NULL) {
                        *word = w_addstr(*word, word_length, max_length, home);
                        if (*word == NULL)
@@ -295,8 +325,8 @@ parse_tilde(char **word, size_t * word_length, size_t * max_length,
                        uid = getuid();
                        buffer = alloca(buflen);
 
-                       while ((result = getpwuid_r(uid, &pwd, buffer, buflen, &tpwd)) 
-                                       != 0 && errno == ERANGE) 
+                       while ((result = getpwuid_r(uid, &pwd, buffer, buflen, &tpwd))
+                                       != 0 && errno == ERANGE)
                        {
                                buflen += 1000;
                                buffer = alloca(buflen);
@@ -380,7 +410,7 @@ do_parse_glob(const char *glob_word, char **word, size_t * word_length,
        }
 
        for (match = 0; match < globbuf.gl_pathc; ++match) {
-               char *matching_word = __strdup(globbuf.gl_pathv[match]);
+               char *matching_word = strdup(globbuf.gl_pathv[match]);
 
                if (matching_word == NULL || w_addword(pwordexp, matching_word)) {
                        globfree(&globbuf);
@@ -407,8 +437,8 @@ parse_glob(char **word, size_t * word_length, size_t * max_length,
        glob_list.we_wordv = NULL;
        glob_list.we_offs = 0;
        for (; words[*offset] != '\0'; ++*offset) {
-               if ((ifs && __strchr(ifs, words[*offset])) ||
-                       (!ifs && __strchr(" \t\n", words[*offset])))
+               if ((ifs && strchr(ifs, words[*offset])) ||
+                       (!ifs && strchr(" \t\n", words[*offset])))
                        /* Reached IFS */
                        break;
 
@@ -765,24 +795,24 @@ exec_comm_child(char *comm, int *fildes, int showerr, int noexec)
 
        /* Redirect output.  */
        dup2(fildes[1], 1);
-       __close(fildes[1]);
+       close(fildes[1]);
 
        /* Redirect stderr to /dev/null if we have to.  */
        if (showerr == 0) {
                int fd;
 
-               __close(2);
-               fd = __open(_PATH_DEVNULL, O_WRONLY);
+               close(2);
+               fd = open(_PATH_DEVNULL, O_WRONLY);
                if (fd >= 0 && fd != 2) {
                        dup2(fd, 2);
-                       __close(fd);
+                       close(fd);
                }
        }
 
        /* Make sure the subshell doesn't field-split on our behalf. */
        unsetenv("IFS");
 
-       __close(fildes[0]);
+       close(fildes[0]);
        execve(_PATH_BSHELL, (char *const *) args, __environ);
 
        /* Bad.  What now?  */
@@ -815,8 +845,8 @@ exec_comm(char *comm, char **word, size_t * word_length,
 
        if ((pid = fork()) < 0) {
                /* Bad */
-               __close(fildes[0]);
-               __close(fildes[1]);
+               close(fildes[0]);
+               close(fildes[1]);
                return WRDE_NOSPACE;
        }
 
@@ -825,17 +855,17 @@ exec_comm(char *comm, char **word, size_t * word_length,
 
        /* Parent */
 
-       __close(fildes[1]);
+       close(fildes[1]);
        buffer = alloca(bufsize);
 
        if (!pwordexp)
                /* Quoted - no field splitting */
        {
                while (1) {
-                       if ((buflen = __read(fildes[0], buffer, bufsize)) < 1) {
+                       if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
                                if (waitpid(pid, &status, WNOHANG) == 0)
                                        continue;
-                               if ((buflen = __read(fildes[0], buffer, bufsize)) < 1)
+                               if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
                                        break;
                        }
 
@@ -858,17 +888,17 @@ exec_comm(char *comm, char **word, size_t * word_length,
                 */
 
                while (1) {
-                       if ((buflen = __read(fildes[0], buffer, bufsize)) < 1) {
+                       if ((buflen = read(fildes[0], buffer, bufsize)) < 1) {
                                if (waitpid(pid, &status, WNOHANG) == 0)
                                        continue;
-                               if ((buflen = __read(fildes[0], buffer, bufsize)) < 1)
+                               if ((buflen = read(fildes[0], buffer, bufsize)) < 1)
                                        break;
                        }
 
                        for (i = 0; i < buflen; ++i) {
-                               if (__strchr(ifs, buffer[i]) != NULL) {
+                               if (strchr(ifs, buffer[i]) != NULL) {
                                        /* Current character is IFS */
-                                       if (__strchr(ifs_white, buffer[i]) == NULL) {
+                                       if (strchr(ifs_white, buffer[i]) == NULL) {
                                                /* Current character is IFS but not whitespace */
                                                if (copying == 2) {
                                                        /*            current character
@@ -962,7 +992,7 @@ exec_comm(char *comm, char **word, size_t * word_length,
                }
        }
 
-       __close(fildes[0]);
+       close(fildes[0]);
 
        /* Check for syntax error (re-execute but with "-n" flag) */
        if (buflen < 1 && status != 0) {
@@ -985,7 +1015,7 @@ exec_comm(char *comm, char **word, size_t * word_length,
   no_space:
        kill(pid, SIGKILL);
        waitpid(pid, NULL, 0);
-       __close(fildes[0]);
+       close(fildes[0]);
        return WRDE_NOSPACE;
 }
 
@@ -1046,8 +1076,7 @@ parse_comm(char **word, size_t * word_length, size_t * max_length,
        }
 
        /* Premature end */
-       if (comm)
-               free(comm);
+       free(comm);
 
        return WRDE_SYNTAX;
 }
@@ -1184,7 +1213,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                goto envsubst;
                }
                while (isdigit(words[++*offset]));
-       } else if (__strchr("*@$", words[*offset]) != NULL) {
+       } else if (strchr("*@$", words[*offset]) != NULL) {
                /* Special parameter. */
                special = 1;
                env = w_addchar(env, &env_length, &env_maxlen, words[*offset]);
@@ -1220,7 +1249,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                        break;
 
                case ':':
-                       if (__strchr("-=?+", words[1 + *offset]) == NULL)
+                       if (strchr("-=?+", words[1 + *offset]) == NULL)
                                goto syntax;
 
                        colon_seen = 1;
@@ -1331,7 +1360,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                /* Is it `$$'? */
                if (*env == '$') {
                        buffer[20] = '\0';
-                       value = _itoa(__getpid(), &buffer[20]);
+                       value = _itoa(getpid(), &buffer[20]);
                }
                /* Is it `${#*}' or `${#@}'? */
                else if ((*env == '*' || *env == '@') && seen_hash) {
@@ -1340,8 +1369,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                                           &buffer[20]);
                        *word = w_addstr(*word, word_length, max_length, value);
                        free(env);
-                       if (pattern)
-                               free(pattern);
+                       free(pattern);
                        return *word ? 0 : WRDE_NOSPACE;
                }
                /* Is it `$*' or `$@' (unquoted) ? */
@@ -1352,7 +1380,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
 
                        /* Build up value parameter by parameter (copy them) */
                        for (p = 1; __libc_argv[p]; ++p)
-                               plist_len += __strlen(__libc_argv[p]) + 1;      /* for space */
+                               plist_len += strlen(__libc_argv[p]) + 1;        /* for space */
                        value = malloc(plist_len);
                        if (value == NULL)
                                goto no_space;
@@ -1382,7 +1410,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                        goto no_space;
 
                                for (p = 2; __libc_argv[p + 1]; p++) {
-                                       char *newword = __strdup(__libc_argv[p]);
+                                       char *newword = strdup(__libc_argv[p]);
 
                                        if (newword == NULL || w_addword(pwordexp, newword))
                                                goto no_space;
@@ -1398,7 +1426,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                        }
                }
        } else
-               value = __getenv(env);
+               value = getenv(env);
 
        if (value == NULL && (flags & WRDE_UNDEF)) {
                /* Variable not defined. */
@@ -1500,8 +1528,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                                if (free_value)
                                                        free(value);
 
-                                               if (expanded)
-                                                       free(expanded);
+                                               free(expanded);
 
                                                goto do_error;
                                        }
@@ -1520,8 +1547,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                                if (free_value)
                                                        free(value);
 
-                                               if (expanded)
-                                                       free(expanded);
+                                               free(expanded);
 
                                                goto do_error;
                                        }
@@ -1543,8 +1569,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                        goto no_space;
                        }
 
-                       if (pattern)
-                               free(pattern);
+                       free(pattern);
 
                        pattern = expanded;
                }
@@ -1562,7 +1587,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                        if (value == NULL || pattern == NULL || *pattern == '\0')
                                break;
 
-                       end = value + __strlen(value);
+                       end = value + strlen(value);
 
                        switch (action) {
                        case ACT_RP_SHORT_LEFT:
@@ -1572,7 +1597,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                        if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
                                                *p = c;
                                                if (free_value) {
-                                                       char *newval = __strdup(p);
+                                                       char *newval = strdup(p);
 
                                                        if (newval == NULL) {
                                                                free(value);
@@ -1596,7 +1621,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                        if (fnmatch(pattern, value, 0) != FNM_NOMATCH) {
                                                *p = c;
                                                if (free_value) {
-                                                       char *newval = __strdup(p);
+                                                       char *newval = strdup(p);
 
                                                        if (newval == NULL) {
                                                                free(value);
@@ -1693,14 +1718,14 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                /* Substitute parameter */
                                break;
 
-                       if (free_value && value)
+                       if (free_value)
                                free(value);
 
                        if (!colon_seen && value)
                                /* Substitute NULL */
                                goto success;
 
-                       value = pattern ? __strdup(pattern) : pattern;
+                       value = pattern ? strdup(pattern) : pattern;
                        free_value = 1;
 
                        if (pattern && !value)
@@ -1710,10 +1735,10 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
 
                case ACT_NONNULL_SUBST:
                        if (value && (*value || !colon_seen)) {
-                               if (free_value && value)
+                               if (free_value)
                                        free(value);
 
-                               value = pattern ? __strdup(pattern) : pattern;
+                               value = pattern ? strdup(pattern) : pattern;
                                free_value = 1;
 
                                if (pattern && !value)
@@ -1739,10 +1764,10 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                                goto success;
                        }
 
-                       if (free_value && value)
+                       if (free_value)
                                free(value);
 
-                       value = pattern ? __strdup(pattern) : pattern;
+                       value = pattern ? strdup(pattern) : pattern;
                        free_value = 1;
 
                        if (pattern && !value)
@@ -1766,7 +1791,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
 
                param_length[20] = '\0';
                *word = w_addstr(*word, word_length, max_length,
-                                                _itoa(value ? __strlen(value) : 0,
+                                                _itoa(value ? strlen(value) : 0,
                                                                        &param_length[20]));
                if (free_value) {
                        assert(value != NULL);
@@ -1788,7 +1813,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
                return *word ? 0 : WRDE_NOSPACE;
        } else {
                /* Need to field-split */
-               char *value_copy = __strdup(value);     /* Don't modify value */
+               char *value_copy = strdup(value);       /* Don't modify value */
                char *field_begin = value_copy;
                int seen_nonws_ifs = 0;
 
@@ -1828,7 +1853,7 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
 
                        /* Skip at most one non-whitespace IFS character after the field */
                        seen_nonws_ifs = 0;
-                       if (*next_field && __strchr(ifs, *next_field)) {
+                       if (*next_field && strchr(ifs, *next_field)) {
                                seen_nonws_ifs = 1;
                                next_field++;
                        }
@@ -1865,16 +1890,14 @@ parse_param(char **word, size_t * word_length, size_t * max_length,
        error = WRDE_SYNTAX;
 
   do_error:
-       if (env)
-               free(env);
+       free(env);
 
-       if (pattern)
-               free(pattern);
+       free(pattern);
 
        return error;
 }
 #else
-static inline int
+static __inline__ int
 parse_backtick(char **word, size_t * word_length, size_t * max_length,
                           const char *words, size_t * offset, int flags,
                           wordexp_t * pwordexp, const char *ifs,
@@ -2025,6 +2048,7 @@ void wordfree(wordexp_t * pwordexp)
                pwordexp->we_wordv = NULL;
        }
 }
+libc_hidden_def(wordfree)
 
 /*
  * wordexp()
@@ -2070,11 +2094,11 @@ int wordexp(const char *words, wordexp_t * we, int flags)
        /* Find out what the field separators are.
         * There are two types: whitespace and non-whitespace.
         */
-       ifs = __getenv("IFS");
+       ifs = getenv("IFS");
 
        if (!ifs)
                /* IFS unset - use <space><tab><newline>. */
-               ifs = __strcpy(ifs_white, " \t\n");
+               ifs = strcpy(ifs_white, " \t\n");
        else {
                char *ifsch = ifs;
                char *whch = ifs_white;
@@ -2193,11 +2217,11 @@ int wordexp(const char *words, wordexp_t * we, int flags)
 
                default:
                        /* Is it a word separator? */
-                       if (__strchr(" \t", words[words_offset]) == NULL) {
+                       if (strchr(" \t", words[words_offset]) == NULL) {
                                char ch = words[words_offset];
 
                                /* Not a word separator -- but is it a valid word char? */
-                               if (__strchr("\n|&;<>(){}", ch)) {
+                               if (strchr("\n|&;<>(){}", ch)) {
                                        /* Fail */
                                        error = WRDE_BADCHAR;
                                        goto do_error;
@@ -2238,8 +2262,7 @@ int wordexp(const char *words, wordexp_t * we, int flags)
         *  set we members back to what they were.
         */
 
-       if (word != NULL)
-               free(word);
+       free(word);
 
        if (error == WRDE_NOSPACE)
                return WRDE_NOSPACE;