changeset 162:0864aec90026

Add an option to let touch extend or truncate a file and rename the err label to time_error to reduce confusion.
author Charlie Shepherd <masterdriverz@gentoo.org>
date Sat, 10 Nov 2007 10:03:01 +0000
parents b4c79ab09f9e
children a913f894b7d8
files toys/Config.in toys/touch.c toys/toylist.h
diffstat 3 files changed, 21 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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.
--- 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 <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 @@
 		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);
 	}
 
--- 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))