diff options
| author | Mattias Andrée <m@maandree.se> | 2026-05-23 20:58:20 +0200 |
|---|---|---|
| committer | Mattias Andrée <m@maandree.se> | 2026-05-23 20:58:20 +0200 |
| commit | d367031437610c3adc4d70a4b3412c6d40a41464 (patch) | |
| tree | c431ee0d1b721f329fac5e06d21de1e38ffc3acc | |
| parent | m fixes (diff) | |
| download | loc-d367031437610c3adc4d70a4b3412c6d40a41464.tar.gz loc-d367031437610c3adc4d70a4b3412c6d40a41464.tar.bz2 loc-d367031437610c3adc4d70a4b3412c6d40a41464.tar.xz | |
Signed-off-by: Mattias Andrée <m@maandree.se>
| -rw-r--r-- | LICENSE | 2 | ||||
| -rw-r--r-- | README | 2 | ||||
| -rw-r--r-- | loc.1 | 3 | ||||
| -rw-r--r-- | loc.c | 21 |
4 files changed, 18 insertions, 10 deletions
@@ -1,6 +1,6 @@ ISC License -© 2018 Mattias Andrée <m@maandree.se> +© 2018, 2026 Mattias Andrée <m@maandree.se> Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above @@ -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", <total #lines of code> If exactly one or no files are specified, this will be the only line output. @@ -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 @@ -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; } |
