aboutsummaryrefslogtreecommitdiff
path: root/toys/touch.c
diff options
context:
space:
mode:
authorCharlie Shepherd <masterdriverz@gentoo.org>2007-11-10 10:03:01 +0000
committerCharlie Shepherd <masterdriverz@gentoo.org>2007-11-10 10:03:01 +0000
commit627dc77bb6749dbb2cb41d2bbca0c8e41c7504ca (patch)
tree7f3109dbd4b0b16e535e2933628f18c8e7424134 /toys/touch.c
parentded91bd8208b1d8a1c7b145f3ee5741b6ad57591 (diff)
downloadtoybox-627dc77bb6749dbb2cb41d2bbca0c8e41c7504ca.tar.gz
Add an option to let touch extend or truncate a file and rename the err label to time_error to reduce confusion.
Diffstat (limited to 'toys/touch.c')
-rw-r--r--toys/touch.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/toys/touch.c b/toys/touch.c
index b4e3d971..514ee1be 100644
--- a/toys/touch.c
+++ b/toys/touch.c
@@ -5,7 +5,9 @@
* Copyright (C) 2007 Charlie Shepherd <masterdriverz@gentoo.org>
*/
-#define _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#include <unistd.h>
+#include <sys/types.h>
#include <sys/stat.h>
#include <utime.h>
#include <time.h>
@@ -16,17 +18,24 @@
#define ATIME 0x04
#define REFERENCE 0x08
#define TIME 0x10
+#define LENGTH 0x20
int touch_main(void)
{
char *arg;
int i, set_a, set_m, create;
time_t curr_a, curr_m;
+ off_t length;
set_a = !!(toys.optflags & ATIME);
set_m = !!(toys.optflags & MTIME);
create = !(toys.optflags & NO_CREATE);
+ if (toys.optflags & LENGTH)
+ length = toy.touch.length;
+ else
+ length = -1;
+
if (toys.optflags & REFERENCE) {
struct stat sb;
if (toys.optflags & TIME)
@@ -40,13 +49,13 @@ int touch_main(void)
char *c;
curr = time(NULL);
if (!localtime_r(&curr, &t))
- goto err;
+ goto time_error;
c = strptime(toy.touch.time, "%m%d%H%M", &t);
if (!c || *c)
- goto err;
+ goto time_error;
curr_a = curr_m = mktime(&t);
if (curr_a == -1)
-err:
+time_error:
error_exit("Error converting time %s to internal format",
toy.touch.time);
} else {
@@ -77,7 +86,11 @@ err:
buf.actime = sb.st_atime;
}
+ if (length != -1)
+ if (truncate(arg, length))
+ goto error;
if (utime(arg, &buf))
+error:
perror_exit(arg);
}