aboutsummaryrefslogtreecommitdiff
path: root/src/touch.c
blob: dc048c07c133477da1d9f95823676592b2d0f882 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

static char *g_name = "touch";

void fatal(const char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	fprintf(stderr, "%s: ", g_name);
	vfprintf(stderr, format, ap);
	fputc('\n', stderr);
	va_end(ap);
	exit(EXIT_FAILURE);
}

enum flags
{
	FLAG_ACCESS_ONLY       = 1 << 0,
	FLAG_MODIFICATION_ONLY = 1 << 1,
	FLAG_NO_CREATE         = 1 << 2,
	FLAG_NO_DEREFERENCE    = 1 << 3,
};

int main(int argc, char **argv)
{
	int        option;
	enum flags flags = 0;
	char       *reference_filepath = NULL;

	g_name = argv[0];
	while ((option = getopt(argc, argv, "acd:fhr:t")) != -1)
	{
		switch (option)
		{
			case 'a': flags |= FLAG_ACCESS_ONLY;       break;
			case 'm': flags |= FLAG_MODIFICATION_ONLY; break;
			case 'c': flags |= FLAG_NO_CREATE;         break;
			case 'h': flags |= FLAG_NO_DEREFERENCE;    break;

			case 'd': exit(1); break;
			case 't': exit(1); break;

			case 'r': reference_filepath = optarg; break;

			case 'f': break;
		}
	}
	if (optind == argc)
		fatal("missing file operand");

	struct timeval times[2];
	if (reference_filepath != NULL)
	{
		struct stat statbuf;
		if (stat(reference_filepath, &statbuf) == -1)
			fatal("%s", strerror(errno));
		times[0].tv_sec  = statbuf.st_atim.tv_sec;
		times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
		times[1].tv_sec  = statbuf.st_mtim.tv_sec;
		times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
	}
	else
	{
		struct timeval now;
		gettimeofday(&now, NULL);
		memcpy(&times[0], &now, sizeof(struct timeval));
		memcpy(&times[1], &now, sizeof(struct timeval));
	}

	for (; optind < argc; optind++)
	{
		struct stat statbuf;
		if (stat(argv[optind], &statbuf) == -1)
		{
			if (!(flags & FLAG_NO_CREATE))
				if (close(creat(argv[optind], 0644)) == -1)
				{
					fprintf(stderr, "%s: cannot create '%s': %s", g_name, argv[optind], strerror(errno));
					continue;
				}
			fprintf(stderr, "%s: cannot atouch '%s': %s", g_name, argv[optind], strerror(errno));
			continue;
		}
		if (flags & FLAG_ACCESS_ONLY)
		{
			times[1].tv_sec  = statbuf.st_mtim.tv_sec;
			times[1].tv_usec = statbuf.st_mtim.tv_nsec / 1000;
		}
		else if (flags & FLAG_MODIFICATION_ONLY)
		{
			times[0].tv_sec  = statbuf.st_atim.tv_sec;
			times[0].tv_usec = statbuf.st_atim.tv_nsec / 1000;
		}
		if (utimes(argv[optind], times) == -1)
			fprintf(stderr, "%s: cannot touch '%s': %s", g_name, argv[optind], strerror(errno));
	}

	return EXIT_SUCCESS;
}