Skip to content

Commit

Permalink
Added better exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mckenney5 committed Jun 16, 2023
1 parent 0675a56 commit cf6f946
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions slimcat.1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ slimcat \- still concatenating files and printing on the standard output, just s
.PP
This is a rewrite of GNU cat where the unused features are removed. The only option is -h for help and -u for unbuffered output.
.PP
We concatenate files(s) to standard output with less than 60 lines of code.
We concatenate files(s) to standard output with about 60 lines of code.
.PP
With no file, or when file is \-, read standard input.
.SH EXAMPLES
Expand All @@ -27,26 +27,26 @@ Copy standard input to standard output.
.SH "EXIT STATUS"
.TP
0
Exited Normally (Success).
Exited Normally (EX_OK).
.TP
1
Error opening file.
64
Unknown command line argument (EX_USAGE).
.TP
2
Unknown command line argument.
66
Error opening file (EX_NOINPUT).
.TP
3
Error while writing.
71
Error while allocating memory (EX_OSERR).
.TP
4
Error while allocating memory.
74
Error while writing (EX_IOERR).

.SH AUTHOR
Written by Adam McKenney
.SH "REPORTING BUGS"
See the offical repo: <https://github.com/mckenney5/slim-cat>
.SH COPYRIGHT
Copyright \(co 2021 Adam McKenney
Copyright \(co 2023 Adam McKenney


Permission is hereby granted, free of charge, to any person obtaining a copy
Expand All @@ -65,5 +65,5 @@ See the license for more information.
.SH "POSIX"
See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cat.html for more info on cat.
.SH "SEE ALSO"
\fBtac\fP(1)
\fBtac\fP(1) \fBsysexits.h\fP(3head)

20 changes: 10 additions & 10 deletions src/cat.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* Copyright 2023 (c) Adam McKenney - See LICENSE for details */
/* A way slimmer version of cat without the GNU bloat features that I have never used nor needed */
#include <stdio.h> //fprintf, perror
#include <stdlib.h> //exit
#include <stdlib.h> //malloc
#include <err.h> //err, errx
#include <sysexits.h> //EX_OK, EX_OSERR, EX_IOERR, EX_NOINPUT
#include <unistd.h> //read, write, close
#include <sys/types.h> //stat
#include <sys/stat.h> //stat
Expand All @@ -21,10 +21,10 @@ void cat(const int fd, const char no_buf, size_t size){
size_t r = 0;
if(no_buf) size = 1;
void *buf = (void*)malloc(size);
if(buf == NULL) err(EXIT_FAILURE, "while allocating buf in cat()");
if(buf == NULL) err(EX_OSERR, "while allocating buf in cat()");
while((r=read(fd, buf, size)) > 0)
if(write(STDOUT_FILENO, buf, r) != r) err(EXIT_FAILURE, "while writing to fd in cat(). %ld bytes were written", r); //if writing stops for some reason
if(r == -1) err(EXIT_FAILURE, "while reading fd in cat()."); //if read(2) throws an error
if(write(STDOUT_FILENO, buf, r) != r) err(EX_IOERR, "while writing to fd in cat(). %ld bytes were written", r); //if writing stops for some reason
if(r == -1) err(EX_IOERR, "while reading fd in cat()."); //if read(2) throws an error
free(buf);
}
}
Expand All @@ -33,9 +33,9 @@ void get_file(const char* file_name, const char no_buf){
if(file_name[0] == '-' && file_name[1] == '\0'){ cat(STDIN_FILENO, no_buf, BUFF_SIZE); return;}
struct stat buffer;
int fd = -1, status = stat(file_name, &buffer);
if(status != 0) err(EXIT_FAILURE, "while checking file '%s' in get_file()", file_name);
if(status != 0) err(EX_NOINPUT, "while checking file '%s' in get_file()", file_name);
fd = open(file_name, O_RDONLY);
if(fd == -1) err(EXIT_FAILURE, "while opening file '%s' in get_file()", file_name);
if(fd == -1) err(EX_IOERR, "while opening file '%s' in get_file()", file_name);
cat(fd, no_buf, buffer.st_size);
close(fd);
}
Expand All @@ -46,12 +46,12 @@ int main(int argc, char *argv[]){
if(argv[1] == NULL)
cat(STDIN_FILENO, no_buf, BUFF_SIZE);
else if(argv[i][0] == '-' && argv[i][1] != '\0'){ //if its a command line arg
if(argv[i][1] == 'h') { puts(HELP); return 0; }
if(argv[i][1] == 'h') errx(EX_OK, "\n%s\n", HELP);
else if(argv[i][1] == 'u') no_buf = 1; //required by POSIX, turns off buffered output
else errx(EXIT_FAILURE, "while reading command line arg %ld in main(): unknown command line arg '%s'\n", i, argv[i]);
else errx(EX_USAGE, "while reading command line arg %ld in main(): unknown command line arg '%s'\n", i, argv[i]);
} else {
for( ; i < argc; i++) get_file(argv[i], no_buf); //step through each arg as if it's a file, even if its a -
}
}
return 0;
return EX_OK;
}

0 comments on commit cf6f946

Please sign in to comment.