Added sfdisk. Ststic-ified a bunch of stuff.
[oweals/busybox.git] / coreutils / touch.c
1 /*
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  */
21
22 #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
31 static const char touch_usage[] = "touch [-c] file [file ...]\n\n"
32 "\tUpdate the last-modified date on the given file[s].\n";
33
34
35
36 extern int 
37 touch_main(int argc, char **argv)
38 {
39     int fd;
40     int create=TRUE;
41
42     if (argc < 2) {
43         usage( touch_usage);
44     }
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);
57         }
58         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);
78 }
79
80
81
82
83
84