| 1 |
greg |
1.1 |
#ifndef lint |
| 2 |
schorsch |
2.5 |
static const char RCSid[] = "$Id: fgetword.c,v 2.4 2003/06/27 06:53:21 greg Exp $"; |
| 3 |
greg |
1.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Read white space separated words from stream |
| 6 |
greg |
2.2 |
* |
| 7 |
greg |
2.4 |
* External symbols declared in rtio.h |
| 8 |
greg |
2.2 |
*/ |
| 9 |
|
|
|
| 10 |
greg |
2.3 |
#include "copyright.h" |
| 11 |
greg |
1.1 |
|
| 12 |
greg |
2.4 |
#include "rtio.h" |
| 13 |
greg |
1.1 |
|
| 14 |
|
|
#include <ctype.h> |
| 15 |
|
|
|
| 16 |
|
|
|
| 17 |
|
|
char * |
| 18 |
greg |
2.2 |
fgetword(s, n, fp) /* get (quoted) word up to n-1 characters */ |
| 19 |
greg |
1.1 |
char *s; |
| 20 |
|
|
int n; |
| 21 |
|
|
register FILE *fp; |
| 22 |
|
|
{ |
| 23 |
greg |
2.2 |
int quote = '\0'; |
| 24 |
greg |
1.1 |
register char *cp; |
| 25 |
|
|
register int c; |
| 26 |
|
|
/* skip initial white space */ |
| 27 |
|
|
do |
| 28 |
|
|
c = getc(fp); |
| 29 |
|
|
while (isspace(c)); |
| 30 |
greg |
2.2 |
/* check for quote */ |
| 31 |
schorsch |
2.5 |
if ((c == '"') | (c == '\'')) { |
| 32 |
greg |
2.2 |
quote = c; |
| 33 |
|
|
c = getc(fp); |
| 34 |
|
|
} |
| 35 |
greg |
1.1 |
/* check for end of file */ |
| 36 |
|
|
if (c == EOF) |
| 37 |
|
|
return(NULL); |
| 38 |
|
|
/* get actual word */ |
| 39 |
|
|
cp = s; |
| 40 |
|
|
do { |
| 41 |
greg |
2.2 |
if (--n <= 0) /* check length limit */ |
| 42 |
greg |
1.1 |
break; |
| 43 |
|
|
*cp++ = c; |
| 44 |
|
|
c = getc(fp); |
| 45 |
greg |
2.2 |
} while (c != EOF && !(quote ? c==quote : isspace(c))); |
| 46 |
greg |
1.1 |
*cp = '\0'; |
| 47 |
schorsch |
2.5 |
if ((c != EOF) & (!quote)) /* replace space */ |
| 48 |
greg |
1.1 |
ungetc(c, fp); |
| 49 |
|
|
return(s); |
| 50 |
|
|
} |