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