blob: 8dac1029474e7ad7152ee75419c7573bcb01ad7e [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
31const 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) {
43 fprintf(stderr, "Usage: %s %s", *argv, touch_usage);
44 exit( FALSE);
45 }
46 argc--;
47 argv++;
48
49 /* Parse options */
50 while (**argv == '-') {
51 while (*++(*argv)) switch (**argv) {
52 case 'c':
53 create = FALSE;
54 break;
55 default:
56 fprintf(stderr, "Unknown option: %c\n", **argv);
57 exit( FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000058 }
Eric Andersen596e5461999-10-07 08:30:23 +000059 argc--;
60 argv++;
61 }
62
63 fd = open (*argv, (create==FALSE)? O_RDWR : O_RDWR | O_CREAT, 0644);
64 if (fd < 0 ) {
65 if (create==FALSE && errno == ENOENT)
66 exit( TRUE);
67 else {
68 perror("touch");
69 exit( FALSE);
70 }
71 }
72 close( fd);
73 if (utime (*argv, NULL)) {
74 perror("touch");
75 exit( FALSE);
76 }
77 else
78 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000079}
Eric Andersen596e5461999-10-07 08:30:23 +000080
81
82
83
84
85