xref: /original-bsd/usr.bin/uucp/libuu/cfgets.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)cfgets.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 /*
13  * get nonblank, non-comment, (possibly continued) line. Alan S. Watt
14  */
15 
16 #include <stdio.h>
17 #define COMMENT		'#'
18 #define CONTINUE	'\\'
19 #define EOLN		'\n'
20 #define EOS		'\0'
21 
22 /*LINTLIBRARY*/
23 
24 char *
25 cfgets(buf, siz, fil)
26 register char *buf;
27 int siz;
28 FILE *fil;
29 {
30 	register char *s;
31 	register i, c, len;
32 	char *fgets();
33 
34 	for (i=0,s=buf; i = (fgets(s, siz-i, fil) != NULL); i = s - buf) {
35 
36 		/* get last character of line */
37 		c = s[len = (strlen(s) - 1)];
38 
39 		/* skip comments; make sure end of comment line seen */
40 		if (*s == COMMENT) {
41 			while (c != EOLN && c != EOF)
42 				c = getc(fil);
43 			*s = EOS;
44 		}
45 
46 		/* skip blank lines */
47 		else if (*s != EOLN) {
48 			s += len;
49 
50 			/* continue lines ending with CONTINUE */
51 			if (c != EOLN || *--s != CONTINUE)
52 				break;
53 		}
54 	}
55 
56 	return i ? buf : NULL;
57 }
58 
59 #ifdef TEST
60 main()
61 {
62 	char buf[512];
63 
64 	while (cfgets(buf, sizeof buf, stdin))
65 		fputs(buf, stdout);
66 }
67 #endif TEST
68