| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: fputword.c,v 3.11 2024/08/03 15:32:59 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Write word to stream, quoting as necessary
|
| 6 |
*
|
| 7 |
* External symbols declared in rtio.h
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include "copyright.h"
|
| 11 |
|
| 12 |
#include <ctype.h>
|
| 13 |
|
| 14 |
#include "rtio.h"
|
| 15 |
|
| 16 |
/* put (quoted) word to file stream */
|
| 17 |
void
|
| 18 |
fputword(char *s, FILE *fp)
|
| 19 |
{
|
| 20 |
int hasspace = 0;
|
| 21 |
int quote = 0;
|
| 22 |
char *cp;
|
| 23 |
/* check if quoting needed */
|
| 24 |
for (cp = s; *cp; cp++)
|
| 25 |
if (isspace(*cp))
|
| 26 |
hasspace++;
|
| 27 |
else if ((cp > s) & (*cp == '"') && cp[1])
|
| 28 |
quote = '\'';
|
| 29 |
else if ((cp > s) & (*cp == '\'') && cp[1])
|
| 30 |
quote = '"';
|
| 31 |
|
| 32 |
if (!*s | hasspace | quote) { /* output with quotes */
|
| 33 |
if (!quote) quote = '"';
|
| 34 |
fputc(quote, fp);
|
| 35 |
fputs(s, fp);
|
| 36 |
fputc(quote, fp);
|
| 37 |
} else /* output sans quotes */
|
| 38 |
fputs(s, fp);
|
| 39 |
}
|