diff options
author | Jack Sangdahl <[email protected]> | 2024-09-25 10:21:55 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2024-09-25 10:21:55 -0600 |
commit | 5b2e4d95a74f83277b654fadceb98bef87690a54 (patch) | |
tree | 7d777ed2eff49692a3d785e2eb5fbf84eb2a6857 | |
parent | 7caf1387f967d58c0ba6d29b30a62fcd61b4caa0 (diff) | |
download | csci3753-5b2e4d95a74f83277b654fadceb98bef87690a54.tar.gz csci3753-5b2e4d95a74f83277b654fadceb98bef87690a54.zip |
pa2: improvements
-rw-r--r-- | pa2_file_tester/file_test.c | 83 |
1 files changed, 30 insertions, 53 deletions
diff --git a/pa2_file_tester/file_test.c b/pa2_file_tester/file_test.c index c5c5be4..1f04bac 100644 --- a/pa2_file_tester/file_test.c +++ b/pa2_file_tester/file_test.c @@ -6,86 +6,63 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> -#define MAX 1024 - -int file_read(int fd, int bytes); -int file_write(int fd, char *data); -int file_seek(int fd, int offset, int whence); +#define MAX_SIZE 256 int main(int argc, char **argv) { if (argc < 2) { printf("%s error: missing filename\n", argv[0]); - return -1; + exit(EXIT_FAILURE); } - int fd = open(argv[1], O_RDWR); - char input[8]; // fgets requires 8 bytes minimum - char *data = (char *)malloc(MAX * sizeof(char)); + if (!fd) { + printf("%s error: invalid filename\n", argv[0]); + exit(EXIT_FAILURE); + } + char *read_data, write_data[MAX_SIZE], input; int bytes, offset, whence; - printf("Option (r for read, w for write, s for seek): "); - while (fgets(input, sizeof(input), stdin)) { - switch (*input) { + while (scanf("%c", &input) != EOF) { + switch (input) { + case 'r': printf("Enter the number of bytes you want to read: "); scanf("%d", &bytes); - if (file_read(fd, bytes) < 0) { - printf("%s error: read failed\n", argv[0]); - return -1; + read_data = (char *)malloc(bytes * sizeof(char)); + if (read(fd, read_data, bytes) < 0) { + fprintf(stderr, "%s error: read failed\n", argv[0]); + free(read_data); + exit(EXIT_FAILURE); } + printf("%s\n", read_data); + free(read_data); break; + case 'w': printf("Enter the data you want to write: "); - scanf("%[^\n]", data); - data[strcspn(data, "\n")] = 0; // remove new line from string - data[strlen(data) + 1] = '\0'; // append null character to string - if (file_write(fd, data) < 0) { - printf("%s error: write failed\n", argv[0]); - free(data); - return -1; + scanf(" %255[^\n]", write_data); + int n = strlen(write_data) + 1; + write_data[n] = '\0'; + if (write(fd, write_data, n) < 0) { + fprintf(stderr, "%s error: write failed\n", argv[0]); + exit(EXIT_FAILURE); } - free(data); break; + case 's': printf("Enter an offset value: "); scanf("%d", &offset); printf("Enter a value for whence: "); scanf("%d", &whence); - if (file_seek(fd, offset, whence) < 0) { - printf("%s error: seek failed\n", argv[0]); - return -1; + if (lseek(fd, offset, whence) < 0) { + fprintf(stderr, "%s error: seek failed\n", argv[0]); + exit(EXIT_FAILURE); } break; + default: printf("Option (r for read, w for write, s for seek): "); } } close(fd); - return 0; -} - -int file_read(int fd, int bytes) { - char *buffer = (char *)malloc(bytes * sizeof(char)); - if (read(fd, buffer, bytes) < 0) { - free(buffer); - return -1; - } - printf("%s\n", buffer); - free(buffer); - return 0; -} - -int file_write(int fd, char *data) { - if (write(fd, data, strlen(data)) < 0) - return -1; - return 0; -} - -int file_seek(int fd, int offset, int whence) { -#define SEEK_HOLE 4 // should be in unistd header? - if (whence < SEEK_SET || whence > SEEK_HOLE) - return -1; - if (lseek(fd, offset, whence) < 0) - return -1; - return 0; + return EXIT_SUCCESS; } |