master xplshn/aruu / shared / libutf / fgetrune.c
 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
 9int
10fgetrune(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
26int
27efgetrune(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}