sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git
Log | Files | Refs | README | LICENSE

fgetrune.c (576B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #include "../utf.h"
      8 
      9 int
     10 fgetrune(Rune *r, FILE *fp)
     11 {
     12 	char buf[UTFmax];
     13 	int  i = 0, c;
     14 
     15 	while (i < UTFmax && (c = fgetc(fp)) != EOF) {
     16 		buf[i++] = c;
     17 		if (charntorune(r, buf, i) > 0)
     18 			break;
     19 	}
     20 	if (ferror(fp))
     21 		return -1;
     22 
     23 	return i;
     24 }
     25 
     26 int
     27 efgetrune(Rune *r, FILE *fp, const char *file)
     28 {
     29 	int ret;
     30 
     31 	if ((ret = fgetrune(r, fp)) < 0) {
     32 		fprintf(stderr, "fgetrune %s: %s\n", file, strerror(errno));
     33 		exit(1);
     34 	}
     35 	return ret;
     36 }