ReactOS  0.4.15-dev-1018-g0695ecb
touch.c File Reference
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/utime.h>
#include <io.h>
#include <fcntl.h>
#include <getopt.h>
#include <various.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for touch.c:

Go to the source code of this file.

Macros

#define DEFFILEMODE   S_IWRITE
 
#define ATOI2(ar)   ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
 

Functions

int rw __P ((char *, struct stat *, int))
 
void stime_arg1 __P ((char *, time_t *))
 
void stime_arg2 __P ((char *, int, time_t *))
 
void usage __P ((void))
 
int main (int argc, char *argv[])
 
void stime_arg1 (char *arg, time_t *tvp)
 

Variables

char copyright []
 
char__progname
 

Macro Definition Documentation

◆ ATOI2

#define ATOI2 (   ar)    ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;

Definition at line 197 of file touch.c.

◆ DEFFILEMODE

#define DEFFILEMODE   S_IWRITE

Definition at line 58 of file touch.c.

Function Documentation

◆ __P() [1/4]

int rw __P ( (char *, struct stat *, int )

◆ __P() [2/4]

void stime_file __P ( (char *, time_t *)  )

◆ __P() [3/4]

void stime_arg2 __P ( (char *, int, time_t *)  )

◆ __P() [4/4]

void usage __P ( (void )

◆ main()

int main ( int argc  ,
char argv[] 
)

hosttype.c Copyright (C) 2002 by Brian Palmer brian.nosp@m.p@sg.nosp@m.inet..nosp@m.com

Definition at line 78 of file touch.c.

79 {
80  struct stat sb;
81  time_t tv[2];
82  int aflag, cflag, fflag, mflag, ch, fd, len, rval, timeset;
83  char *p;
84  struct utimbuf utb;
85 
86  __progname = argv[0];
87 
88  aflag = cflag = fflag = mflag = timeset = 0;
89  time(&tv[0]);
90 
91  while ((ch = getopt(argc, argv, "acfmr:t:")) != EOF)
92  switch(ch) {
93  case 'a':
94  aflag = 1;
95  break;
96  case 'c':
97  cflag = 1;
98  break;
99  case 'f':
100  fflag = 1;
101  break;
102  case 'm':
103  mflag = 1;
104  break;
105  case 'r':
106  timeset = 1;
107  stime_file(optarg, tv);
108  break;
109  case 't':
110  timeset = 1;
111  stime_arg1(optarg, tv);
112  break;
113  case '?':
114  default:
115  usage();
116  }
117  argc -= optind;
118  argv += optind;
119 
120  /* Default is both -a and -m. */
121  if (aflag == 0 && mflag == 0)
122  aflag = mflag = 1;
123 
124  /*
125  * If no -r or -t flag, at least two operands, the first of which
126  * is an 8 or 10 digit number, use the obsolete time specification.
127  */
128  if (!timeset && argc > 1) {
129  (void)strtol(argv[0], &p, 10);
130  len = p - argv[0];
131  if (*p == '\0' && (len == 8 || len == 10)) {
132  timeset = 1;
133  stime_arg2(argv[0], len == 10, tv);
134  }
135  }
136 
137  /* Otherwise use the current time of day. */
138  if (!timeset)
139  tv[1] = tv[0];
140 
141  if (*argv == NULL)
142  usage();
143 
144  for (rval = 0; *argv; ++argv) {
145  /* See if the file exists. */
146  if (stat(*argv, &sb)) {
147  if (!cflag) {
148  /* Create the file. */
149  fd = _open(*argv,
151  if (fd == -1 || fstat(fd, &sb) || _close(fd)) {
152  rval = 1;
153  warn("%s", *argv);
154  continue;
155  }
156 
157  /* If using the current time, we're done. */
158  if (!timeset)
159  continue;
160  } else
161  continue;
162  }
163 
164  if (!aflag)
165  tv[0] = sb.st_atime;
166  if (!mflag)
167  tv[1] = sb.st_mtime;
168 
169  /* Try utime. */
170  utb.actime = tv[0];
171  utb.modtime = tv[1];
172  if (!utime(*argv, &utb))
173  continue;
174 
175  /* If the user specified a time, nothing else we can do. */
176  if (timeset) {
177  rval = 1;
178  warn("%s", *argv);
179  }
180 
181  /*
182  * System V and POSIX 1003.1 require that a NULL argument
183  * set the access/modification times to the current time.
184  * The permission checks are different, too, in that the
185  * ability to write the file is sufficient. Take a shot.
186  */
187  if (!utime(*argv, NULL))
188  continue;
189 
190  /* Try reading/writing. */
191  if (rw(*argv, &sb, fflag))
192  rval = 1;
193  }
194  return rval;
195 }
static int argc
Definition: ServiceArgs.c:12
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define warn(...)
superblock * sb
Definition: btrfs.c:4220
int optind
Definition: getopt.c:47
static int fd
Definition: io.c:51
__u16 time
Definition: mkdosfs.c:366
int mflag
Definition: finger.c:66
#define argv
Definition: mplay32.c:18
__CRT_INLINE int __cdecl utime(const char *_Filename, struct utimbuf *_Utimbuf)
Definition: utime.h:136
#define O_WRONLY
Definition: acwin.h:111
smooth NULL
Definition: ftsmooth.c:416
_Check_return_opt_ _CRTIMP int __cdecl _close(_In_ int _FileHandle)
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
int rw
float rval
Definition: cylfrac.c:48
GLsizeiptr const GLvoid GLenum usage
Definition: glext.h:5919
Definition: stat.h:55
GLenum GLsizei len
Definition: glext.h:6722
_CRTIMP int __cdecl stat(const char *_Filename, struct stat *_Stat)
Definition: stat.h:345
void stime_arg1(char *arg, time_t *tvp)
Definition: touch.c:200
__kernel_time_t time_t
Definition: linux.h:252
const char * optarg
Definition: getopt.c:49
_CRTIMP int __cdecl _open(const char *_Filename, int _OpenFlag,...)
Definition: file.c:2001
#define EOF
Definition: stdio.h:24
_Check_return_ long __cdecl strtol(_In_z_ const char *_Str, _Out_opt_ _Deref_post_z_ char **_EndPtr, _In_ int _Radix)
_CRTIMP int __cdecl fstat(int _Desc, struct stat *_Stat)
Definition: stat.h:341
GLfloat GLfloat p
Definition: glext.h:8902
char * __progname
Definition: touch.c:68
#define DEFFILEMODE
Definition: touch.c:58
#define O_CREAT
Definition: acwin.h:110

◆ stime_arg1()

void stime_arg1 ( char arg,
time_t tvp 
)

Definition at line 200 of file touch.c.

Referenced by main().

Variable Documentation

◆ __progname

char* __progname

Definition at line 68 of file touch.c.

Referenced by main(), verr(), verrx(), vwarn(), and vwarnx().

◆ copyright

char copyright[]
Initial value:
=
"@(#) Copyright (c) 1993 Regents of the University of California.\n\
All rights reserved.\n"

Definition at line 40 of file touch.c.