diff options
author | Mattias Andrée <maandree@kth.se> | 2018-02-20 21:14:09 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2018-02-20 21:14:09 +0100 |
commit | 88a3f68cf80a9323a9556ff1c05eb75bdefd4b15 (patch) | |
tree | f0c897391f8d166ef8f8655a56806f7a1ef2aeef | |
parent | Merge pull request #1 from pranomostro/master (diff) | |
download | loc-88a3f68cf80a9323a9556ff1c05eb75bdefd4b15.tar.gz loc-88a3f68cf80a9323a9556ff1c05eb75bdefd4b15.tar.bz2 loc-88a3f68cf80a9323a9556ff1c05eb75bdefd4b15.tar.xz |
Treat all files as regular files
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r-- | loc.c | 55 |
1 files changed, 5 insertions, 50 deletions
@@ -137,55 +137,12 @@ zuwidth(size_t num) return sprintf(buf, "%zu", num); } -static int -xopen(const char *path, int flags, int *closep) -{ - int fd; - const char *num; - unsigned long int tmp; - - *closep = 0; - - if (!strcmp(path, "/dev/stdin")) - return STDIN_FILENO; - if (!strcmp(path, "/dev/stdout")) - return STDOUT_FILENO; - if (!strcmp(path, "/dev/stderr")) - return STDERR_FILENO; - if (strncmp(path, "/dev/fd/", sizeof("/dev/fd/") - 1)) { - num = &path[sizeof("/dev/fd/") - 1]; - goto inherited; - } - if (strncmp(path, "/proc/self/fd/", sizeof("/proc/self/fd/") - 1)) { - num = &path[sizeof("/proc/self/fd/") - 1]; - goto inherited; - } - -normal: - fd = open(path, flags); - if (fd < 0) { - fprintf(stderr, "%s: %s: %s\n", argv0, path, strerror(errno)); - return -1; - } - *closep = 1; - return fd; - -inherited: - if (!isdigit(*num)) - goto normal; - errno = 0; - tmp = strtoul(num, (void *)&num, 10); - if (errno || *num || tmp > (unsigned long int)INT_MAX) - goto normal; - return (int)tmp; -} - int main(int argc, char *argv[]) { ssize_t n; struct result *res; - int i, fd, doclose, maxleft = 0, maxright = 0, maxwidth, left, right, ret = 0; + int i, fd, maxleft = 0, maxright = 0, maxwidth, left, right, ret = 0; ARGBEGIN { default: @@ -196,12 +153,11 @@ main(int argc, char *argv[]) if (!argc || !strcmp(argv[0], "-")) { n = count(STDIN_FILENO, "<stdin>"); } else { - fd = xopen(argv[0], O_RDONLY, &doclose); + fd = open(argv[0], O_RDONLY); if (fd < 0) return 1; n = count(fd, argv[0]); - if (doclose) - close(fd); + close(fd); } printf("%zi\n", n); } else { @@ -210,12 +166,11 @@ main(int argc, char *argv[]) for (i = 0; i < argc; i++) { if (!strcmp(argv[i], "-")) { res[i].n = count(STDIN_FILENO, "<stdin>"); - } else if ((fd = xopen(argv[i], O_RDONLY, &doclose)) < 0) { + } else if ((fd = open(argv[i], O_RDONLY)) < 0) { res[i].n = -1; } else { res[i].n = count(fd, argv[i]); - if (doclose) - close(fd); + close(fd); } if (res[i].n >= 0) { left = strwidth(argv[i]); |