source: vendor/emx/current/src/utils/touch.c

Last change on this file was 18, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.6 KB
Line 
1/* touch.c -- Update the time stamp of files
2 Copyright (c) 1994-1995 by Eberhard Mattes
3
4This file is part of emx.
5
6emx is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11emx is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with emx; see the file COPYING. If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
20
21
22#include <sys/types.h>
23#include <sys/utime.h>
24#include <sys/stat.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <io.h>
28#include <fcntl.h>
29#include <getopt.h>
30
31#define FALSE 0
32#define TRUE 1
33
34static void usage (void)
35{
36 fputs ("Usage: touch [-c] [-r <ref_file>] <files>\n\n"
37 "Options:\n"
38 " -c Don't create files\n"
39 " -r <ref_file> Use time and date of <ref_file>\n", stderr);
40 exit (1);
41}
42
43
44int main (int argc, char *argv[])
45{
46 int c, i, h, rc;
47 int dont_create;
48 const char *ref_fname;
49 struct utimbuf *ut;
50 char *s;
51
52 _response (&argc, &argv);
53 _wildcard (&argc, &argv);
54 if (argc < 2)
55 usage ();
56 dont_create = FALSE; ref_fname = NULL; opterr = FALSE;
57 while ((c = getopt (argc, argv, "cr:")) != -1)
58 switch (c)
59 {
60 case 'c':
61 dont_create = TRUE;
62 break;
63 case 'r':
64 ref_fname = optarg;
65 break;
66 default:
67 usage ();
68 }
69 if (optind >= argc)
70 usage ();
71
72 if (ref_fname == NULL)
73 ut = NULL;
74 else
75 {
76 struct stat st;
77 static struct utimbuf ut_ref;
78
79 if (stat (ref_fname, &st) != 0)
80 {
81 perror (ref_fname);
82 exit (1);
83 }
84 ut_ref.actime = st.st_atime;
85 ut_ref.modtime = st.st_mtime;
86 ut = &ut_ref;
87 }
88
89 rc = 0;
90 for (i = optind; i < argc; ++i)
91 {
92 s = argv[i];
93 if (utime (s, ut) != 0 && !dont_create)
94 {
95 h = open (s, O_CREAT|O_EXCL|O_WRONLY|O_BINARY, S_IREAD|S_IWRITE);
96 if (h < 0)
97 {
98 perror (s);
99 rc = 1;
100 }
101 else
102 {
103 close (h);
104 if (ut != NULL && utime (s, ut) != 0)
105 {
106 perror (s);
107 rc = 1;
108 }
109 }
110 }
111 }
112 return rc;
113}
Note: See TracBrowser for help on using the repository browser.