k9core

Coreutils for *nix operating systems
git clone git://bvnf.space/k9core.git
Log | Files | Refs | LICENSE

commit 35afd689286a9771d63cde57389af4451f821e10
parent 4ab085345f6e25661299f982239a68d010bc3062
Author: aabacchus <ben@bvnf.space>
Date:   Tue, 14 Sep 2021 17:09:02 +0100

standardize return values

Diffstat:
Msrc/cat.c | 13+++++++------
Msrc/chmod.c | 4+++-
Msrc/chroot.c | 2++
Msrc/cp.c | 5++---
Msrc/groups.c | 15++++++++++++++-
Msrc/head.c | 14+++++++-------
Msrc/hostname.c | 7++++++-
Msrc/id.c | 1+
Msrc/kill.c | 2+-
Msrc/logname.c | 1+
Msrc/ls.c | 4++--
Msrc/mv.c | 3++-
Msrc/rmdir.c | 3++-
Msrc/shred.c | 22+++++++++++++++++-----
Msrc/stat.c | 4+++-
Msrc/tee.c | 33+++++++++++++++------------------
Msrc/unlink.c | 3++-
17 files changed, 87 insertions(+), 49 deletions(-)

diff --git a/src/cat.c b/src/cat.c @@ -18,17 +18,17 @@ cat(int fd, const char *filename) if(fd == -1) { fprintf(stderr, "cat: %s: %s\n", filename, strerror(errno)); - return -1; + return 1; } while((c = read(fd, buf, sizeof(buf))) > 0) { if(c == -1) { fprintf(stderr, "cat: %s: %s\n", filename, strerror(errno)); - return -1; + return 1; } if(write(1, buf, c) == -1) { fprintf(stderr, "cat: %s: %s\n", filename, strerror(errno)); - return -1; + return 1; } } close(fd); @@ -37,11 +37,12 @@ cat(int fd, const char *filename) int main(int argc, char *argv[]) { + int return_value; getopt(argc, argv, "u"); if(argc == optind) - cat(0, "-"); + return_value = cat(0, "-"); for(int i = optind; i < argc; i++) - cat(1, argv[i]); + return_value = cat(1, argv[i]); - return 0; + return return_value; } diff --git a/src/chmod.c b/src/chmod.c @@ -10,8 +10,10 @@ main(int argc, char *argv[]) mode_t mode = atoi(argv[1]); for(int i = 2; i < argc; i++) { int fd = chmod(argv[i], mode); - if(fd == -1) + if(fd == -1) { fprintf(stderr, "chmod: %s: %s", argv[i], strerror(errno)); + return 1; + } } return 0; diff --git a/src/chroot.c b/src/chroot.c @@ -16,4 +16,6 @@ main(int argc, char *argv[]) fprintf(stderr, "chroot: %s: %s\n", argv[1], strerror(errno)); return 1; } + /* exec into a shell? */ + return 0; } diff --git a/src/cp.c b/src/cp.c @@ -40,10 +40,9 @@ copy(const char *src, const char *dst) int main(int argc, char *argv[]) { - int fd; if(argc != 3) { fprintf(stderr, "usage: cp source destination\n"); return 1; - } else - fd = copy(argv[1], argv[2]); + } + return copy(argv[1], argv[2]); } diff --git a/src/groups.c b/src/groups.c @@ -1,21 +1,34 @@ +#include <errno.h> #include <grp.h> #include <pwd.h> #include <stdio.h> #include <stdlib.h> +#include <string.h> #include <unistd.h> int main(int argc, char *argv[]) { struct passwd *pwd; + errno = 0; if(argc == 2) pwd = getpwnam(argv[1]); else pwd = getpwuid(getuid()); + if(pwd == NULL) { + fprintf(stderr, + "groups: %s: %s\n", + argv[1], + errno == 0 ? "user not found" : strerror(errno)); + return 1; + } int ngroups = 10; uid_t *groups = malloc(ngroups); - getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups); + if(getgrouplist(pwd->pw_name, pwd->pw_gid, groups, &ngroups) == -1) { + /* the user is a member of more than ngroups groups */ + return 1; + } struct group *gr; for(int i = 0; i < ngroups; i++) { gr = getgrgid(groups[i]); diff --git a/src/head.c b/src/head.c @@ -7,8 +7,7 @@ int head(FILE *file, int lines) { if(file == NULL) { - fprintf( - stderr, "error opening file: %i = %s\n", errno, strerror(errno)); + fprintf(stderr, "head: %s\n", strerror(errno)); return 1; } int a; @@ -29,21 +28,22 @@ int main(int argc, char *argv[]) { int lines = 10; + int return_value = 0; switch(argc) { case 1: - head(stdin, lines); + return_value = head(stdin, lines); break; case 2: - head(fopen(argv[1], "r"), lines); + return_value = head(fopen(argv[1], "r"), lines); break; case 3: lines = abs(atoi(argv[1])); - head(fopen(argv[2], "r"), lines); + return_value = head(fopen(argv[2], "r"), lines); break; default: lines = atoi(argv[2]); - head(fopen(argv[3], "r"), lines); + return_value = head(fopen(argv[3], "r"), lines); break; } - return 0; + return return_value; } diff --git a/src/hostname.c b/src/hostname.c @@ -1,11 +1,16 @@ +#include <errno.h> #include <stdio.h> +#include <string.h> #include <unistd.h> int main(void) { char buf[64]; - gethostname(buf, 64); + if(gethostname(buf, 64) == -1) { + fprintf(stderr, "hostname: %s\n", strerror(errno)); + return 1; + } puts(buf); return 0; } diff --git a/src/id.c b/src/id.c @@ -9,6 +9,7 @@ main(int argc, char *argv[]) { /* TODO: support supplying username as an arg */ int c = getopt(argc, argv, "Ggnru"); + errno = 0; struct passwd *user_data = getpwnam(getlogin()); if(user_data == NULL) { fprintf(stderr, diff --git a/src/kill.c b/src/kill.c @@ -61,7 +61,7 @@ main(int argc, char *argv[]) return 1; } if(kill(pid, sig) == -1) { - fprintf(stderr, "kill: %i: %s\n", sig, strerror(errno)); + fprintf(stderr, "kill: %i: %s\n", pid, strerror(errno)); return 1; } return 0; diff --git a/src/logname.c b/src/logname.c @@ -5,4 +5,5 @@ int main(void) { printf("%s\n", getlogin()); + return 0; } diff --git a/src/ls.c b/src/ls.c @@ -13,7 +13,7 @@ recursive_list_dirs(const char *directory) DIR *dir = opendir(directory); if(dir == NULL) { fprintf(stderr, "ls: %s: %s\n", directory, strerror(errno)); - return -1; + return 1; } struct dirent *ent; @@ -113,7 +113,7 @@ main(int argc, char *argv[]) "ls: %s: %s\n", ent->d_name, strerror(errno)); - return -1; + return 1; } if((st.st_mode & S_IEXEC) != 0) suffix = '*'; diff --git a/src/mv.c b/src/mv.c @@ -32,11 +32,12 @@ move(const char *src, const char *dst) int main(int argc, char *argv[]) { - if(argc == 1) { + if(argc != 3) { fprintf(stderr, "usage: mv source destination\n"); return 1; } else if(rename(argv[1], argv[2]) == -1) { fprintf(stderr, "mv: %s\n", strerror(errno)); return 1; } + return 0; } diff --git a/src/rmdir.c b/src/rmdir.c @@ -8,8 +8,9 @@ main(int argc, char *argv[]) { for(int i = 1; i < argc; i++) { - if(rmdir(argv[i])) { + if(rmdir(argv[i]) == -1) { fprintf(stderr, "rmdir: %s: %s\n", argv[i], strerror(errno)); + return 1; } } return 0; diff --git a/src/shred.c b/src/shred.c @@ -12,17 +12,23 @@ fill_with_zeroes(const char *filename) { int fd = open(filename, O_WRONLY); if(fd == -1) { - printf("Error reading file: %i = %s\n", errno, strerror(errno)); + fprintf(stderr, "shred: %s: %s\n", filename, strerror(errno)); return 1; } struct stat stat_struct; /* What name should i use? */ - stat(filename, &stat_struct); + if(stat(filename, &stat_struct) == -1) { + fprintf(stderr, "shred: %s: %s\n", filename, strerror(errno)); + return 1; + } long int bytes_to_write = stat_struct.st_size; char *buf = NULL; buf = malloc(bytes_to_write); for(int i = 0; i < bytes_to_write; i++) - write(fd, "\0\0\0\0\0", bytes_to_write + 2048); + if(write(fd, "\0\0\0\0\0", bytes_to_write + 2048) == -1) { + fprintf(stderr, "shred: %s: %s\n", filename, strerror(errno)); + return 1; + } free(buf); return 0; } @@ -32,7 +38,13 @@ main(int argc, char *argv[]) { int c = getopt(argc, argv, "u"); /* TODO: add -f */ - fill_with_zeroes(argv[1]); + if(fill_with_zeroes(argv[1]) != 0) { + return 1; + } if(c == 'u') - remove(argv[1]); + if(remove(argv[1]) == -1) { + fprintf(stderr, "shred: %s: %s\n", argv[1], strerror(errno)); + return 1; + } + return 0; } diff --git a/src/stat.c b/src/stat.c @@ -7,6 +7,7 @@ int main(int argc, char **argv) { + int return_value = 0; if(argc == 1) { fprintf(stderr, "usage: stat file...\n"); return 1; @@ -16,6 +17,7 @@ main(int argc, char **argv) for(int i = 1; i < argc; i++) { if(stat(argv[i], &file_data) == -1) { fprintf(stderr, "stat: %s: %s\n", argv[i], strerror(errno)); + return_value = 1; continue; } /* About file size, location... */ @@ -56,5 +58,5 @@ main(int argc, char **argv) creat_date); } - return 0; + return return_value; } diff --git a/src/tee.c b/src/tee.c @@ -8,10 +8,6 @@ int tee(int fd) { - if(fd == -1) { - fprintf(stderr, "tee: %s\n", strerror(errno)); - return 1; - } char buf[8192]; int read_bytes = 0; while((read_bytes = read(0, buf, 8192)) > 0) { @@ -38,7 +34,8 @@ main(int argc, char *argv[]) int c; int append = 0; int ignore_signt = 0; - int fd = 0; + int fd; + int return_value = 0; int FLAGS = O_WRONLY | O_CREAT; /* yeah, it will overwrite the thing if it * can't read what's in the file, thanks * POSIX! */ @@ -52,21 +49,21 @@ main(int argc, char *argv[]) break; } } - if(argc == optind) { - if(ignore_signt) - signal(SIGINT, SIG_IGN); - tee(1); + if(argc == optind || argv[optind][0] == '-') { + fd = 1; } else { - if(argv[argc - 1][0] == '-') - fd = 1; if(append) FLAGS = O_RDWR | O_APPEND; /* Remember what I said? */ - - fd = open(argv[argc - 1], FLAGS); - if(ignore_signt) - signal(SIGINT, SIG_IGN); - if(tee(fd) == 1) - return -1; + fd = open(argv[optind], FLAGS); + if(fd == -1) { + fprintf(stderr, "tee: %s: %s\n", argv[optind], strerror(errno)); + return 1; + } } - return 0; + if(ignore_signt) + signal(SIGINT, SIG_IGN); + + return_value = tee(fd); + close(fd); + return return_value; } diff --git a/src/unlink.c b/src/unlink.c @@ -6,7 +6,7 @@ int main(int argc, char *argv[]) { - if(argc == 1) { + if(argc != 2) { fprintf(stderr, "usage: unlink file\n"); return 1; } @@ -14,6 +14,7 @@ main(int argc, char *argv[]) int fd = unlink(argv[1]); if(fd == -1) { fprintf(stderr, "unlink: %s: %s\n", argv[1], strerror(errno)); + return 1; } return 0; }