fixed segfault
This commit is contained in:
parent
bb099036fb
commit
9adde985d9
16
src/cat.c
16
src/cat.c
|
@ -18,6 +18,7 @@
|
|||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sysexits.h>
|
||||
#include <stdbool.h>
|
||||
|
@ -71,10 +72,11 @@ int main(int argc, char *argv[]) {
|
|||
* but shall accept multiple occurrences of '-' as a file
|
||||
* operand.
|
||||
*/
|
||||
printf("argv[%d]: %s\n", i, argv[1]);
|
||||
|
||||
if (argc == 1 || argv[i] == "-") { file = fdopen(1, "r"); }
|
||||
else if ((file = fopen(argv[i], O_RDONLY)) == NULL) {
|
||||
printf("argv[%d]: %s\n", i, argv[i]);
|
||||
if (argv[i] == "-" || u && (argc - 1) == 1 || !u && argc == 1) {
|
||||
file = fdopen(0, "r");
|
||||
} else if ((file = fopen(argv[i], "r")) == NULL) {
|
||||
// TODO: Add error handling
|
||||
fputs(usage, stderr);
|
||||
return EX_NOINPUT;
|
||||
}
|
||||
|
@ -87,7 +89,11 @@ int main(int argc, char *argv[]) {
|
|||
putchar(byte);
|
||||
}
|
||||
} 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) {
|
||||
fprintf(stderr, "%s: %s: Error closing file.\n", argv[0], argv[i]);
|
||||
|
|
Loading…
Reference in New Issue
Block a user