syscall-advent

System Call Advent Calendar
Log | Files | Refs

commit b8e17b96ba267f1fd21279f00226f4db7ff2fba3
parent 34f463cf39e4caf4a1e5aeeffd6b64721cb22d4d
Author: dwrz <dwrz@dwrz.net>
Date:   Tue,  6 Dec 2022 17:46:40 +0000

Refactor cat

Diffstat:
M01-cat.c | 10++++------
1 file changed, 4 insertions(+), 6 deletions(-)

diff --git a/01-cat.c b/01-cat.c @@ -9,15 +9,13 @@ static char buf[4096]; int main(int argc, char *argv[]) { - for (int i = 0; i < argc; ++i) { - // Skip the first argument (program name). - if (i == 0) continue; - + // Skip the first argument (program name). + for (int i = 1; i < argc; ++i) { // Open the file. int fd = open(argv[i], O_RDONLY); if (fd == 0) { perror("failed to open"); - exit(1); + return 1; } // Read the file into the buffer, then print it to stdout. @@ -25,7 +23,7 @@ int main(int argc, char *argv[]) { while ((count = read(fd, &buf, sizeof(buf))) != 0) { if (count < 1) { perror("failed to read"); - exit(1); + return 1; } // To prevent printing contents from a read that // didn't fill the buffer, terminate the string.