aboutsummaryrefslogtreecommitdiffstats
path: root/loc.c
diff options
context:
space:
mode:
Diffstat (limited to 'loc.c')
-rw-r--r--loc.c21
1 files changed, 14 insertions, 7 deletions
diff --git a/loc.c b/loc.c
index 1f309c8..072a6a3 100644
--- a/loc.c
+++ b/loc.c
@@ -1,8 +1,7 @@
/* See LICENSE file for copyright and license details. */
-#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
-#include <limits.h>
+#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -43,6 +42,8 @@ count(int fd, const char *fname)
if (n <= 0) {
if (!n)
break;
+ if (errno == EINTR)
+ continue;
fprintf(stderr, "%s: %s: %s\n", argv0, fname, strerror(errno));
return -1;
}
@@ -120,13 +121,14 @@ count(int fd, const char *fname)
static int
strwidth(const char *str)
{
- size_t n = strlen(str) + 1;
+ size_t wn, n = strlen(str) + 1;
wchar_t *wcs = calloc(n, sizeof(*wcs));
int r = -1;
if (!wcs)
fprintf(stderr, "%s: out of memory\n", argv0), exit(1);
- if (mbstowcs(wcs, str, n) != (size_t)-1)
- r = wcswidth(wcs, n);
+ wn = mbstowcs(wcs, str, n);
+ if (wn != (size_t)-1)
+ r = wcswidth(wcs, wn);
free(wcs);
return r < 0 ? (int)(n - 1) : r;
}
@@ -154,13 +156,17 @@ main(int argc, char *argv[])
usage();
} ARGEND;
+ setlocale(LC_ALL, "");
+
if (argc < 2) {
if (!argc || !strcmp(argv[0], "-")) {
n = count(STDIN_FILENO, "<stdin>");
} else {
fd = open(argv[0], O_RDONLY);
- if (fd < 0)
+ if (fd < 0) {
+ fprintf(stderr, "%s: %s: %s\n", argv0, argv[0], strerror(errno));
return 1;
+ }
n = count(fd, argv[0]);
close(fd);
}
@@ -175,6 +181,7 @@ main(int argc, char *argv[])
if (!strcmp(argv[i], "-")) {
res[i].n = count(STDIN_FILENO, "<stdin>");
} else if ((fd = open(argv[i], O_RDONLY)) < 0) {
+ fprintf(stderr, "%s: %s: %s\n", argv0, argv[i], strerror(errno));
res[i].n = -1;
} else {
res[i].n = count(fd, argv[i]);
@@ -202,7 +209,7 @@ main(int argc, char *argv[])
printf("%zi\n", total);
}
- if (fflush(stdin) || ferror(stdin) || fclose(stdin))
+ if (fflush(stdout) || ferror(stdout) || fclose(stdout))
fprintf(stderr, "%s: <stdout>: %s\n", argv0, strerror(errno)), exit(1);
return ret;
}