From 407faeea6f3d8e7df4efa14de584044131fe6b7b Mon Sep 17 00:00:00 2001 From: Perry Kivolowitz Date: Mon, 19 Dec 2022 22:06:13 -0600 Subject: [PATCH] c version of file operations --- section_1/funcs/fops.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 section_1/funcs/fops.c diff --git a/section_1/funcs/fops.c b/section_1/funcs/fops.c new file mode 100644 index 0000000..e881002 --- /dev/null +++ b/section_1/funcs/fops.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +int main(int argc, char **argv) { + char * fname = "test.txt"; + char * p_legend = NULL; + int fd = open(fname, O_RDWR | O_CREAT, 00600); + int retval = 0; + off_t seek_return; + char buffer[80]; + + if (fd < 0) { + p_legend = fname; + goto out; + } else { + printf("Flags used to open file: %o\n", O_RDWR | O_CREAT); + for (int counter = 0; counter < 10; counter++) { + if (write(fd, "data\n", 5) != 5) { + p_legend = "On writing"; + goto out; + } + } + + if (seek_return = lseek(fd, 0, SEEK_SET) != 0) { + p_legend = "On seeking"; + goto out; + } + + for (int counter = 0; counter < 10; counter++) { + memset(buffer, 0, 80); + if (read(fd, buffer, 5) != 5) { + p_legend = "On reading"; + goto out; + } + printf("%d %s", counter, buffer); + } + +out: if (fd >= 0) { + close(fd); + if (unlink(fname)) { + p_legend = "On unlinking"; + } + } + + if (p_legend != NULL) { + perror(p_legend); + retval = 1; + } + } + return retval; +}