[Toybox] [PATCH 2/2] Add a "touch" command.
Gaurang Shastri
gmshastri at gmail.com
Wed Aug 15 22:33:49 PDT 2012
Just Minor change before code release to next version [?]
(Please find my change in USE_xx part)
//Gaurang Shastri
On Thu, Aug 16, 2012 at 9:26 AM, Avery Pennarun <apenwarr at gmail.com> wrote:
> Doesn't implement optional manual setting of timestamp; just uses the
> current time, which is the most common use case.
> ---
> toys.h | 1 +
> toys/touch.c | 57
> +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 58 insertions(+), 0 deletions(-)
> create mode 100644 toys/touch.c
>
> diff --git a/toys.h b/toys.h
> index 4a84bf4..2cc58af 100644
> --- a/toys.h
> +++ b/toys.h
> @@ -37,6 +37,7 @@
> #include <sys/statvfs.h>
> #include <sys/sysinfo.h>
> #include <sys/swap.h>
> +#include <sys/time.h>
> #include <sys/times.h>
> #include <sys/types.h>
> #include <sys/utsname.h>
> diff --git a/toys/touch.c b/toys/touch.c
> new file mode 100644
> index 0000000..4bbfacd
> --- /dev/null
> +++ b/toys/touch.c
> @@ -0,0 +1,57 @@
> +/* vi: set sw=4 ts=4:
> + *
> + * touch.c - Create files and update their atime/mtime.
> + *
> + * Copyright 2012 Avery Pennarun <apenwarr at gmail.com>
> + *
> + * See http://www.opengroup.org/onlinepubs/009695399/utilities/touch.html
> + *
> *- USE_CP(NEWTOY(touch, "<1", TOYFLAG_BIN))
> *
* + USE_TOUCH(NEWTOY(touch, "<1", TOYFLAG_BIN))*
> * *+
> +config TOUCH
> + bool "touch"
> + default n
> + help
> + usage: touch <filenames...>
> +
> + Updates the timestamps on the given files, and creates them if
> + they do not exist.
> +*/
> +
> +#include "toys.h"
> +
> +#define TT this.touch
> +
> +
> +void touch_file(char *filename, struct timeval *times)
> +{
> + int fd = -1;
> +
> + if (utimes(filename, times) < 0) {
> + if (errno != ENOENT) {
> + perror_exit("utimes(%s)", filename);
> + } else {
> + fd = open(filename, O_WRONLY|O_CREAT, 0666);
> + if (fd < 0) {
> + perror_exit("open '%s'", filename);
> + }
> + xclose(fd);
> + }
> + }
> +}
> +
> +
> +void touch_main(void)
> +{
> + struct timeval times[2];
> + struct timezone tz;
> + int i;
> +
> + gettimeofday(×[0], &tz);
> + times[1] = times[0];
> +
> + for (i=0; i<toys.optc; i++) {
> + char *filename = toys.optargs[i];
> + touch_file(filename, times);
> + }
> +}
> --
> 1.7.9.dirty
>
> _______________________________________________
> Toybox mailing list
> Toybox at lists.landley.net
> http://lists.landley.net/listinfo.cgi/toybox-landley.net
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20120816/b2c4ab09/attachment-0005.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 347.gif
Type: image/gif
Size: 186 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20120816/b2c4ab09/attachment-0007.gif>
More information about the Toybox
mailing list