From b535b05436ac22f7601df04ae60152075ad65934 Mon Sep 17 00:00:00 2001 From: Squibid Date: Fri, 5 May 2023 08:58:44 +0000 Subject: add flags, verbose flag, no write flag --- touch.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/touch.c b/touch.c index 19b9ed7..86bca8e 100644 --- a/touch.c +++ b/touch.c @@ -1,12 +1,39 @@ #include +#include +#include +#include int main(int argc, char *argv[]) { FILE *f; + int wr = 1; + int v = 0; + int c; + + while ((c = getopt(argc, argv, "cv")) != -1) { + switch (c) { + case 'c': wr = 0; break; + case 'v': v = 1; break; + } + } for (int i = 1; i < argc; i++) { - f = fopen(argv[i], "a"); - fprintf(f, "\n"); /* TODO make this actually work without writing anything to the file*/ - fclose(f); + while (strncmp(argv[i], "-", 1) == 0) /* ignore args that start with - */ + i++; + if (wr) { + if (access (argv[i], F_OK) && v) { + printf("Making new file '%s'.\n", argv[i]); + } else if (v) { + printf("File '%s' already exists, updating time.\n", argv[i]); + } + f = fopen(argv[i], "w"); + fclose(f); + } + if (access (argv[i], F_OK) && !wr && v) { + printf("File '%s' doesn't exist, Can't update time.\n", argv[i]); + } else if (!wr && v) { + printf("File '%s' exist's, updating time.\n", argv[i]); + } + utime(argv[i], NULL); } } -- cgit v1.2.1