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