# HG changeset patch # User Charlie Shepherd # Date 1194688981 0 # Node ID 0864aec9002604e2f4a4b467407f61d6ce314f32 # Parent b4c79ab09f9ef3182684403b789820f48d9db125 Add an option to let touch extend or truncate a file and rename the err label to time_error to reduce confusion. diff -r b4c79ab09f9e -r 0864aec90026 toys/Config.in --- a/toys/Config.in Sat Nov 10 10:01:28 2007 +0000 +++ b/toys/Config.in Sat Nov 10 10:03:01 2007 +0000 @@ -261,9 +261,9 @@ bool "touch" default y help - usage: touch [-acmrt] FILE... + usage: touch [-acmrtl] FILE... - Change file timestamps and ensure file existance. + Change file timestamps, ensure file existance and change file length. -a Only change the access time. -c Do not create the file if it doesn't exist. diff -r b4c79ab09f9e -r 0864aec90026 toys/touch.c --- a/toys/touch.c Sat Nov 10 10:01:28 2007 +0000 +++ b/toys/touch.c Sat Nov 10 10:03:01 2007 +0000 @@ -5,7 +5,9 @@ * Copyright (C) 2007 Charlie Shepherd */ -#define _XOPEN_SOURCE +#define _XOPEN_SOURCE 600 +#include +#include #include #include #include @@ -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 @@ 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 @@ buf.actime = sb.st_atime; } + if (length != -1) + if (truncate(arg, length)) + goto error; if (utime(arg, &buf)) +error: perror_exit(arg); } diff -r b4c79ab09f9e -r 0864aec90026 toys/toylist.h --- a/toys/toylist.h Sat Nov 10 10:01:28 2007 +0000 +++ b/toys/toylist.h Sat Nov 10 10:03:01 2007 +0000 @@ -55,6 +55,7 @@ struct touch_data { char *ref_file; char *time; + long length; }; struct toysh_data { @@ -116,7 +117,7 @@ USE_TOYSH(OLDTOY(sh, toysh, "c:i", TOYFLAG_BIN)) USE_SLEEP(NEWTOY(sleep, "<1", TOYFLAG_BIN)) USE_SYNC(NEWTOY(sync, NULL, TOYFLAG_BIN)) -USE_TOUCH(NEWTOY(touch, "t:r:mca", TOYFLAG_BIN)) +USE_TOUCH(NEWTOY(touch, "l#t:r:mca", TOYFLAG_BIN)) USE_TOYSH(NEWTOY(toysh, "c:i", TOYFLAG_BIN)) USE_TRUE(NEWTOY(true, NULL, TOYFLAG_BIN)) USE_WHICH(NEWTOY(which, "a", TOYFLAG_USR|TOYFLAG_BIN))