static char *sccsid
= "@(#)parsit.c 1.1 (Berkeley) 12/5/84";
* Parse a string of words separated by spaces into an
* array of pointers to characters, just like good ol' argv[]
* argc = parsit(line, &argv);
* returns the number of words parsed in argc. argv[argc] will
* be (char *) NULL to indicate end of list, if you're not
* happy with just knowing how many words you have.
* Note that setting argv = (char **) NULL is only done the first
* time the routine is called with a new "argv" -- it tells
* parsit that "argv" is a new array, and parsit shouldn't free
* up the elements (as it would do if it were an old array).
* University of California, Berkeley
* (ARPA: phil@Berkeley.ARPA; UUCP: ...!ucbvax!phil)
extern char *malloc(), *strcpy();
if (argv
!= (char **) NULL
) { /* Check to see if we should */
i
= 0; /* free up the old array */
free(argv
[i
]); /* If so, free each member */
} while (argv
[i
++] != (char *) NULL
);
free((char *) argv
); /* and then free the ptr itself */
while (1) { /* count words in input */
for (; *linecp
== ' ' || *linecp
== '\t'; ++linecp
)
for (; *linecp
!= ' ' && *linecp
!= '\t' && *linecp
!= '\0'; ++linecp
)
/* Then malloc enough for that many words plus 1 (for null) */
if ((argv
= (char **) malloc((num_words
+ 1) * sizeof(char *))) ==
fprintf(stderr
, "parsit: malloc out of space!\n");
while (1) { /* Now build the list of words */
for (; *line
== ' ' || *line
== '\t'; ++line
)
for (; *line
!= ' ' && *line
!= '\t' && *line
!= '\0'; ++line
)
argv
[j
] = malloc(strlen(word
) + 1);
if (argv
[j
] == (char *) NULL
) {
fprintf(stderr
, "parsit: malloc out of space!\n");
(void) strcpy(argv
[j
], word
);
argv
[j
] = (char *) NULL
; /* remember null at end of list */