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

Defined functions

Defined variables

sccsid defined in line 2; never used
Last modified: 1986-03-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 952
Valid CSS Valid XHTML 1.0 Strict