Mercurial > hg > toybox
annotate toys/posix/touch.c @ 700:ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 16 Nov 2012 15:36:01 -0600 |
parents | 26ea643e1de1 |
children | 7b316e3c0e11 |
rev | line source |
---|---|
699 | 1 /* vi: set sw=4 ts=4: |
2 * | |
3 * touch.c : change timestamp of a file | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
4 * Copyright 2012 Choubey Ji <warior.linux@gmail.com> |
699 | 5 * |
6 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/touch.html | |
7 | |
8 USE_TOUCH(NEWTOY(touch, "mrt", TOYFLAG_BIN)) | |
9 | |
10 config TOUCH | |
11 bool "th" | |
12 default y | |
13 help | |
14 Usage: Usage: touch [OPTION]... FILE... | |
15 Update the access and modification times of each FILE to the current time. | |
16 -m change only the modification time | |
17 -r, --reference=FILE use this file's times instead of current time | |
18 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time | |
19 */ | |
20 | |
21 #include "toys.h" | |
22 | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
23 int check_date_format(char *date_input) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
24 { |
699 | 25 int count_date_digit = 0; |
26 unsigned long long flag_b4_sec; | |
27 int flag_af_sec; | |
28 char *date_store = (char *)malloc(12 * sizeof(char)); | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
29 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
30 while(date_input[count_date_digit] != '.') { |
699 | 31 date_store[count_date_digit] = date_input[count_date_digit]; |
32 count_date_digit++; | |
33 } | |
34 date_store[count_date_digit++] = '\0'; | |
35 flag_b4_sec = atoll(date_store); | |
36 date_store[0] = date_input[count_date_digit++]; | |
37 date_store[1] = date_input[count_date_digit]; | |
38 date_store[2] = '\0'; | |
39 flag_af_sec = atoi(date_store); | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
40 if(date_store[0] == '0' && date_store[1] == '0') flag_af_sec = 1; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
41 if(flag_b4_sec && flag_af_sec) return 0; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
42 else return -1; |
699 | 43 } |
44 | |
45 /* function to return number of seconds since epoch till the given date */ | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
46 time_t get_time_sec(char *date_input) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
47 { |
699 | 48 int count_date_digit = 0; |
49 char temp_date[12]; | |
50 char mm[2]; | |
51 char dd[2]; | |
52 char hh[2]; | |
53 char ss[2]; | |
54 char year[4]; | |
55 time_t time_of_modify; | |
56 struct tm t_yyyymmddhhss; | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
57 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
58 while(date_input[count_date_digit] != '.') { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
59 if(count_date_digit < 4) |
699 | 60 year[count_date_digit] = date_input[count_date_digit]; |
61 count_date_digit++; | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
62 if(count_date_digit == 4) { |
699 | 63 year[count_date_digit] = '\0'; |
64 t_yyyymmddhhss.tm_year = atoi(year)-1900; | |
65 break; | |
66 } | |
67 } | |
68 mm[0] = date_input[4]; | |
69 mm[1] = date_input[5]; | |
70 mm[2] = '\0'; | |
71 t_yyyymmddhhss.tm_mon = (atoi(mm) - 1); | |
72 mm[0] = date_input[6]; | |
73 mm[1] = date_input[7]; | |
74 mm[2] = '\0'; | |
75 t_yyyymmddhhss.tm_mday = atoi(mm); | |
76 mm[0] = date_input[8]; | |
77 mm[1] = date_input[9]; | |
78 mm[2] = '\0'; | |
79 t_yyyymmddhhss.tm_hour = atoi(mm); | |
80 mm[0] = date_input[10]; | |
81 mm[1] = date_input[11]; | |
82 mm[2] = '\0'; | |
83 t_yyyymmddhhss.tm_min = atoi(mm); | |
84 mm[0] = date_input[13]; | |
85 mm[1] = date_input[14]; | |
86 mm[2] = '\0'; | |
87 t_yyyymmddhhss.tm_sec = atoi(mm); | |
88 time_of_modify = mktime(&t_yyyymmddhhss); | |
89 return time_of_modify; | |
90 } | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
91 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
92 void touch_main(void) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
93 { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
94 int fd, touch_flag_t = 0, touch_flag_m = 0, touch_flag_r = 0; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
95 time_t now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
96 struct utimbuf modinfo; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
97 struct stat filestat; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
98 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
99 if (!toys.optflags) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
100 time(&now); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
101 modinfo.modtime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
102 modinfo.actime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
103 } else { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
104 if (toys.optflags & 1) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
105 touch_flag_t = 1; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
106 if ((toys.optflags >> 2) & 1) touch_flag_m = 1; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
107 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
108 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
109 if ((toys.optflags >> 1) & 1) touch_flag_r = 1; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
110 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
111 if (toys.optflags >> 2) touch_flag_m = 1; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
112 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
113 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
114 if (touch_flag_t) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
115 if (!check_date_format(toys.optargs[0])) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
116 modinfo.modtime = get_time_sec((char *)toys.optargs[0]); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
117 modinfo.actime = get_time_sec(toys.optargs[0]); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
118 } else { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
119 perror_msg("Invalid date format, -t [yyyyMMddhhmm.ss]"); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
120 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
121 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
122 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
123 if(touch_flag_r) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
124 if(stat(toys.optargs[0], &filestat) < 0) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
125 printf("Error : unable to get information for file %s\n", toys.optargs[0]); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
126 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
127 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
128 modinfo.modtime = filestat.st_mtime; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
129 modinfo.actime = filestat.st_atime; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
130 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
131 if(touch_flag_m) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
132 if(stat(toys.optargs[toys.optc - 1], &filestat) < 0) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
133 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
134 return; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
135 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
136 modinfo.actime = filestat.st_atime; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
137 if(!(touch_flag_r | touch_flag_t)) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
138 time(&now); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
139 modinfo.modtime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
140 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
141 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
142 if (utime(toys.optargs[toys.optc - 1], &modinfo) == -1) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
143 if ((fd = open(toys.optargs[toys.optc - 1],O_CREAT |O_RDWR, 0644)) != -1) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
144 close(fd); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
145 utime(toys.optargs[toys.optc - 1], &modinfo); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
146 } else { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
147 perror("unable to create the file"); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
148 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
149 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
150 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
151 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
152 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
153 |