Annotation of coherent/b/lib/libc/gen/_addargs.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * libc/gen/_addargs.c
                      3:  * Add environmental args to argc/argv.
                      4:  */
                      5: 
                      6: #include <stdio.h>
                      7: #include <string.h>
                      8: #include <ctype.h>
                      9: 
                     10: #define        NBUF    128
                     11: 
                     12: /* External. */
                     13: extern char *getenv();
                     14: extern char *malloc();
                     15: 
                     16: /* Forward. */
                     17: static char    **copyargs();
                     18: static int     getargs();
                     19: static char    *scanarg();
                     20: 
                     21: /*
                     22:  * Add "nameHEAD" and "nameTAIL" to the supplied argc/argv.
                     23:  * Return -1 on failure, 0 if argc/argv unchanged, 1 otherwise.
                     24:  */
                     25: int
                     26: _addargs(name, argcp, argvp) char *name; int *argcp; char ***argvp;
                     27: {
                     28:        register char **cpp, **oargv;
                     29:        register int oargc, hargc, targc;
                     30:        char *hp, *tp;
                     31:        
                     32:        if ((hargc = getargs(name, "HEAD", &hp)) == -1)
                     33:                return -1;
                     34:        if ((targc = getargs(name, "TAIL", &tp)) == -1)
                     35:                return -1;
                     36:        if (hargc == 0 && targc == 0)
                     37:                return 0;
                     38:        oargc = *argcp;
                     39:        oargv = *argvp;
                     40:        if ((cpp = (char **)malloc((hargc + oargc + targc + 1) * sizeof(char *))) == NULL)
                     41:                return -1;
                     42:        *argcp = hargc + oargc + targc;
                     43:        *argvp = cpp;
                     44:        *cpp++ = *oargv++;                      /* old argv[0] */
                     45:        if (hargc != 0)
                     46:                cpp = copyargs(cpp, hp);        /* HEAD args */
                     47:        while (*oargv != NULL)
                     48:                *cpp++ = *oargv++;              /* remainder of old argv[] */
                     49:        if (targc != 0)
                     50:                cpp = copyargs(cpp, tp);        /* TAIL args */
                     51:        *cpp = NULL;                            /* NULL-terminate */
                     52:        return 1;
                     53: }
                     54: 
                     55: /*
                     56:  * NUL-terminate each whitespace-separated arg in p
                     57:  * and store a pointer to each arg in cpp.
                     58:  * Return the updated cpp.
                     59:  */
                     60: static
                     61: char **
                     62: copyargs(cpp, p) register char **cpp; register char *p;
                     63: {
                     64:        register char c;
                     65: 
                     66:        while ((c = *p) != '\0') {
                     67:                if (isspace(c))
                     68:                        *p++ = '\0';
                     69:                else if (c == '\'' || c == '"') {
                     70:                        *cpp++ = p + 1;
                     71:                        p = scanarg(p);
                     72:                        if (*(p-1) == c)
                     73:                                *(p-1) = '\0';
                     74:                } else {
                     75:                        *cpp++ = p++;
                     76:                        p = scanarg(p);
                     77:                }
                     78:        }
                     79:        return cpp;
                     80: }
                     81: 
                     82: /*
                     83:  * Look for environmental variable "<name><ext>".
                     84:  * If found, malloc a copy and store its address through cpp.
                     85:  * Return -1 if malloc fails,
                     86:  * else return count of whitespace-separated args in result.
                     87:  */
                     88: static
                     89: int
                     90: getargs(name, ext, cpp) char *name, *ext; char **cpp;
                     91: {
                     92:        register char *cp, *p;
                     93:        register int count;
                     94:        char buf[NBUF];
                     95: 
                     96:        if ((cp = getenv(strcat(strcpy(buf, name), ext))) == NULL)
                     97:                return 0;
                     98:        if ((p = malloc(strlen(cp) + 1)) == NULL)
                     99:                return -1;
                    100:        *cpp = strcpy(p, cp);
                    101:        count = 0;
                    102:        while (*p != '\0') {
                    103:                if (!isspace(*p)) {
                    104:                        ++count;
                    105:                        p = scanarg(p);
                    106:                } else
                    107:                        ++p;
                    108:        }
                    109:        return count;
                    110: }
                    111: 
                    112: /*
                    113:  * Scan arg p to next whitespace or NUL.
                    114:  * Ignore whitespace within args quoted by ' ' or " ".
                    115:  * Return pointer to following character.
                    116:  */
                    117: static
                    118: char *
                    119: scanarg(p) register char *p;
                    120: {
                    121:        register char c;
                    122: 
                    123:        if ((c = *p) == '\'' || c == '"') {
                    124:                ++p;
                    125:                while (*p != '\0' && *p++ != c)
                    126:                        ;
                    127:        } else
                    128:                while (!isspace(*p) && *p != '\0')
                    129:                        ++p;
                    130:        return p;
                    131: }
                    132: 
                    133: /* end of _addargs.c */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.