commit ff33d71a05b2c76f618e031917dd719e43c64ff9
parent b601d2328b5807e516089fd6bda542774108319a
Author: aabacchus <ben@bvnf.space>
Date: Mon, 13 Sep 2021 19:18:06 +0100
standardize error messages
Diffstat:
26 files changed, 173 insertions(+), 90 deletions(-)
diff --git a/src/cat.c b/src/cat.c
@@ -17,14 +17,19 @@ cat(int fd, const char *filename)
fd = open(filename, O_RDONLY);
if(fd == -1) {
- fprintf(stderr, "error opening %s: %s\n", filename, strerror(errno));
+ fprintf(stderr, "cat: %s: %s\n", filename, strerror(errno));
return -1;
}
while((c = read(fd, buf, sizeof(buf))) > 0) {
- if(c == -1)
+ if(c == -1) {
+ fprintf(stderr, "cat: %s: %s\n", filename, strerror(errno));
return -1;
- write(1, buf, c);
+ }
+ if(write(1, buf, c) == -1) {
+ fprintf(stderr, "cat: %s: %s\n", filename, strerror(errno));
+ return -1;
+ }
}
close(fd);
return 0;
diff --git a/src/chgrp.c b/src/chgrp.c
@@ -24,10 +24,14 @@ main(int argc, char *argv[])
gid_t gid = group_data->gr_gid;
for(int i = optind + 1; i < argc; i++) {
if(follow_symlink) {
- if(lchown(argv[i], gid, getuid()) == -1)
- fprintf(stderr, "Error: %i = %s\n", errno, strerror(errno));
+ if(lchown(argv[i], gid, getuid()) == -1) {
+ fprintf(stderr, "chgrp: %s: %s\n", argv[i], strerror(errno));
+ return 1;
+ }
} else if(chown(argv[i], gid, getuid()) == -1) {
- fprintf(stderr, "Error: %i = %s\n", errno, strerror(errno));
+ fprintf(stderr, "chgrp: %s: %s\n", argv[i], strerror(errno));
+ return 1;
}
}
+ return 0;
}
diff --git a/src/chmod.c b/src/chmod.c
@@ -10,12 +10,8 @@ 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) fprintf(stderr,
- "Error setting %i on %s\n %i = %s",
- mode,
- argv[i],
- errno,
- strerror(errno));
+ if(fd == -1)
+ fprintf(stderr, "chmod: %s: %s", argv[i], strerror(errno));
}
return 0;
diff --git a/src/chown.c b/src/chown.c
@@ -1,5 +1,7 @@
+#include <errno.h>
#include <pwd.h>
#include <stdio.h>
+#include <string.h>
#include <sys/types.h>
#include <unistd.h>
@@ -9,17 +11,28 @@ int
main(int argc, char *argv[])
{
if(argc == 1) {
- fprintf(stderr, "No command given\n");
+ fprintf(stderr, "usage: chown owner file...\n");
return 1;
}
const char *user = argv[1];
+ errno = 0;
struct passwd *data = getpwnam(user);
+ if(data == NULL) {
+ fprintf(stderr,
+ "chown: %s: %s\n",
+ user,
+ errno == 0 ? "user not found" : strerror(errno));
+ return 1;
+ }
uid_t uid = data->pw_gid;
gid_t gid = data->pw_gid; /* Change this with the strtok() thing */
for(int i = 2; i <= argc; i++) {
- chown(argv[i], uid, gid);
+ if(chown(argv[i], uid, gid) == -1) {
+ fprintf(stderr, "chown: %s: %s\n", argv[i], strerror(errno));
+ return 1;
+ }
}
return 0;
diff --git a/src/chroot.c b/src/chroot.c
@@ -1,4 +1,6 @@
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
/* UNTESTED */
@@ -6,9 +8,12 @@
int
main(int argc, char *argv[])
{
- if(argc == 1) {
- fprintf(stderr, "Missing operand\n");
+ if(argc != 2) {
+ fprintf(stderr, "usage: chroot newroot\n");
+ return 1;
+ }
+ if(chroot(argv[1]) == -1) {
+ fprintf(stderr, "chroot: %s: %s\n", argv[1], strerror(errno));
return 1;
}
- chroot(argv[1]);
}
diff --git a/src/cp.c b/src/cp.c
@@ -8,26 +8,30 @@ int
copy(const char *src, const char *dst)
{
int source = open(src, O_RDONLY);
- int destination = creat(dst, 0644);
-
- if(destination == -1) {
- fprintf(stderr,
- "Error opening destination file: %i = %s\n",
- errno,
- strerror(errno));
+ if(source == -1) {
+ fprintf(stderr, "cp: %s: %s\n", src, strerror(errno));
return 1;
}
- if(source == -1) {
- fprintf(stderr,
- "Error opening source file: %i = %s\n",
- errno,
- strerror(errno));
+
+ int destination = creat(dst, 0644);
+ if(destination == -1) {
+ fprintf(stderr, "cp: %s: %s\n", dst, strerror(errno));
return 1;
}
+
int lines;
char buf[8912];
- while((lines = read(source, buf, sizeof(buf))) > 0)
- write(destination, buf, lines);
+ while((lines = read(source, buf, sizeof(buf))) > 0) {
+ if(lines == -1) {
+ fprintf(stderr, "cp: %s: %s\n", src, strerror(errno));
+ return 1;
+ }
+ if(write(destination, buf, lines) == -1) {
+ fprintf(stderr, "cp: %s: %s\n", dst, strerror(errno));
+ return 1;
+ }
+ }
+
close(destination);
close(source);
return 0;
@@ -37,7 +41,7 @@ int
main(int argc, char *argv[])
{
int fd;
- if(argc == 1) {
+ if(argc != 3) {
fprintf(stderr, "usage: cp source destination\n");
return 1;
} else
diff --git a/src/date.c b/src/date.c
@@ -1,3 +1,4 @@
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -28,6 +29,11 @@ main(int argc, char *argv[])
if(u)
timeinfo = gmtime(&now);
+ if(timeinfo == NULL) {
+ fprintf(stderr, "date: %s\n", strerror(errno));
+ return 1;
+ }
+
if(argc > optind && argv[optind][0] == '+') {
argv[optind]++;
strcpy(FORMAT, argv[optind]);
diff --git a/src/du.c b/src/du.c
@@ -1,5 +1,7 @@
+#include <errno.h>
#include <getopt.h>
#include <stdio.h>
+#include <string.h>
#include <sys/stat.h>
int
@@ -17,13 +19,16 @@ main(int argc, char *argv[])
}
}
if(argc == optind) {
- printf("no!\n");
+ printf("usage: du [-h] file...\n");
return 1;
}
for(int i = optind; i < argc; i++) {
- stat(argv[i], &file_data);
+ if(stat(argv[i], &file_data) == -1) {
+ fprintf(stderr, "du: %s: %s\n", argv[i], strerror(errno));
+ continue;
+ }
if(human_readable)
- printf("%li\t %s", file_data.st_size * 1024, argv[i]);
+ printf("%liK\t %s", file_data.st_size / 1024, argv[i]);
else
printf("%li\t %s", file_data.st_size, argv[i]);
puts("");
diff --git a/src/exec.c b/src/exec.c
@@ -1,14 +1,19 @@
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if(argc == 1) {
- fprintf(stderr, "what do you want to do?\n");
+ fprintf(stderr, "usage: exec command [args]\n");
return 1;
}
char *const argv2[1] = { argv[2] };
- execv(argv[1], argv2);
+ if(execv(argv[1], argv2) == -1) {
+ fprintf(stderr, "exec: %s: %s\n", argv[1], strerror(errno));
+ return 1;
+ }
return 0;
}
diff --git a/src/id.c b/src/id.c
@@ -1,12 +1,22 @@
+#include <errno.h>
#include <getopt.h>
#include <pwd.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
+ /* TODO: support supplying username as an arg */
int c = getopt(argc, argv, "Ggnru");
struct passwd *user_data = getpwnam(getlogin());
+ if(user_data == NULL) {
+ fprintf(stderr,
+ "id: %s: %s\n",
+ getlogin(),
+ errno == 0 ? "user not found" : strerror(errno));
+ return 1;
+ }
switch(c) {
case 'g':
case 'u':
diff --git a/src/kill.c b/src/kill.c
@@ -1,6 +1,8 @@
+#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
void
list_signals(void)
@@ -38,7 +40,7 @@ main(int argc, char *argv[])
int sig = 0;
pid_t pid;
if(argc == 1) {
- fprintf(stderr, "expected something\n");
+ fprintf(stderr, "usage: kill [signal_number] pid\n");
return 1;
}
if(argv[1][1] == 'l') {
@@ -55,9 +57,12 @@ main(int argc, char *argv[])
pid = abs(atoi(argv[2]));
break;
default:
- fprintf(stderr, "Specify who to kill\n");
+ fprintf(stderr, "usage: kill [signal_number] pid\n");
return 1;
}
- kill(pid, sig);
+ if(kill(pid, sig) == -1) {
+ fprintf(stderr, "kill: %i: %s\n", sig, strerror(errno));
+ return 1;
+ }
return 0;
}
diff --git a/src/ln.c b/src/ln.c
@@ -8,7 +8,7 @@ main(int argc, char *argv[])
{
if(argc == 1) {
- printf("Usage: ln oldfile newfile\n");
+ printf("usage: ln [-s] destination link\n");
return 1;
}
int opts, fd, fflag;
@@ -23,10 +23,7 @@ main(int argc, char *argv[])
}
int symstat = symlink(argv[2], argv[3]);
if(symstat == -1) {
- fprintf(stderr,
- "Symlink error: %i = %s",
- errno,
- strerror(errno));
+ fprintf(stderr, "ln: %s: %s", argv[3], strerror(errno));
return 1;
}
break;
@@ -36,10 +33,7 @@ main(int argc, char *argv[])
default:
fd = link(argv[1], argv[2]);
if(fd == -1) {
- fprintf(stderr,
- "Error creating link: %i = %s\n",
- errno,
- strerror(errno));
+ fprintf(stderr, "ln: %s: %s\n", argv[2], strerror(errno));
return 1;
}
return 0;
diff --git a/src/ls.c b/src/ls.c
@@ -12,8 +12,7 @@ recursive_list_dirs(const char *directory)
{
DIR *dir = opendir(directory);
if(dir == NULL) {
- fprintf(stderr, "Error opening directory: %s\n", strerror(errno));
- puts(directory);
+ fprintf(stderr, "ls: %s: %s\n", directory, strerror(errno));
return -1;
}
struct dirent *ent;
@@ -78,8 +77,8 @@ main(int argc, char *argv[])
DIR *dir = opendir(directory);
if(dir == NULL) {
/* maybe we were given a file? */
- perror(directory);
- continue;
+ fprintf(stderr, "ls: %s: %s\n", directory, strerror(errno));
+ continue;
}
struct dirent *ent;
if(recursive) {
@@ -110,7 +109,10 @@ main(int argc, char *argv[])
ent->d_name,
&st,
AT_SYMLINK_NOFOLLOW) < 0) {
- perror(ent->d_name);
+ fprintf(stderr,
+ "ls: %s: %s\n",
+ ent->d_name,
+ strerror(errno));
return -1;
}
if((st.st_mode & S_IEXEC) != 0)
diff --git a/src/mkdir.c b/src/mkdir.c
@@ -1,3 +1,4 @@
+#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
@@ -8,7 +9,7 @@ main(int argc, char *argv[])
{
int c = getopt(argc, argv, "p");
if(argc == 1) {
- fprintf(stderr, "specify path(s) to make\n");
+ fprintf(stderr, "usage [-p] dir...\n");
return 1;
}
/*
@@ -34,7 +35,7 @@ main(int argc, char *argv[])
int fd = mkdir(argv[i], 420);
if(fd == -1) {
- fprintf(stderr, "Error creating dir %s\n", argv[i]);
+ fprintf(stderr, "mkdir: %s: %s\n", argv[i], strerror(errno));
return 1;
}
}
diff --git a/src/mv.c b/src/mv.c
@@ -1,5 +1,7 @@
+#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
/*
@@ -33,6 +35,8 @@ main(int argc, char *argv[])
if(argc == 1) {
fprintf(stderr, "usage: mv source destination\n");
return 1;
- } else
- rename(argv[1], argv[2]);
+ } else if(rename(argv[1], argv[2]) == -1) {
+ fprintf(stderr, "mv: %s\n", strerror(errno));
+ return 1;
+ }
}
diff --git a/src/rm.c b/src/rm.c
@@ -12,10 +12,7 @@ main(int argc, char *argv[])
for(int i = 1; i < argc; i++) {
int fd = remove(argv[i]);
if(fd == -1) {
- fprintf(stderr,
- "Error removing file: %i = %s",
- errno,
- strerror(errno));
+ fprintf(stderr, "rm: %s: %s\n", argv[i], strerror(errno));
}
}
return 0;
diff --git a/src/rmdir.c b/src/rmdir.c
@@ -9,10 +9,7 @@ main(int argc, char *argv[])
for(int i = 1; i < argc; i++) {
if(rmdir(argv[i])) {
- fprintf(stderr,
- "rmdir: failed to remove '%s', %s\n",
- argv[i],
- strerror(errno));
+ fprintf(stderr, "rmdir: %s: %s\n", argv[i], strerror(errno));
}
}
return 0;
diff --git a/src/sleep.c b/src/sleep.c
@@ -1,15 +1,20 @@
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
if(argc == 1) {
- fprintf(stderr, "missing opperand\n");
+ fprintf(stderr, "usage: sleep time\n");
+ return 1;
+ }
+ if(usleep(atof(argv[1]) * 1000000) == -1) {
+ fprintf(stderr, "sleep: %s\n", strerror(errno));
return 1;
}
- usleep(atof(argv[1]) * 1000000);
return 0;
}
diff --git a/src/stat.c b/src/stat.c
@@ -8,14 +8,14 @@ int
main(int argc, char **argv)
{
if(argc == 1) {
- fprintf(stderr, "usage: stat FILE...\n");
+ fprintf(stderr, "usage: stat file...\n");
return 1;
}
char mod_date[64], acc_date[64], creat_date[64];
struct stat file_data;
for(int i = 1; i < argc; i++) {
if(stat(argv[i], &file_data) == -1) {
- printf("Cannot stat '%s': %s\n", argv[i], strerror(errno));
+ fprintf(stderr, "stat: %s: %s\n", argv[i], strerror(errno));
continue;
}
/* About file size, location... */
diff --git a/src/tee.c b/src/tee.c
@@ -9,15 +9,25 @@ int
tee(int fd)
{
if(fd == -1) {
- fprintf(stderr, "%s\n", strerror(errno));
+ fprintf(stderr, "tee: %s\n", strerror(errno));
return 1;
}
char buf[8192];
int read_bytes = 0;
while((read_bytes = read(0, buf, 8192)) > 0) {
- write(fd, buf, read_bytes);
+ if(read_bytes == -1) {
+ fprintf(stderr, "tee: %s\n", strerror(errno));
+ return 1;
+ }
+ if(write(fd, buf, read_bytes) == -1) {
+ fprintf(stderr, "tee: %s\n", strerror(errno));
+ return 1;
+ }
if(fd != 1)
- write(1, buf, read_bytes);
+ if(write(1, buf, read_bytes) == -1) {
+ fprintf(stderr, "tee: %s\n", strerror(errno));
+ return 1;
+ }
}
return 0;
}
diff --git a/src/touch.c b/src/touch.c
@@ -8,18 +8,15 @@
int
main(int argc, char *argv[])
{
- if(argc <= 1) {
- fprintf(stderr, "Give a file\n");
+ if(argc != 2) {
+ fprintf(stderr, "usage: touch file\n");
return 1;
}
int fd = creat(argv[1], 0644);
if(fd == -1) {
- fprintf(stderr,
- "Error creating file: %i = %s\n",
- errno,
- strerror(errno));
+ fprintf(stderr, "touch: %s: %s\n", argv[1], strerror(errno));
return 1;
}
close(fd);
diff --git a/src/tty.c b/src/tty.c
@@ -1,9 +1,16 @@
+#include <errno.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
int
main(void)
{
- printf("%s\n", ttyname(1));
+ char *tty = ttyname(1);
+ if(tty == NULL) {
+ fprintf(stderr, "tty: %s\n", strerror(errno));
+ return 1;
+ }
+ printf("%s\n", tty);
return 0;
}
diff --git a/src/uname.c b/src/uname.c
@@ -1,3 +1,4 @@
+#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
@@ -59,7 +60,10 @@ main(int argc, char *argv[])
}
}
- uname(&kernel_info);
+ if(uname(&kernel_info) == -1) {
+ fprintf(stderr, "uname: %s\n", strerror(errno));
+ return 1;
+ }
if(all) {
printf("%s %s %s %s %s %s\n",
diff --git a/src/unlink.c b/src/unlink.c
@@ -7,13 +7,13 @@ int
main(int argc, char *argv[])
{
if(argc == 1) {
- fprintf(stderr, "What do I unlink?\n");
+ fprintf(stderr, "usage: unlink file\n");
return 1;
}
int fd = unlink(argv[1]);
if(fd == -1) {
- fprintf(stderr, "Error unlinking: %i = %s\n", errno, strerror(errno));
+ fprintf(stderr, "unlink: %s: %s\n", argv[1], strerror(errno));
}
return 0;
}
diff --git a/src/wc.c b/src/wc.c
@@ -23,11 +23,8 @@ wc(const char *filename, struct wc_values *data)
{
FILE *file = fopen(filename, "r");
if(file == NULL) {
- fprintf(stderr,
- "error opening file %s: %s\n",
- filename,
- strerror(errno));
- return -1;
+ fprintf(stderr, "wc: %s: %s\n", filename, strerror(errno));
+ return 1;
}
size_t c;
char buf;
@@ -94,7 +91,7 @@ main(int argc, char *argv[])
}
}
if(optind == argc) {
- wc("/dev/stdin", &data); /* lol */
+ return_value = wc("/dev/stdin", &data); /* lol */
print_values("stdin", data);
} else
for(int i = optind; i < argc; i++) {
@@ -111,5 +108,5 @@ main(int argc, char *argv[])
data.total_words,
data.total_bytes);
- return 0;
+ return return_value;
}
diff --git a/src/whoami.c b/src/whoami.c
@@ -1,10 +1,20 @@
+#include <errno.h>
#include <pwd.h>
#include <stdio.h>
+#include <string.h>
#include <unistd.h>
int
main(void)
{
+ errno = 0;
struct passwd *user_data = getpwuid(getuid());
+ if(user_data == NULL) {
+ fprintf(stderr,
+ "whoami: %s\n",
+ errno == 0 ? "user not found" : strerror(errno));
+ return 1;
+ }
printf("%s\n", user_data->pw_name);
+ return 0;
}