40 |
int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
41 |
int touch_main(int argc UNUSED_PARAM, char **argv) |
int touch_main(int argc UNUSED_PARAM, char **argv) |
42 |
{ |
{ |
43 |
|
int fd; |
44 |
|
int status = EXIT_SUCCESS; |
45 |
|
int opts; |
46 |
#if ENABLE_DESKTOP |
#if ENABLE_DESKTOP |
47 |
struct utimbuf timebuf; |
# if ENABLE_LONG_OPTS |
48 |
|
static const char touch_longopts[] ALIGN1 = |
49 |
|
/* name, has_arg, val */ |
50 |
|
"no-create\0" No_argument "c" |
51 |
|
"reference\0" Required_argument "r" |
52 |
|
"date\0" Required_argument "d" |
53 |
|
; |
54 |
|
# endif |
55 |
char *reference_file = NULL; |
char *reference_file = NULL; |
56 |
|
char *date_str = NULL; |
57 |
|
struct timeval timebuf[2]; |
58 |
|
timebuf[1].tv_usec = timebuf[0].tv_usec = 0; |
59 |
#else |
#else |
60 |
#define reference_file NULL |
# define reference_file NULL |
61 |
#define timebuf (*(struct utimbuf*)NULL) |
# define date_str NULL |
62 |
|
# define timebuf ((struct timeval*)NULL) |
63 |
#endif |
#endif |
64 |
int fd; |
|
65 |
int status = EXIT_SUCCESS; |
#if ENABLE_DESKTOP && ENABLE_LONG_OPTS |
66 |
int flags = getopt32(argv, "c" USE_DESKTOP("r:") |
applet_long_options = touch_longopts; |
67 |
|
#endif |
68 |
|
/* -d and -t both set time. In coreutils, |
69 |
|
* accepted data format differs a bit between -d and -t. |
70 |
|
* We accept the same formats for both */ |
71 |
|
opts = getopt32(argv, "c" IF_DESKTOP("r:d:t:") |
72 |
/*ignored:*/ "fma" |
/*ignored:*/ "fma" |
73 |
USE_DESKTOP(, &reference_file)); |
IF_DESKTOP(, &reference_file) |
74 |
|
IF_DESKTOP(, &date_str) |
75 |
|
IF_DESKTOP(, &date_str) |
76 |
|
); |
77 |
|
|
78 |
flags &= 1; /* only -c bit is left */ |
opts &= 1; /* only -c bit is left */ |
79 |
argv += optind; |
argv += optind; |
80 |
if (!*argv) { |
if (!*argv) { |
81 |
bb_show_usage(); |
bb_show_usage(); |
84 |
if (reference_file) { |
if (reference_file) { |
85 |
struct stat stbuf; |
struct stat stbuf; |
86 |
xstat(reference_file, &stbuf); |
xstat(reference_file, &stbuf); |
87 |
timebuf.actime = stbuf.st_atime; |
timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime; |
88 |
timebuf.modtime = stbuf.st_mtime; |
} |
89 |
|
|
90 |
|
if (date_str) { |
91 |
|
struct tm tm_time; |
92 |
|
time_t t; |
93 |
|
|
94 |
|
//time(&t); |
95 |
|
//localtime_r(&t, &tm_time); |
96 |
|
memset(&tm_time, 0, sizeof(tm_time)); |
97 |
|
parse_datestr(date_str, &tm_time); |
98 |
|
|
99 |
|
/* Correct any day of week and day of year etc. fields */ |
100 |
|
tm_time.tm_isdst = -1; /* Be sure to recheck dst */ |
101 |
|
t = validate_tm_time(date_str, &tm_time); |
102 |
|
|
103 |
|
timebuf[1].tv_sec = timebuf[0].tv_sec = t; |
104 |
} |
} |
105 |
|
|
106 |
do { |
do { |
107 |
if (utime(*argv, reference_file ? &timebuf : NULL)) { |
if (utimes(*argv, (reference_file || date_str) ? timebuf : NULL) != 0) { |
108 |
if (errno == ENOENT) { /* no such file */ |
if (errno == ENOENT) { /* no such file */ |
109 |
if (flags) { /* creation is disabled, so ignore */ |
if (opts) { /* creation is disabled, so ignore */ |
110 |
continue; |
continue; |
111 |
} |
} |
112 |
/* Try to create the file. */ |
/* Try to create the file */ |
113 |
fd = open(*argv, O_RDWR | O_CREAT, |
fd = open(*argv, O_RDWR | O_CREAT, 0666); |
114 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH |
if (fd >= 0) { |
115 |
); |
xclose(fd); |
116 |
if ((fd >= 0) && !close(fd)) { |
if (reference_file || date_str) |
117 |
if (reference_file) |
utimes(*argv, timebuf); |
|
utime(*argv, &timebuf); |
|
118 |
continue; |
continue; |
119 |
} |
} |
120 |
} |
} |