1: #ifndef lint
   2: static char *osccsid = "@(#)parsit.c	1.1	(Berkeley) 12/5/84";
   3: static char *sccsid = "@(#)parsit.c	1.2	(Berkeley) 10/15/87";
   4: #endif
   5: 
   6: /*
   7:  * Parse a string of words separated by spaces into an
   8:  * array of pointers to characters, just like good ol' argv[]
   9:  * and argc.
  10:  *
  11:  * Usage:
  12:  *
  13:  * char line[132];
  14:  * char **argv;
  15:  * int argc;
  16:  *
  17:  *	argv = (char **) NULL;
  18:  *	argc = parsit(line, &argv);
  19:  *
  20:  * returns the number of words parsed in argc.  argv[argc] will
  21:  * be (char *) NULL to indicate end of list, if you're not
  22:  * happy with just knowing how many words you have.
  23:  *
  24:  * Note that setting argv = (char **) NULL is only done the first
  25:  * time the routine is called with a new "argv" -- it tells
  26:  * parsit that "argv" is a new array, and parsit shouldn't free
  27:  * up the elements (as it would do if it were an old array).
  28:  *
  29:  *	Phil Lapsley
  30:  *	College of Engineering
  31:  *	University of California, Berkeley
  32:  *	(ARPA: phil@Berkeley.ARPA; UUCP: ...!ucbvax!phil)
  33:  */
  34: 
  35: #include <stdio.h>
  36: 
  37: extern  char    *malloc(), *strcpy();
  38: 
  39: parsit(line, array)
  40: char *line;
  41: char ***array;
  42: {
  43:     char    **argv;
  44:     char    word[132];
  45:     char    *linecp;
  46:     int i, j, num_words;
  47: 
  48:     argv = *array;
  49:     if (argv != (char **) NULL) {  /* Check to see if we should */
  50:         for (i = 0; argv[i] != (char *) NULL; i++)  /* free */
  51:             free(argv[i]);  /* the old array */
  52:         free((char *) argv);    /* and then free the ptr itself */
  53:     }
  54: 
  55:     linecp = line;
  56:     num_words = 0;
  57:     while (1) { /* count words in input */
  58:         for (; *linecp == ' ' || *linecp == '\t'; ++linecp)
  59:             ;
  60:         if (*linecp == '\0')
  61:             break;
  62: 
  63:         for (; *linecp != ' ' && *linecp != '\t' && *linecp != '\0'; ++linecp)
  64:             ;
  65:         ++num_words;
  66:         if (*linecp == '\0')
  67:             break;
  68:     }
  69: 
  70:     /* Then malloc enough for that many words plus 1 (for null) */
  71: 
  72:     if ((argv = (char **) malloc((num_words + 1) * sizeof(char *))) ==
  73:         (char **) NULL) {
  74:         fprintf(stderr, "parsit: malloc out of space!\n");
  75:         return(0);
  76:     }
  77: 
  78:     j = i = 0;
  79:     while (1) { /* Now build the list of words */
  80:         for (; *line == ' ' || *line == '\t'; ++line)
  81:             ;
  82:         if (*line == '\0')
  83:             break;
  84: 
  85:         i = 0;
  86:         for (; *line != ' ' && *line != '\t' && *line != '\0'; ++line)
  87:             word[i++] =  *line;
  88:         word[i] = '\0';
  89:         argv[j] = malloc(strlen(word) + 1);
  90:         if (argv[j] == (char *) NULL) {
  91:             fprintf(stderr, "parsit: malloc out of space!\n");
  92:             return(0);
  93:         }
  94: 
  95:         (void) strcpy(argv[j], word);
  96:         ++j;
  97:         if (*line == '\0')
  98:             break;
  99:     }
 100:     argv[j] = (char *) NULL;  /* remember null at end of list */
 101:     *array = argv;
 102:     return(j);
 103: }

Defined functions

Defined variables

osccsid defined in line 2; never used
sccsid defined in line 3; never used
Last modified: 1988-01-05
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 2552
Valid CSS Valid XHTML 1.0 Strict