home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NNTP-1.000 / NNTP-1 / nntp.1.5.11t / server / parsit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-17  |  2.6 KB  |  112 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)parsit.c    1.4    (Berkeley) 8/27/89";
  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;
  44.     char    *linecp;
  45.     int    i, j, num_words,longest_word;
  46.  
  47.     argv = *array;
  48.     if (argv != (char **) NULL) {  /* Check to see if we should */
  49.         for (i = 0; argv[i] != (char *) NULL; i++)    /* free */
  50.             free(argv[i]);    /* the old array */
  51.         free((char *) argv);    /* and then free the ptr itself */
  52.     }
  53.  
  54.     linecp = line;
  55.     num_words = longest_word = 0;
  56.     while (1) {    /* count words in input */
  57.         for (; *linecp == ' ' || *linecp == '\t'; ++linecp)
  58.             ;
  59.         if (*linecp == '\0')
  60.             break;
  61.         word = linecp;
  62.         for (; *linecp != ' ' && *linecp != '\t' && *linecp != '\0'; ++linecp)
  63.             ;
  64.         ++num_words;
  65.         if ((i = linecp - word) > longest_word) longest_word = i;
  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.     /* malloc enough the longest word */
  78.  
  79.     if ((word = (char *) malloc(longest_word+1)) ==    (char *) NULL) {
  80.         fprintf(stderr, "parsit: malloc out of space!\n");
  81.         return(0);
  82.     }
  83.  
  84.     j = i = 0;
  85.     while (1) {    /* Now build the list of words */
  86.         for (; *line == ' ' || *line == '\t'; ++line)
  87.             ;
  88.         if (*line == '\0')
  89.             break;
  90.  
  91.         i = 0;
  92.         for (; *line != ' ' && *line != '\t' && *line != '\0'; ++line)
  93.             word[i++] =  *line;
  94.         word[i] = '\0';
  95.         argv[j] = malloc(strlen(word) + 1);
  96.         if (argv[j] == (char *) NULL) {
  97.             fprintf(stderr, "parsit: malloc out of space!\n");
  98.             free(word);
  99.             return(0);
  100.         }
  101.  
  102.         (void) strcpy(argv[j], word);
  103.         ++j;
  104.         if (*line == '\0')
  105.             break;
  106.     }
  107.     argv[j] = (char *) NULL;  /* remember null at end of list */
  108.     *array = argv;
  109.     free(word);
  110.     return(j);
  111. }
  112.