summaryrefslogtreecommitdiff
path: root/utils/touchy/touchy.c
blob: dc4dc8d83f48022f15373a30fc6791e834541d92 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
 * Simple _utime() wrapper for setting the mod. time on files
 * to the current system time.
 *
 */
#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(_WIN32)
#error "Win32-only, the platform you're using is supposed to have 'touch' already."
#else
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <utime.h>

int
main(int argc, char** argv)
{
  int rc;
  int i=0;
  int fd;
  int wBitSet = 0;
  struct _stat sb;

  if (argc == 1) {
    fprintf(stderr, "Usage: %s <files>\n", argv[0]);
    return 1;
  }
  
  
  while (i++ < (argc-1)) {
    if ( (_access(argv[i], 00) < 0) && (errno == ENOENT || errno == EACCES) ) {
       /* File doesn't exist, try creating it. */
      if ( (fd = _open(argv[i], _O_CREAT | _O_EXCL | _O_TRUNC, _S_IREAD | _S_IWRITE)) < 0 ) {
      	fprintf(stderr, "Unable to create %s, skipping.\n", argv[i]);
      } else {
      	_close(fd);
      }
    }
    if ( (_access(argv[i], 02)) < 0 ) {
    	/* No write permission, try setting it first. */
	if (_stat(argv[i], &sb) < 0) {
	   fprintf(stderr, "Unable to change mod. time for %s  (%d)\n", argv[i], errno);
	   continue;
	}
	if (_chmod(argv[i], (sb.st_mode & _S_IREAD) | _S_IWRITE) < 0) {
	   fprintf(stderr, "Unable to change mod. time for %s  (%d)\n", argv[i], errno);
	   continue;
	}
	wBitSet = 1;
    }
    if ( (rc = _utime(argv[i],NULL)) < 0) {
      fprintf(stderr, "Unable to change mod. time for %s  (%d)\n", argv[i], errno);
    }
    if (wBitSet) {
	/* Turn the file back into a read-only file */
   	_chmod(argv[i], (sb.st_mode & _S_IREAD));
	wBitSet = 0;
    }
  }
  
  return 0;
}
#endif