fixed segfault

This commit is contained in:
Emma Tebibyte 2023-07-24 09:01:41 -06:00
parent bb099036fb
commit 9adde985d9
Signed by untrusted user: emma
GPG Key ID: 6D661C738815E7DD
1 changed files with 12 additions and 6 deletions

View File

@ -18,6 +18,7 @@
* along with this program. If not, see https://www.gnu.org/licenses/. * along with this program. If not, see https://www.gnu.org/licenses/.
*/ */
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <sysexits.h> #include <sysexits.h>
#include <stdbool.h> #include <stdbool.h>
@ -71,11 +72,12 @@ int main(int argc, char *argv[]) {
* but shall accept multiple occurrences of '-' as a file * but shall accept multiple occurrences of '-' as a file
* operand. * operand.
*/ */
printf("argv[%d]: %s\n", i, argv[1]); printf("argv[%d]: %s\n", i, argv[i]);
if (argv[i] == "-" || u && (argc - 1) == 1 || !u && argc == 1) {
if (argc == 1 || argv[i] == "-") { file = fdopen(1, "r"); } file = fdopen(0, "r");
else if ((file = fopen(argv[i], O_RDONLY)) == NULL) { } else if ((file = fopen(argv[i], "r")) == NULL) {
fputs(usage, stderr); // TODO: Add error handling
fputs(usage, stderr);
return EX_NOINPUT; return EX_NOINPUT;
} }
@ -87,7 +89,11 @@ int main(int argc, char *argv[]) {
putchar(byte); putchar(byte);
} }
} else { } else {
char *buf = calloc(4096, 1); char *buf = calloc(1, 4096);
while (byte != EOF) {
byte = fgetc(file);
fputc(byte, fdopen(1, "r"));
}
} }
if (fclose(file) == -1) { if (fclose(file) == -1) {
fprintf(stderr, "%s: %s: Error closing file.\n", argv[0], argv[i]); fprintf(stderr, "%s: %s: Error closing file.\n", argv[0], argv[i]);