home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / futi14as.zip / TOUCH.C < prev   
C/C++ Source or Header  |  1992-02-22  |  10KB  |  446 lines

  1. /* touch -- change modification and access times of files
  2.    Copyright (C) 1987, 1989, 1990, Free Software Foundation Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.  */
  26.  
  27. #ifdef MSDOS
  28. static char RCS_Id[] =
  29.   "$Header: e:/gnu/fileutil/RCS/touch.c 1.4.0.2 90/09/19 12:09:27 tho Exp $";
  30.  
  31. static char Program_Id[] = "touch";
  32. static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
  33.  
  34. #define VERSION \
  35.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  36.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  37.  
  38. #define COPYING \
  39.   "This is free software, distributed under the terms of the\n" \
  40.   "GNU General Public License.  For details, see the file COPYING.\n"
  41. #endif /* MSDOS */
  42.  
  43. /* Usage: touch [-acm] [-r reference-file] [-t MMDDhhmm[[CC]YY][.ss]]
  44.           [-d time] [+time {atime,access,use,mtime,modify}] [+date time]
  45.           [+file reference-file] [+no-create] file...
  46.  
  47.    Options:
  48.    -a, +time={atime,access,use}    Change access time only.
  49.    -c, +no-create        Do not create files that do not exist.
  50.    -d, +date TIME        Specify time and date in various formats.
  51.    -m, +time={mtime,modify}    Change modification time only.
  52.    -r, +file FILE        Use the time and date of reference file FILE.
  53.    -t TIME            Specify time and date in the form
  54.                 `MMDDhhmm[[CC]YY][.ss]'.
  55.    
  56.    If no options are given, -am is the default, using the current time.
  57.    The -r, -t, and -d options are mutually exclusive.  If a file does not
  58.    exist, create it unless -c is given.
  59.  
  60.    Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
  61.    and Randy Smith. */
  62.  
  63. #include <stdio.h>
  64. #include <ctype.h>
  65. #include <getopt.h>
  66. #include <errno.h>
  67. #include <sys/types.h>
  68. #include "system.h"
  69.  
  70. #ifdef STDC_HEADERS
  71. #include <stdlib.h>
  72. #include <time.h>
  73. #else
  74. char *malloc ();
  75. char *realloc ();
  76. time_t mktime ();
  77. time_t time ();
  78.  
  79. extern int errno;
  80. #endif
  81.  
  82. #ifndef _POSIX_SOURCE
  83. long lseek ();
  84. #endif
  85.  
  86.  
  87. #ifdef MSDOS
  88.  
  89. #include <io.h>
  90. #include <gnulib.h>
  91.  
  92. extern int argmatch (char *arg, char **optlist);
  93. extern void invalid_arg (char *kind, char *value, int problem);
  94. extern time_t posixtime (char *s);
  95. extern time_t getdate (char *p, struct timeb *now);
  96. void main (int argc, char **argv);
  97. int touch (char *file);
  98. void usage (void);
  99.  
  100. #else /* not MSDOS */
  101.  
  102. int argmatch ();
  103. int touch ();
  104. time_t getdate ();
  105. time_t posixtime ();
  106. void error ();
  107. void invalid_arg ();
  108. void usage ();
  109.  
  110. #endif /* not MSDOS */
  111.  
  112. /* Bitmasks for `change_times'. */
  113. #define CH_ATIME 1
  114. #define CH_MTIME 2
  115.  
  116. /* Which timestamps to change. */
  117. int change_times;
  118.  
  119. /* (-c) If nonzero, don't create if not already there. */
  120. int no_create;
  121.  
  122. /* (-d) If nonzero, date supplied on command line in getdate formats. */
  123. int flexible_date;
  124.  
  125. /* (-r) If nonzero, use times from a reference file. */
  126. int use_ref;
  127.  
  128. /* (-t) If nonzero, date supplied on command line in POSIX format. */
  129. int posix_date;
  130.  
  131. /* If nonzero, the only thing we have to do is change both the
  132.    modification and access time to the current time, so we don't
  133.    have to own the file, just be able to read and write it.  */
  134. int amtime_now;
  135.  
  136. /* New time to use when setting time. */
  137. time_t newtime;
  138.  
  139. /* File to use for -r. */
  140. char *ref_file;
  141.  
  142. /* Info about the reference file. */
  143. struct stat ref_stats;
  144.  
  145. /* The name by which this program was run. */
  146. char *program_name;
  147.  
  148. struct option longopts[] =
  149. {
  150. #ifdef MSDOS
  151.   {"copying", 0, NULL, 30},
  152.   {"version", 0, NULL, 31},
  153. #endif
  154.   {"time", 1, 0, 130},
  155.   {"no-create", 0, 0, 'c'},
  156.   {"date", 1, 0, 'd'},
  157.   {"file", 1, 0, 'r'},
  158.   {0, 0, 0, 0}
  159. };
  160.  
  161. /* Valid arguments to the `+time' option. */
  162. char *time_args[] =
  163. {
  164.   "atime", "access", "use", "mtime", "modify", 0
  165. };
  166.  
  167. /* The bits in `change_times' that those arguments set. */
  168. int time_masks[] =
  169. {
  170.   CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
  171. };
  172.  
  173. void
  174. main (argc, argv)
  175.      int argc;
  176.      char **argv;
  177. {
  178.   int c;
  179.   int longind;
  180.   int date_set = 0;
  181.   int err = 0;
  182.  
  183.   program_name = argv[0];
  184.   change_times = no_create = use_ref = posix_date = flexible_date = 0;
  185.   newtime = (time_t) -1;
  186.  
  187.   while ((c = getopt_long (argc, argv, "acd:mr:t:", longopts, &longind))
  188.      != EOF)
  189.     {
  190.       switch (c)
  191.     {
  192.     case 'a':
  193.       change_times |= CH_ATIME;
  194.       break;
  195.  
  196.     case 'c':
  197.       no_create++;
  198.       break;
  199.  
  200.     case 'd':
  201.       flexible_date++;
  202.       newtime = getdate (optarg, NULL);
  203.       if (newtime == (time_t) -1)
  204.         error (1, 0, "invalid date format `%s'", optarg);
  205.       date_set++;
  206.       break;
  207.  
  208.     case 'm':
  209.       change_times |= CH_MTIME;
  210.       break;
  211.  
  212.     case 'r':
  213.       use_ref++;
  214.       ref_file = optarg;
  215.       break;
  216.  
  217.     case 't':
  218.       posix_date++;
  219.       newtime = posixtime (optarg);
  220.       if (newtime == (time_t) -1)
  221.         error (1, 0, "invalid date format `%s'", optarg);
  222.       date_set++;
  223.       break;
  224.  
  225.     case 130:
  226.       longind = argmatch (optarg, time_args);
  227.       if (longind < 0)
  228.         {
  229.           invalid_arg ("time selector", optarg, longind);
  230.           usage ();
  231.         }
  232.       change_times |= time_masks[longind];
  233.       break;
  234.  
  235. #ifdef MSDOS
  236.     case 30:
  237.       fprintf (stderr, COPYING);
  238.       exit (0);
  239.       break;
  240.  
  241.     case 31:
  242.       fprintf (stderr, VERSION);
  243.       exit (0);
  244.       break;
  245. #endif
  246.  
  247.     default:
  248.       usage ();
  249.     }
  250.     }
  251.  
  252.   if (change_times == 0)
  253.     change_times = CH_ATIME | CH_MTIME;
  254.  
  255.   if ((use_ref && (posix_date || flexible_date))
  256.       || (posix_date && flexible_date))
  257.     {
  258.       error (0, 0, "cannot specify times from more than one source");
  259.       usage ();
  260.     }
  261.  
  262.   if (use_ref)
  263.     {
  264.       if (stat (ref_file, &ref_stats))
  265.     error (1, errno, "%s", ref_file);
  266.       date_set++;
  267.     }
  268.  
  269.   if (!date_set && optind < argc && strcmp (argv[optind - 1], "--"))
  270.     {
  271.       newtime = posixtime (argv[optind]);
  272.       if (newtime != (time_t) -1)
  273.     {
  274.       optind++;
  275.       date_set++;
  276.     }
  277.     }
  278.   if (!date_set)
  279.     {
  280.       if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
  281.     amtime_now = 1;
  282.       else
  283.     time (&newtime);
  284.     }
  285.  
  286.   if (optind == argc)
  287.     {
  288.       error (0, 0, "file arguments missing");
  289.       usage ();
  290.     }
  291.  
  292.   for (; optind < argc; ++optind)
  293.     err += touch (argv[optind]);
  294.  
  295.   exit (err != 0);
  296. }
  297.  
  298. /* Update the time of file FILE according to the options given.
  299.    Return 0 if successful, 1 if an error occurs. */
  300.  
  301. int
  302. touch (file)
  303.      char *file;
  304. {
  305.   int status;
  306.   struct stat sbuf;
  307.   int fd;
  308.  
  309.   if (stat (file, &sbuf))
  310.     {
  311.       if (errno != ENOENT)
  312.     {
  313.       error (0, errno, "%s", file);
  314.       return 1;
  315.     }
  316.       if (no_create)
  317.     return 0;
  318.       fd = creat (file, 0666);
  319.       if (fd == -1)
  320.     {
  321.       error (0, errno, "%s", file);
  322.       return 1;
  323.     }
  324.       if (amtime_now)
  325.     {
  326.       close (fd);
  327.       return 0;        /* We've done all we have to. */
  328.     }
  329.       if (fstat (fd, &sbuf))
  330.     {
  331.       error (0, errno, "%s", file);
  332.       close (fd);
  333.       return 1;
  334.     }
  335.       close (fd);
  336.     }
  337.   else if ((sbuf.st_mode & S_IFMT) != S_IFREG)
  338.     {
  339.       error (0, 0, "`%s' is not a regular file", file);
  340.       return 1;
  341.     }
  342.  
  343.   if (amtime_now)
  344.     {
  345.       /* Need to pass NULL to utime so it will not fail if we just have
  346.      write access to the file, but don't own it.  */
  347. #if defined (UTIME_NULL_MISSING)
  348.       /* This system won't take utime (file, NULL) (e.g., BSD4.3).
  349.      Emulate it.  */
  350.       int fd;
  351.       char c;
  352.  
  353.       status = 0;
  354.       fd = open (file, O_RDWR, 0666);
  355.       if (fd < 0
  356.       || read (fd, &c, sizeof (char)) < 0
  357.       || lseek (fd, 0, L_SET) < 0
  358.       || write (fd, &c, sizeof (char)) < 0
  359.       || ftruncate (fd, sbuf.st_size) < 0)
  360.     status = -1;
  361.       if (fd >= 0)
  362.     close (fd);
  363. #else
  364.       status = utime (file, NULL);
  365. #endif
  366.     }
  367.   else
  368.     {
  369.       struct utimbuf utb;
  370.  
  371.       if (use_ref)
  372.     {
  373.       utb.actime = ref_stats.st_atime;
  374.       utb.modtime = ref_stats.st_mtime;
  375.     }
  376.       else
  377.     utb.actime = utb.modtime = newtime;
  378.  
  379.       if (!(change_times & CH_ATIME))
  380.     utb.actime = sbuf.st_atime;
  381.  
  382.       if (!(change_times & CH_MTIME))
  383.     utb.modtime = sbuf.st_mtime;
  384.  
  385.       status = utime (file, &utb);
  386.     }
  387.   
  388.   if (status)
  389.     {
  390.       error (0, errno, "%s", file);
  391.       return 1;
  392.     }
  393.  
  394.   return 0;
  395. }
  396.  
  397. /*  Use the one from gnulib */
  398. #ifndef MSDOS
  399.  
  400. /* Like malloc but get error if no storage available. */
  401.  
  402. char *
  403. xmalloc (size)
  404.      unsigned size;
  405. {
  406.   register char *val = (char *) malloc (size);
  407.  
  408.   if (!val)
  409.     error (1, 0, "virtual memory exhausted");
  410.   return val;
  411. }
  412.  
  413. /* Like realloc but get error if no storage available.  */
  414.  
  415. char *
  416. xrealloc (ptr, size)
  417.      char *ptr;
  418.      unsigned size;
  419. {
  420.   register char *val = (char *) realloc (ptr, size);
  421.  
  422.   if (!val)
  423.     error (1, 0, "virtual memory exhausted");
  424.   return val;
  425. }
  426.  
  427. #endif /* not MSDOS */
  428.  
  429. void
  430. usage ()
  431. {
  432. #ifdef MSDOS
  433.   fprintf (stderr, "\
  434. Usage: %s [-acm] [-r reference-file] [-t MMDDhhmm[[CC]YY][.ss]]\n\
  435.        [-d time] [+time {atime,access,use,mtime,modify}] [+date time]\n\
  436.        [+file reference-file] [+no-create] [+copying] [+version] file...\n",
  437. #else /* not MSDOS */
  438.   fprintf (stderr, "\
  439. Usage: %s [-acm] [-r reference-file] [-t MMDDhhmm[[CC]YY][.ss]]\n\
  440.        [-d time] [+time {atime,access,use,mtime,modify}] [+date time]\n\
  441.        [+file reference-file] [+no-create] file...\n",
  442. #endif /* not MSDOS */
  443.        program_name);
  444.   exit (1);
  445. }
  446.