| 1 |
greg |
1.1 |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint |
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
| 5 |
|
|
#endif |
| 6 |
|
|
|
| 7 |
|
|
/* |
| 8 |
|
|
* Read white space separated words from stream |
| 9 |
|
|
*/ |
| 10 |
|
|
|
| 11 |
|
|
#include <stdio.h> |
| 12 |
|
|
|
| 13 |
|
|
#include <ctype.h> |
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
char * |
| 17 |
|
|
fgetword(s, n, fp) /* get a word, maximum n-1 characters */ |
| 18 |
|
|
char *s; |
| 19 |
|
|
int n; |
| 20 |
|
|
register FILE *fp; |
| 21 |
|
|
{ |
| 22 |
|
|
register char *cp; |
| 23 |
|
|
register int c; |
| 24 |
|
|
/* skip initial white space */ |
| 25 |
|
|
do |
| 26 |
|
|
c = getc(fp); |
| 27 |
|
|
while (isspace(c)); |
| 28 |
|
|
/* check for end of file */ |
| 29 |
|
|
if (c == EOF) |
| 30 |
|
|
return(NULL); |
| 31 |
|
|
/* get actual word */ |
| 32 |
|
|
cp = s; |
| 33 |
|
|
do { |
| 34 |
|
|
if (--n <= 0) /* check length limit */ |
| 35 |
|
|
break; |
| 36 |
|
|
*cp++ = c; |
| 37 |
|
|
c = getc(fp); |
| 38 |
|
|
} while (c != EOF && !isspace(c)); |
| 39 |
|
|
*cp = '\0'; |
| 40 |
|
|
if (c != EOF) /* replace delimiter */ |
| 41 |
|
|
ungetc(c, fp); |
| 42 |
|
|
return(s); |
| 43 |
|
|
} |