home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / Editors / emacs / Emacs-1.14b1-sources / sources / utility-src / fileutils / src / touch.c < prev   
Encoding:
C/C++ Source or Header  |  1994-04-08  |  9.2 KB  |  407 lines  |  [TEXT/EMAC]

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