aboutsummaryrefslogtreecommitdiffstats
path: root/touch.c
blob: 86bca8e0e3346df14d4d15ee1885c98882d8a425 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <utime.h>
#include <unistd.h>
#include <string.h>

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++) {
    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);
  }
}