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