From d367031437610c3adc4d70a4b3412c6d40a41464 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 23 May 2026 20:58:20 +0200 Subject: Fix mistakes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- LICENSE | 2 +- README | 2 +- loc.1 | 3 ++- loc.c | 21 ++++++++++++++------- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/LICENSE b/LICENSE index 5b13d47..884dfaa 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ ISC License -© 2018 Mattias Andrée +© 2018, 2026 Mattias Andrée Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above diff --git a/README b/README index a645897..ec3deb6 100644 --- a/README +++ b/README @@ -19,7 +19,7 @@ DESCRIPTION The total for all processed files is printed on the last line with the format - "%zu\n", <#total lines of code> + "%zu\n", If exactly one or no files are specified, this will be the only line output. diff --git a/loc.1 b/loc.1 index af23da4..8118146 100644 --- a/loc.1 +++ b/loc.1 @@ -3,6 +3,7 @@ loc - count number of lines of code .SH SYNOPSIS .B loc +[-s] .RI [ file "] ..." .SH DESCRIPTION .B loc @@ -28,7 +29,7 @@ The total for all processed files is printed on the last line with the format .nf - \fB"%zu\n"\fP, <\fI#total lines of code\fP> + \fB"%zu\n"\fP, <\fItotal #lines of code\fP> .fi .PP If exactly one or no files are specified, this will be the 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 #include #include -#include +#include #include #include #include @@ -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, ""); } 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, ""); } 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: : %s\n", argv0, strerror(errno)), exit(1); return ret; } -- cgit v1.3.1