annotate toys/touch.c @ 147:ec6e13b2495d

Patch from Charlie Shepherd: Implement touch, set the default in Config.in to y, and remove the length argument (and all talk of changing the length of files).
author Rob Landley <rob@landley.net>
date Wed, 31 Oct 2007 01:13:36 -0500
parents de3b6d914468
children 2eb41e7bf180
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4: */
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 /*
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
3 * touch.c - Modify a file's timestamps.
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
4 *
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
5 * Copyright (C) 2007 Charlie Shepherd <masterdriverz@gentoo.org>
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 */
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
7
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
8 #define _XOPEN_SOURCE
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
9 #include <sys/stat.h>
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
10 #include <utime.h>
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
11 #include <time.h>
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 #include "toys.h"
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
13
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
14 #define MTIME 0x01
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
15 #define NO_CREATE 0x02
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
16 #define ATIME 0x04
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
17 #define REFERENCE 0x08
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
18 #define TIME 0x10
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
19
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 int touch_main(void)
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 {
147
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
22 char *arg;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
23 int i, set_a, set_m, create;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
24 time_t curr_a, curr_m;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
25
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
26 set_a = !!(toys.optflags & ATIME);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
27 set_m = !!(toys.optflags & MTIME);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
28 create = !(toys.optflags & NO_CREATE);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
29
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
30 if (toys.optflags & REFERENCE) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
31 struct stat sb;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
32 if (toys.optflags & TIME) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
33 fprintf(stderr,
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
34 "Cannot specify times from more than one source\n");
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
35 return 1;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
36 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
37 if (stat(toy.touch.ref_file, &sb) == -1) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
38 perror(toy.touch.ref_file);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
39 return 1;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
40 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
41 curr_m = sb.st_mtime;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
42 curr_a = sb.st_atime;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
43 } else if (toys.optflags & TIME) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
44 struct tm t;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
45 time_t curr;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
46 char *c;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
47 curr = time(NULL);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
48 if (!localtime_r(&curr, &t))
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
49 goto err;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
50 c = strptime(toy.touch.time, "%m%d%H%M", &t);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
51 if (!c || *c)
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
52 goto err;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
53 curr_a = curr_m = mktime(&t);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
54 if (curr_a == -1) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
55 err:
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
56 fprintf(stderr, "Error converting time %s to internal format",
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
57 toy.touch.time);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
58 return 1;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
59 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
60 } else {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
61 curr_m = curr_a = time(NULL);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
62 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
63
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
64 for (i = 0; (arg = toys.optargs[i]); i++) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
65 struct utimbuf buf;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
66 struct stat sb;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
67
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
68 buf.modtime = curr_m;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
69 buf.actime = curr_a;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
70
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
71 if (stat(arg, &sb) == -1) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
72 if (create && errno == ENOENT) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
73 if (creat(arg, O_RDWR))
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
74 goto error;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
75 if (stat(arg, &sb))
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
76 goto error;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
77 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
78 } else {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
79 error:
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
80 perror(arg);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
81 return 1;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
82 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
83
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
84 if ((set_a+set_m) == 1) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
85 /* We've been asked to only change one */
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
86 if (set_a)
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
87 buf.modtime = sb.st_mtime;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
88 else if (set_m)
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
89 buf.actime = sb.st_atime;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
90 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
91
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
92 if (utime(arg, &buf)) {
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
93 perror(arg);
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
94 return 1;
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
95 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
96 }
ec6e13b2495d Patch from Charlie Shepherd: Implement touch, set the default in Config.in to
Rob Landley <rob@landley.net>
parents: 130
diff changeset
97
130
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 return 0;
de3b6d914468 Add a dummy "touch" to make it all build again.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 }