FreeBSD/usr.bin/sed/main.c
/*
 * Like fgets, but go through the chain of compilation units chaining them
 * together.  Empty strings and files are ignored.
 */
Like fgets, but go through the chain of compilation units chaining them
together.  Empty strings and files are ignored.
Like fgets but go through the chain of compilation units chaining them together . Empty strings and files are ignored .
char *
cu_fgets(char *buf, int n, int *more)
{
	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
	static FILE *f;		
	static char *s;		
	static char string_ident[30];
	char *p;
again:
	switch (state) {
	case ST_EOF:
		if (script == NULL) {
			if (more != NULL)
				*more = 0;
			return (NULL);
		}
		linenum = 0;
		switch (script->type) {
		case CU_FILE:
			if ((f = fopen(script->s, "r")) == NULL)
				err(1, "%s", script->s);
			fname = script->s;
			state = ST_FILE;
			goto again;
		case CU_STRING:
			if (((size_t)snprintf(string_ident,
			    sizeof(string_ident), "\"%s\"", script->s)) >=
			    sizeof(string_ident) - 1)
				(void)strcpy(string_ident +
				    sizeof(string_ident) - 6, " ...\"");
			fname = string_ident;
			s = script->s;
			state = ST_STRING;
			goto again;
		}
	case ST_FILE:
		if ((p = fgets(buf, n, f)) != NULL) {
			linenum++;
			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
				nflag = 1;
			if (more != NULL)
				*more = !feof(f);
			return (p);
		}
		script = script->next;
		(void)fclose(f);
		state = ST_EOF;
		goto again;
	case ST_STRING:
		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
			nflag = 1;
		p = buf;
		for (;;) {
			if (n-- <= 1) {
				*p = '\0';
				linenum++;
				if (more != NULL)
					*more = 1;
				return (buf);
			}
			switch (*s) {
			case '\0':
				state = ST_EOF;
				if (s == script->s) {
					script = script->next;
					goto again;
				} else {
					script = script->next;
					*p = '\0';
					linenum++;
					if (more != NULL)
						*more = 0;
					return (buf);
				}
			case '\n':
				*p++ = '\n';
				*p = '\0';
				s++;
				linenum++;
				if (more != NULL)
					*more = 0;
				return (buf);
			default:
				*p++ = *s++;
			}
		}
	}
	return (NULL);
}
char * cu_fgets ( char * buf , int n , int * more ) { static enum { ST_EOF , ST_FILE , ST_STRING } state = ST_EOF ; static FILE * f ; static char * s ; static char string_ident [ 30 ] ; char * p ; again : switch ( state ) { case ST_EOF : if ( script == NULL ) { if ( more != NULL ) * more = 0 ; return ( NULL ) ; } linenum = 0 ; switch ( script -> type ) { case CU_FILE : if ( ( f = fopen ( script -> s , " " ) ) == NULL ) err ( 1 , " " , script -> s ) ; fname = script -> s ; state = ST_FILE ; goto again ; case CU_STRING : if ( ( ( size_t ) snprintf ( string_ident , sizeof ( string_ident ) , " \" \" " , script -> s ) ) >= sizeof ( string_ident ) - 1 ) ( void ) strcpy ( string_ident + sizeof ( string_ident ) - 6 , " \" " ) ; fname = string_ident ; s = script -> s ; state = ST_STRING ; goto again ; } case ST_FILE : if ( ( p = fgets ( buf , n , f ) ) != NULL ) { linenum ++ ; if ( linenum == 1 && buf [ 0 ] == ' ' && buf [ 1 ] == ' ' ) nflag = 1 ; if ( more != NULL ) * more = ! feof ( f ) ; return ( p ) ; } script = script -> next ; ( void ) fclose ( f ) ; state = ST_EOF ; goto again ; case ST_STRING : if ( linenum == 0 && s [ 0 ] == ' ' && s [ 1 ] == ' ' ) nflag = 1 ; p = buf ; for ( ; ; ) { if ( n -- <= 1 ) { * p = ' \0 ' ; linenum ++ ; if ( more != NULL ) * more = 1 ; return ( buf ) ; } switch ( * s ) { case ' \0 ' : state = ST_EOF ; if ( s == script -> s ) { script = script -> next ; goto again ; } else { script = script -> next ; * p = ' \0 ' ; linenum ++ ; if ( more != NULL ) * more = 0 ; return ( buf ) ; } case ' \n ' : * p ++ = ' \n ' ; * p = ' \0 ' ; s ++ ; linenum ++ ; if ( more != NULL ) * more = 0 ; return ( buf ) ; default : * p ++ = * s ++ ; } } } return ( NULL ) ; }
Like fgets, but go through the chain of compilation units chaining them
 together.
Like fgets but go through the chain of compilation units chaining them together .
/* Current open file */ /* Current pointer inside string */ /* NOTREACHED */