home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / touch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  8.1 KB  |  377 lines

  1. /* touch -- change modification and access times of files
  2.    Copyright (C) 1987, 1989, 1990, 1991 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 2, 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. /* Options:
  19.    -a, --time={atime,access,use}    Change access time only.
  20.    -c, --no-create        Do not create files that do not exist.
  21.    -d, --date=TIME        Specify time and date in various formats.
  22.    -f                Ignored.
  23.    -m, --time={mtime,modify}    Change modification time only.
  24.    -r, --file=FILE        Use the time and date of reference file FILE.
  25.    -t TIME            Specify time and date in the form
  26.                 `MMDDhhmm[[CC]YY][.ss]'.
  27.    
  28.    If no options are given, -am is the default, using the current time.
  29.    The -r, -t, and -d options are mutually exclusive.  If a file does not
  30.    exist, create it unless -c is given.
  31.  
  32.    Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
  33.    and Randy Smith. */
  34.  
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include <getopt.h>
  38. #include <sys/types.h>
  39. #include "system.h"
  40. #include "version.h"
  41.  
  42. #ifndef STDC_HEADERS
  43. time_t mktime ();
  44. time_t time ();
  45. #endif
  46.  
  47. int argmatch ();
  48. time_t get_date ();
  49. time_t posixtime ();
  50. void error ();
  51. void invalid_arg ();
  52.  
  53. static int touch ();
  54. static void usage ();
  55. #ifndef HAVE_UTIME_NULL
  56. static int utime_now ();
  57. #endif
  58.  
  59. /* Bitmasks for `change_times'. */
  60. #define CH_ATIME 1
  61. #define CH_MTIME 2
  62.  
  63. /* The name by which this program was run. */
  64. char *program_name;
  65.  
  66. /* Which timestamps to change. */
  67. static int change_times;
  68.  
  69. /* (-c) If nonzero, don't create if not already there. */
  70. static int no_create;
  71.  
  72. /* (-d) If nonzero, date supplied on command line in get_date formats. */
  73. static int flexible_date;
  74.  
  75. /* (-r) If nonzero, use times from a reference file. */
  76. static int use_ref;
  77.  
  78. /* (-t) If nonzero, date supplied on command line in POSIX format. */
  79. static int posix_date;
  80.  
  81. /* If nonzero, the only thing we have to do is change both the
  82.    modification and access time to the current time, so we don't
  83.    have to own the file, just be able to read and write it.  */
  84. static int amtime_now;
  85.  
  86. /* New time to use when setting time. */
  87. static time_t newtime;
  88.  
  89. /* File to use for -r. */
  90. static char *ref_file;
  91.  
  92. /* Info about the reference file. */
  93. static struct stat ref_stats;
  94.  
  95. /* If non-zero, display usage information and exit.  */
  96. static int flag_help;
  97.  
  98. /* If non-zero, print the version on standard error.  */
  99. static int flag_version;
  100.  
  101. static struct option const longopts[] =
  102. {
  103.   {"time", required_argument, 0, 130},
  104.   {"no-create", no_argument, 0, 'c'},
  105.   {"date", required_argument, 0, 'd'},
  106.   {"file", required_argument, 0, 'r'},
  107.   {"help", no_argument, &flag_help, 1},
  108.   {"version", no_argument, &flag_version, 1},
  109.   {0, 0, 0, 0}
  110. };
  111.  
  112. /* Valid arguments to the `--time' option. */
  113. static char const* const time_args[] =
  114. {
  115.   "atime", "access", "use", "mtime", "modify", 0
  116. };
  117.  
  118. /* The bits in `change_times' that those arguments set. */
  119. static int const time_masks[] =
  120. {
  121.   CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
  122. };
  123.  
  124. void
  125. main (argc, argv)
  126.      int argc;
  127.      char **argv;
  128. {
  129.   int c, i;
  130.   int date_set = 0;
  131.   int err = 0;
  132.  
  133.   program_name = argv[0];
  134.   change_times = no_create = use_ref = posix_date = flexible_date = 0;
  135.   newtime = (time_t) -1;
  136.  
  137.   while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, (int *) 0))
  138.      != EOF)
  139.     {
  140.       switch (c)
  141.     {
  142.     case 0:
  143.       break;
  144.  
  145.     case 'a':
  146.       change_times |= CH_ATIME;
  147.       break;
  148.  
  149.     case 'c':
  150.       no_create++;
  151.       break;
  152.  
  153.     case 'd':
  154.       flexible_date++;
  155.       newtime = get_date (optarg, NULL);
  156.       if (newtime == (time_t) -1)
  157.         error (1, 0, "invalid date format `%s'", optarg);
  158.       date_set++;
  159.       break;
  160.  
  161.     case 'f':
  162.       break;
  163.  
  164.     case 'm':
  165.       change_times |= CH_MTIME;
  166.       break;
  167.  
  168.     case 'r':
  169.       use_ref++;
  170.       ref_file = optarg;
  171.       break;
  172.  
  173.     case 't':
  174.       posix_date++;
  175.       newtime = posixtime (optarg);
  176.       if (newtime == (time_t) -1)
  177.         error (1, 0, "invalid date format `%s'", optarg);
  178.       date_set++;
  179.       break;
  180.  
  181.     case 130:
  182.       i = argmatch (optarg, time_args);
  183.       if (i < 0)
  184.         {
  185.           invalid_arg ("time selector", optarg, i);
  186.           usage ();
  187.         }
  188.       change_times |= time_masks[i];
  189.       break;
  190.  
  191.     default:
  192.       usage ();
  193.     }
  194.     }
  195.  
  196.   if (flag_version)
  197.     {
  198.       fprintf (stderr, "%s\n", version_string);
  199.       exit (0);
  200.     }
  201.  
  202.   if (flag_help)
  203.     usage ();
  204.  
  205.   if (change_times == 0)
  206.     change_times = CH_ATIME | CH_MTIME;
  207.  
  208.   if ((use_ref && (posix_date || flexible_date))
  209.       || (posix_date && flexible_date))
  210.     {
  211.       error (0, 0, "cannot specify times from more than one source");
  212.       usage ();
  213.     }
  214.  
  215.   if (use_ref)
  216.     {
  217.       if (stat (ref_file, &ref_stats))
  218.     error (1, errno, "%s", ref_file);
  219.       date_set++;
  220.     }
  221.  
  222.   if (!date_set && optind < argc && strcmp (argv[optind - 1], "--"))
  223.     {
  224.       newtime = posixtime (argv[optind]);
  225.       if (newtime != (time_t) -1)
  226.     {
  227.       optind++;
  228.       date_set++;
  229.     }
  230.     }
  231.   if (!date_set)
  232.     {
  233.       if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
  234.     amtime_now = 1;
  235.       else
  236.     time (&newtime);
  237.     }
  238.  
  239.   if (optind == argc)
  240.     {
  241.       error (0, 0, "file arguments missing");
  242.       usage ();
  243.     }
  244.  
  245.   for (; optind < argc; ++optind)
  246.     err += touch (argv[optind]);
  247.  
  248.   exit (err != 0);
  249. }
  250.  
  251. /* Update the time of file FILE according to the options given.
  252.    Return 0 if successful, 1 if an error occurs. */
  253.  
  254. static int
  255. touch (file)
  256.      char *file;
  257. {
  258.   int status;
  259.   struct stat sbuf;
  260.   int fd;
  261.  
  262.   if (stat (file, &sbuf))
  263.     {
  264.       if (errno != ENOENT)
  265.     {
  266.       error (0, errno, "%s", file);
  267.       return 1;
  268.     }
  269.       if (no_create)
  270.     return 0;
  271.       fd = creat (file, 0666);
  272.       if (fd == -1)
  273.     {
  274.       error (0, errno, "%s", file);
  275.       return 1;
  276.     }
  277.       if (amtime_now)
  278.     {
  279.       if (close (fd) < 0)
  280.         {
  281.           error (0, errno, "%s", file);
  282.           return 1;
  283.         }
  284.       return 0;        /* We've done all we have to. */
  285.     }
  286.       if (fstat (fd, &sbuf))
  287.     {
  288.       error (0, errno, "%s", file);
  289.       close (fd);
  290.       return 1;
  291.     }
  292.       if (close (fd) < 0)
  293.     {
  294.       error (0, errno, "%s", file);
  295.       return 1;
  296.     }    
  297.     }
  298.  
  299.   if (amtime_now)
  300.     {
  301. #ifndef HAVE_UTIME_NULL
  302.       status = utime_now (file, sbuf.st_size);
  303. #else
  304.       /* Pass NULL to utime so it will not fail if we just have
  305.      write access to the file, but don't own it.  */
  306.       status = utime (file, NULL);
  307. #endif
  308.     }
  309.   else
  310.     {
  311.       struct utimbuf utb;
  312.  
  313.       if (use_ref)
  314.     {
  315.       utb.actime = ref_stats.st_atime;
  316.       utb.modtime = ref_stats.st_mtime;
  317.     }
  318.       else
  319.     utb.actime = utb.modtime = newtime;
  320.  
  321.       if (!(change_times & CH_ATIME))
  322.     utb.actime = sbuf.st_atime;
  323.  
  324.       if (!(change_times & CH_MTIME))
  325.     utb.modtime = sbuf.st_mtime;
  326.  
  327.       status = utime (file, &utb);
  328.     }
  329.   
  330.   if (status)
  331.     {
  332.       error (0, errno, "%s", file);
  333.       return 1;
  334.     }
  335.  
  336.   return 0;
  337. }
  338.  
  339. #ifndef HAVE_UTIME_NULL
  340. /* Emulate utime (file, NULL) for systems (like 4.3BSD) that do not
  341.    interpret it to set the access and modification times of FILE to
  342.    the current time.  FILESIZE is the correct size of FILE, used to
  343.    make sure empty files are not lengthened to 1 byte.
  344.    Return 0 if successful, -1 if not. */
  345.  
  346. static int
  347. utime_now (file, filesize)
  348.      char *file;
  349.      off_t filesize;
  350. {
  351.   int fd;
  352.   char c;
  353.   int status = 0;
  354.  
  355.   fd = open (file, O_RDWR, 0666);
  356.   if (fd < 0
  357.       || read (fd, &c, sizeof (char)) < 0
  358.       || lseek (fd, (off_t) 0, SEEK_SET) < 0
  359.       || write (fd, &c, sizeof (char)) < 0
  360.       || ftruncate (fd, filesize) < 0
  361.       || close (fd) < 0)
  362.     status = -1;
  363.   return status;
  364. }
  365. #endif
  366.  
  367. static void
  368. usage ()
  369. {
  370.   fprintf (stderr, "\
  371. Usage: %s [-acfm] [-r reference-file] [-t MMDDhhmm[[CC]YY][.ss]]\n\
  372.        [-d time] [--time={atime,access,use,mtime,modify}] [--date=time]\n\
  373.        [--file=reference-file] [--no-create] [--help] [--version] file...\n",
  374.        program_name);
  375.   exit (1);
  376. }
  377.