Mercurial > hg > toybox
comparison toys/posix/touch.c @ 699:26ea643e1de1
Add touch from Choubey Ji.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 16 Nov 2012 12:43:41 -0600 |
parents | |
children | ad6bff0c9169 |
comparison
equal
deleted
inserted
replaced
698:078138791b5c | 699:26ea643e1de1 |
---|---|
1 /* vi: set sw=4 ts=4: | |
2 * | |
3 * touch.c : change timestamp of a file | |
4 * Copyright 2012 Choubey Ji <warior.linux@gmail.com> | |
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 | |
22 | |
23 #include "toys.h" | |
24 | |
25 time_t get_time_sec(char *); | |
26 | |
27 void touch_main(void){ | |
28 int fd, touch_flag_t = 0, touch_flag_m = 0, touch_flag_r = 0; | |
29 time_t now; | |
30 struct utimbuf modinfo; | |
31 struct stat filestat; | |
32 if(!toys.optflags){ | |
33 time(&now); | |
34 modinfo.modtime = now; | |
35 modinfo.actime = now; | |
36 }else{ | |
37 if(toys.optflags & 1){ | |
38 touch_flag_t = 1; | |
39 if((toys.optflags >> 2) & 1){ | |
40 touch_flag_m = 1; | |
41 } | |
42 } | |
43 | |
44 if((toys.optflags >> 1) & 1) | |
45 touch_flag_r = 1; | |
46 | |
47 if(toys.optflags >> 2) | |
48 touch_flag_m = 1; | |
49 } | |
50 | |
51 if(touch_flag_t){ | |
52 if(!check_date_format(toys.optargs[0])){ | |
53 modinfo.modtime = get_time_sec((char *)toys.optargs[0]); | |
54 modinfo.actime = get_time_sec(toys.optargs[0]); | |
55 }else{ | |
56 perror_msg("Invalid date format, -t [yyyyMMddhhmm.ss]"); | |
57 toys.exitval = EXIT_FAILURE; | |
58 } | |
59 } | |
60 if(touch_flag_r){ | |
61 if(stat(toys.optargs[0], &filestat) < 0){ | |
62 printf("Error : unable to get information for file %s\n", toys.optargs[0]); | |
63 toys.exitval = EXIT_FAILURE; | |
64 } | |
65 modinfo.modtime = filestat.st_mtime; | |
66 modinfo.actime = filestat.st_atime; | |
67 } | |
68 if(touch_flag_m){ | |
69 if(stat(toys.optargs[toys.optc - 1], &filestat) < 0){ | |
70 toys.exitval = EXIT_FAILURE; | |
71 return; | |
72 } | |
73 modinfo.actime = filestat.st_atime; | |
74 if(!(touch_flag_r | touch_flag_t)){ | |
75 time(&now); | |
76 modinfo.modtime = now; | |
77 } | |
78 } | |
79 if(utime(toys.optargs[toys.optc - 1], &modinfo) == -1){ | |
80 if((fd = open(toys.optargs[toys.optc - 1],O_CREAT |O_RDWR, 0644)) != -1){ | |
81 close(fd); | |
82 utime(toys.optargs[toys.optc - 1], &modinfo); | |
83 }else{ | |
84 perror("unable to create the file"); | |
85 toys.exitval = EXIT_FAILURE; | |
86 } | |
87 } | |
88 } | |
89 | |
90 int check_date_format(char *date_input){ | |
91 int count_date_digit = 0; | |
92 unsigned long long flag_b4_sec; | |
93 int flag_af_sec; | |
94 char *date_store = (char *)malloc(12 * sizeof(char)); | |
95 while(date_input[count_date_digit] != '.'){ | |
96 date_store[count_date_digit] = date_input[count_date_digit]; | |
97 count_date_digit++; | |
98 } | |
99 date_store[count_date_digit++] = '\0'; | |
100 flag_b4_sec = atoll(date_store); | |
101 date_store[0] = date_input[count_date_digit++]; | |
102 date_store[1] = date_input[count_date_digit]; | |
103 date_store[2] = '\0'; | |
104 flag_af_sec = atoi(date_store); | |
105 if(date_store[0] == '0' && date_store[1] == '0') | |
106 flag_af_sec = 1; | |
107 if(flag_b4_sec && flag_af_sec) | |
108 return 0; | |
109 else | |
110 return -1; | |
111 } | |
112 | |
113 /* function to return number of seconds since epoch till the given date */ | |
114 time_t get_time_sec(char *date_input){ | |
115 int count_date_digit = 0; | |
116 char temp_date[12]; | |
117 char mm[2]; | |
118 char dd[2]; | |
119 char hh[2]; | |
120 char ss[2]; | |
121 char year[4]; | |
122 time_t time_of_modify; | |
123 struct tm t_yyyymmddhhss; | |
124 while(date_input[count_date_digit] != '.'){ | |
125 if(count_date_digit < 4){ | |
126 year[count_date_digit] = date_input[count_date_digit]; | |
127 } | |
128 count_date_digit++; | |
129 if(count_date_digit == 4){ | |
130 year[count_date_digit] = '\0'; | |
131 t_yyyymmddhhss.tm_year = atoi(year)-1900; | |
132 break; | |
133 } | |
134 } | |
135 mm[0] = date_input[4]; | |
136 mm[1] = date_input[5]; | |
137 mm[2] = '\0'; | |
138 t_yyyymmddhhss.tm_mon = (atoi(mm) - 1); | |
139 mm[0] = date_input[6]; | |
140 mm[1] = date_input[7]; | |
141 mm[2] = '\0'; | |
142 t_yyyymmddhhss.tm_mday = atoi(mm); | |
143 mm[0] = date_input[8]; | |
144 mm[1] = date_input[9]; | |
145 mm[2] = '\0'; | |
146 t_yyyymmddhhss.tm_hour = atoi(mm); | |
147 mm[0] = date_input[10]; | |
148 mm[1] = date_input[11]; | |
149 mm[2] = '\0'; | |
150 t_yyyymmddhhss.tm_min = atoi(mm); | |
151 mm[0] = date_input[13]; | |
152 mm[1] = date_input[14]; | |
153 mm[2] = '\0'; | |
154 t_yyyymmddhhss.tm_sec = atoi(mm); | |
155 time_of_modify = mktime(&t_yyyymmddhhss); | |
156 return time_of_modify; | |
157 } |