shithub: riscv

ref: f0f2d452c873255dec4b3359b3f93e349be0f2c5
dir: /sys/src/libstdio/fgets.c/

View raw version
/*
 * pANS stdio -- fgets
 */
#include "iolib.h"
char *fgets(char *as, int n, FILE *f){
	int c;
	char *s=as;
	c = EOF;
	while(n>1 && (c=getc(f))!=EOF){
		*s++=c;
		--n;
		if(c=='\n') break;
	}
	if(c==EOF && s==as
	|| ferror(f)) return NULL;
	if(n) *s='\0';
	return as;
}