Mercurial > hg > toybox
annotate toys/posix/touch.c @ 701:7b316e3c0e11
Fix touch warnings (unused variables, failure to reserve space for null terminator).
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 16 Nov 2012 15:44:45 -0600 |
parents | ad6bff0c9169 |
children | 9fb5fe6eb13f |
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; |
701
7b316e3c0e11
Fix touch warnings (unused variables, failure to reserve space for null terminator).
Rob Landley <rob@landley.net>
parents:
700
diff
changeset
|
49 char mm[3]; |
7b316e3c0e11
Fix touch warnings (unused variables, failure to reserve space for null terminator).
Rob Landley <rob@landley.net>
parents:
700
diff
changeset
|
50 char year[5]; |
699 | 51 time_t time_of_modify; |
52 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
|
53 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
54 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
|
55 if(count_date_digit < 4) |
699 | 56 year[count_date_digit] = date_input[count_date_digit]; |
57 count_date_digit++; | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
58 if(count_date_digit == 4) { |
699 | 59 year[count_date_digit] = '\0'; |
60 t_yyyymmddhhss.tm_year = atoi(year)-1900; | |
61 break; | |
62 } | |
63 } | |
64 mm[0] = date_input[4]; | |
65 mm[1] = date_input[5]; | |
66 mm[2] = '\0'; | |
67 t_yyyymmddhhss.tm_mon = (atoi(mm) - 1); | |
68 mm[0] = date_input[6]; | |
69 mm[1] = date_input[7]; | |
70 mm[2] = '\0'; | |
71 t_yyyymmddhhss.tm_mday = atoi(mm); | |
72 mm[0] = date_input[8]; | |
73 mm[1] = date_input[9]; | |
74 mm[2] = '\0'; | |
75 t_yyyymmddhhss.tm_hour = atoi(mm); | |
76 mm[0] = date_input[10]; | |
77 mm[1] = date_input[11]; | |
78 mm[2] = '\0'; | |
79 t_yyyymmddhhss.tm_min = atoi(mm); | |
80 mm[0] = date_input[13]; | |
81 mm[1] = date_input[14]; | |
82 mm[2] = '\0'; | |
83 t_yyyymmddhhss.tm_sec = atoi(mm); | |
84 time_of_modify = mktime(&t_yyyymmddhhss); | |
85 return time_of_modify; | |
86 } | |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
87 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
88 void touch_main(void) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
89 { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
90 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
|
91 time_t now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
92 struct utimbuf modinfo; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
93 struct stat filestat; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
94 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
95 if (!toys.optflags) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
96 time(&now); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
97 modinfo.modtime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
98 modinfo.actime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
99 } else { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
100 if (toys.optflags & 1) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
101 touch_flag_t = 1; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
102 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
|
103 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
104 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
105 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
|
106 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
107 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
|
108 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
109 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
110 if (touch_flag_t) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 } else { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
115 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
|
116 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
117 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
118 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
119 if(touch_flag_r) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
120 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
|
121 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
|
122 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
123 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
124 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
|
125 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
|
126 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
127 if(touch_flag_m) { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
128 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
|
129 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
130 return; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
131 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
132 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
|
133 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
|
134 time(&now); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
135 modinfo.modtime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
136 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
137 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
138 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
|
139 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
|
140 close(fd); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
141 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
|
142 } else { |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
143 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
|
144 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
145 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
146 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
147 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
148 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
149 |