blob: d882a63193192357bab41a67d7671cd374eb3090 [file] [log] [blame]
Eric Andersen596e5461999-10-07 08:30:23 +00001/*
2 * Mini touch implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
Eric Andersencc8ed391999-10-05 16:24:54 +000021
Eric Andersen596e5461999-10-07 08:30:23 +000022#include "internal.h"
23#include <stdio.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <utime.h>
28#include <errno.h>
29
30
Eric Andersene77ae3a1999-10-19 20:03:34 +000031static const char touch_usage[] = "touch [-c] file [file ...]\n\n"
Eric Andersencc8ed391999-10-05 16:24:54 +000032"\tUpdate the last-modified date on the given file[s].\n";
33
Eric Andersen596e5461999-10-07 08:30:23 +000034
35
36extern int
37touch_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000038{
Eric Andersen596e5461999-10-07 08:30:23 +000039 int fd;
40 int create=TRUE;
41
42 if (argc < 2) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000043 usage( touch_usage);
Eric Andersen596e5461999-10-07 08:30:23 +000044 }
45 argc--;
46 argv++;
47
48 /* Parse options */
49 while (**argv == '-') {
50 while (*++(*argv)) switch (**argv) {
51 case 'c':
52 create = FALSE;
53 break;
54 default:
55 fprintf(stderr, "Unknown option: %c\n", **argv);
56 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000057 }
Eric Andersen596e5461999-10-07 08:30:23 +000058 argc--;
59 argv++;
60 }
61
62 fd = open (*argv, (create==FALSE)? O_RDWR : O_RDWR | O_CREAT, 0644);
63 if (fd < 0 ) {
64 if (create==FALSE && errno == ENOENT)
65 exit( TRUE);
66 else {
67 perror("touch");
68 exit( FALSE);
69 }
70 }
71 close( fd);
72 if (utime (*argv, NULL)) {
73 perror("touch");
74 exit( FALSE);
75 }
76 else
77 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000078}
Eric Andersen596e5461999-10-07 08:30:23 +000079
80
81
82
83
84