home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 33 < prev    next >
Encoding:
Internet Message Format  |  1990-07-13  |  3.3 KB

  1. From: jfh@killer.UUCP (John Haugh)
  2. Newsgroups: comp.sources.misc
  3. Subject: Program to set file access and modification times.
  4. Message-ID: <2828@ncoast.UUCP>
  5. Date: 9 Jul 87 02:42:27 GMT
  6. Sender: allbery@ncoast.UUCP
  7. Organization: The Unix(tm) Connection, Dallas, Texas
  8. Lines: 164
  9. Approved: allbery@ncoast.UUCP
  10. X-Archive: comp.sources.misc/8707/33
  11.  
  12. I wrote this one day trying to solve the problem of the file modification
  13. dates of restored files.  Someone in comp.unix.questions wanted something
  14. like this, so here it is.
  15.  
  16. Bugs - doesn't know about DST, and barely knows how to get the hours
  17. in the first place.  No manual page, this program works kind of like the
  18. settime command that I remember from Version 7 days.  Also, the inode
  19. changed time will always be set to the time you changed the file times.
  20.  
  21. Enjoy!
  22.  
  23. John.        (jfh@killer.UUCP)
  24. ------------------------  cut somewheres near here ----------------------
  25.  
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29.  
  30. /*
  31.  * settime - change the access and modification dates of files
  32.  *
  33.  *    This is mine.  You steal, and you die.  Ha, Ha.
  34.  */
  35.  
  36. static    char    sccsid[] = "@(#)settime.c    1.0    John F. Haugh    08/05/86";
  37.  
  38. #define    SECPERMIN    60
  39. #define    MINPERHR    60
  40. #define    HRPERDAY    24
  41. #define    SECPERHR    (MINPERHR*SECPERMIN)
  42. #define    SECPERDAY    (HRPERDAY*MINPERHR*SECPERMIN)
  43. #define    YEAR(y)        (y & 03 ? 365:366)
  44. #define    MONTH(m,y)    (m != 2  ? months[(m - 1)]:(y & 03 ? 28:29))
  45.  
  46. extern    long    timezone;
  47.  
  48. int    months[12] = {
  49.     31, 28, 31, 30,
  50.     31, 30, 31, 31,
  51.     30, 31, 30, 31
  52. };
  53.  
  54. conversion ()
  55. {
  56.     fprintf (stderr, "settime: conversion error\n");
  57.     exit (1);
  58. }
  59.  
  60. usage ()
  61. {
  62.     fprintf (stderr, "usage: settime [ mmddHHMMyy | -f file ] name ...\n");
  63.     exit (1);
  64. }
  65.  
  66. twodigits (s)
  67. char    *s;
  68. {
  69.     if (s[0] == 0 || s[1] == 0) {
  70.         fprintf (stderr, "settime: bad date string\n");
  71.         exit (1);
  72.     }
  73.     return ((s[0] - '0') * 10 + s[1] - '0');
  74. }
  75.  
  76. month (m, y)
  77. int    m,
  78.     y;
  79. {
  80.     int    i;
  81.     int    days = 0;
  82.  
  83.     for (i = 1; i < m;i++)
  84.         days += MONTH (i, y);
  85.  
  86.     return (SECPERDAY * days);
  87. }
  88.  
  89. year (y)
  90. int    y;
  91. {
  92.     int    i;
  93.     int    days = 0;
  94.  
  95.     for (i = 70;i < y;i++)
  96.         days += YEAR (i);
  97.  
  98.     return (days * SECPERDAY);
  99. }
  100.  
  101. time_t    date (ds)
  102. char    *ds;
  103. {
  104.     time_t    seconds = 0;
  105.     int    len;
  106.     int    mm,
  107.         dd,
  108.         HH,
  109.         MM,
  110.         yy;
  111.  
  112.     len = strlen (ds);
  113.     if (len & 01) {
  114.         mm = ds[0] - '0';    
  115.         ds++;
  116.     }
  117.     else {
  118.         mm = twodigits (ds);
  119.         ds += 2;
  120.     }
  121.     dd = twodigits (ds);
  122.     ds += 2;
  123.     HH = twodigits (ds);
  124.     ds += 2;
  125.     MM = twodigits (ds);
  126.     ds += 2;
  127.     yy = twodigits (ds);
  128.     if (MM > 59)
  129.         conversion ();
  130.     if (mm > 12 || mm == 0)
  131.         conversion ();
  132.     if (HH > 23)
  133.          conversion ();
  134.     if (yy < 70)
  135.         conversion ();
  136.     if (dd > MONTH (mm, yy) || dd == 0)
  137.         conversion ();
  138.     seconds = year (yy) + month (mm, yy) + (SECPERDAY * (dd - 1)) + (SECPERHR * HH) + (SECPERMIN * MM) + timezone;
  139.     return (seconds);
  140. }
  141.  
  142. main (argc, argv)
  143. int    argc;
  144. char    **argv;
  145. {
  146.     struct    utimbuf {
  147.         time_t    actime;
  148.         time_t    modtime;
  149.     } utimbuf;
  150.     struct    stat    statbuf;
  151.     int    i;
  152.  
  153.     if (argc < 3)
  154.         usage ();
  155.  
  156.     tzset ();
  157.     if (argv[1][0] == '-') {
  158.         if (argv[1][1] != 'f')
  159.             usage ();
  160.         if (stat (argv[2], &statbuf) != 0) {
  161.             perror (argv[2]);
  162.             exit (2);
  163.         }
  164.         utimbuf.actime = statbuf.st_atime;
  165.         utimbuf.modtime = statbuf.st_mtime;
  166.         i = 3;
  167.     }
  168.     else {
  169.         utimbuf.actime = utimbuf.modtime = date (argv[1]);
  170.         i = 2;
  171.     }
  172.     for (;argv[i] != (char *) 0;i++)
  173.         if (utime (argv[i], &utimbuf) != 0)
  174.             perror (argv[i]);
  175. }
  176.