Mercurial > hg > toybox
annotate toys/posix/touch.c @ 702:9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 16 Nov 2012 18:01:35 -0600 |
parents | 7b316e3c0e11 |
children | dcc6136e6659 |
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 | |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
8 USE_TOUCH(NEWTOY(touch, "mr:t:", TOYFLAG_BIN)) |
699 | 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 | |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
21 #define FOR_touch |
699 | 22 #include "toys.h" |
23 | |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
24 GLOBALS( |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
25 char *date; |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
26 char *file; |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
27 ) |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
28 |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
29 void touch_main(void) |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
30 { |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
31 int fd; |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
32 time_t now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
33 struct utimbuf modinfo; |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
34 struct stat st; |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
35 |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
36 if (TT.date) { |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
37 struct tm *tm = getdate(TT.date); |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
38 |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
39 if (!tm) perror_exit("bad date '%s'", TT.date); |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
40 now = mktime(tm); |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
41 } else time(&now); |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
42 modinfo.modtime = now; |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
43 modinfo.actime = now; |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
44 |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
45 if (TT.file) { |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
46 xstat(TT.file, &st); |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
47 modinfo.modtime = st.st_mtime; |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
48 modinfo.actime = st.st_atime; |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
49 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
50 |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
51 if (toys.optflags & FLAG_m) { |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
52 if(stat(toys.optargs[toys.optc - 1], &st) < 0) { |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
53 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
54 return; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
55 } |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
56 modinfo.actime = st.st_atime; |
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
57 if(!(toys.optflags & (FLAG_r|FLAG_t))) { |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
58 time(&now); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
59 modinfo.modtime = now; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
60 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
61 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
62 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
|
63 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
|
64 close(fd); |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
65 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
|
66 } else { |
702
9fb5fe6eb13f
More touch cleanup to use generic infrastructure: use getdate() from libc, use flag macros, option parsing can collect argument strings in global block, use existing perror_* macros.
Rob Landley <rob@landley.net>
parents:
701
diff
changeset
|
67 perror_msg("can't create '%s'", toys.optargs[toys.optc-1]); |
700
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
68 toys.exitval = EXIT_FAILURE; |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
69 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
70 } |
ad6bff0c9169
Refactor touch (cleanup whitespace, brackets, function order), code otherwise same.
Rob Landley <rob@landley.net>
parents:
699
diff
changeset
|
71 } |