home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / PAMAKE18.ZIP / MTIME.C < prev    next >
C/C++ Source or Header  |  1989-09-30  |  10KB  |  360 lines

  1. /*************************************************************************
  2. |                                                                        |
  3. |   MTIME.C                                                     30.09.89 |
  4. |   PAMAKE Utility:  compiler-dependent time functions                   |
  5. |                                                                        |
  6. *************************************************************************/
  7.  
  8. /*************************************************************************
  9. |                                                                        |
  10. |   Important note:  PAMAKE stores date/time values in two different     |
  11. |   ways depending on which compiler is in use:                          |
  12. |                                                                        |
  13. |   LATTICE, TURBOC   :   Date/time held in DOS file format              |
  14. |                         (PAMAKE_FTIME is #defined)                     |
  15. |                                                                        |
  16. |   MSC, VMS          :   Date/time held in 'Unix' format                |
  17. |                                                                        |
  18. *************************************************************************/
  19.  
  20. #ifdef VMS
  21. #include stdio
  22. #include "h.h"
  23. #include errno
  24. #include ctype
  25. #include string
  26. #include stdlib
  27. #include types
  28. #include stat
  29. #endif
  30.  
  31. #ifdef LATTICE
  32. #define PAMAKE_FTIME 1
  33. #include <stdio.h>
  34. #include <fcntl.h>
  35. #include "h.h"
  36. #include <dos.h>
  37. #include <error.h>
  38. #include <time.h>
  39. #endif
  40.  
  41. #ifdef __TURBOC__
  42. #define PAMAKE_FTIME 1
  43. #include <stdio.h>
  44. #include <fcntl.h>
  45. #include "h.h"
  46. #include <dos.h>
  47. #include <io.h>
  48. #include <stdlib.h>
  49. #include <errno.h>
  50. #include <time.h>
  51. #endif
  52.  
  53. #ifdef MSC
  54. #include <stdio.h>
  55. #include <fcntl.h>
  56. #include <io.h>
  57. #include <sys\types.h>
  58. #include <sys\stat.h>
  59. #include <sys\utime.h>
  60. #include <dos.h>
  61. #include "h.h"
  62. #include <errno.h>
  63. #include <ctype.h>
  64. #include <string.h>
  65. #include <stdlib.h>
  66. #include <time.h>
  67. #endif 
  68.  
  69. /*************************************************************************
  70. |                                                                        |
  71. |   This module implements the following functions:                      |
  72. |                                                                        |
  73. |   modtime    :  get the modification time of a file, 0 if not found    |
  74. |   curtime    :  return current time in suitable format                 |
  75. |   touch      :  update the time of a file to now                       |
  76. |   dodisp     :  display a file time                                    |
  77. |                                                                        |
  78. *************************************************************************/
  79.  
  80. /*************************************************************************
  81. |                                                                        |
  82. |   MODTIME                                                              |
  83. |                                                                        |
  84. *************************************************************************/
  85.  
  86. #ifdef LATTICE
  87. #define MODTIME_DONE 1
  88.  
  89. void
  90. modtime(np)
  91. struct name *           np;
  92. {
  93.     long                    ret;
  94.     int                     fd;
  95.  
  96.     if ((fd = open(dollar(np->n_name), 0)) < 0)
  97.     {
  98.         if (errno != ENOENT) 
  99.             fatal("Unable to open file %s: error code %02x", dollar(np->n_name), errno);
  100.         np->n_time = 0L;
  101.     }
  102.     else
  103.     {
  104.         if ((ret = getft(fd)) == -1L)
  105.             fatal("Unable to get modification time for file %s; error code %02x", dollar(np->n_name), errno);
  106.         np->n_time = ret;
  107.     }
  108.     close(fd);
  109. }
  110.  
  111. #endif
  112.  
  113.  
  114. #ifdef __TURBOC__
  115. #define MODTIME_DONE 1
  116.  
  117. void
  118. modtime(np)
  119. struct name *           np;
  120. {
  121.     long                    ret;
  122.     int                     fd;
  123.  
  124.     if ((fd = open(dollar(np->n_name), 0)) < 0)
  125.     {
  126.         if (errno != ENOENT) 
  127.             fatal("Unable to open file %s: error code %02x", dollar(np->n_name), errno);
  128.         np->n_time = 0L;
  129.     }
  130.     else
  131.     {
  132.         if (getftime(fd,(struct ftime *)&ret))
  133.             fatal("Unable to get modification time for file %s; error code %02x", dollar(np->n_name), errno);
  134.         np->n_time = ret;
  135.     }
  136.     close(fd);
  137. }
  138.  
  139. #endif
  140.  
  141.  
  142. #ifndef MODTIME_DONE
  143.  
  144. void
  145. modtime(np)
  146. struct name *           np;
  147. {
  148.     struct stat             info;
  149.     int                     fd;
  150.  
  151.     if ((fd = open(dollar(np->n_name), 0)) < 0)
  152.     {
  153.         if (errno != ENOENT) 
  154.             fatal("Unable to open file %s: error code %02x", dollar(np->n_name), errno);
  155.         np->n_time = 0L;
  156.     }
  157.     else if (fstat(fd, &info) < 0)
  158.         fatal("Unable to get modification time for file %s; error code %02x", dollar(np->n_name), errno);
  159.     else
  160.         np->n_time = info.st_mtime;
  161.  
  162.     close(fd);
  163. }
  164.  
  165. #endif
  166.  
  167.  
  168. /*************************************************************************
  169. |                                                                        |
  170. |   CURTIME                                                              |
  171. |                                                                        |
  172. *************************************************************************/
  173.  
  174. #ifdef PAMAKE_FTIME
  175.  
  176. unsigned long curtime(void)
  177. {
  178.     unsigned long           ct;
  179.     struct tm *             p;   
  180.     unsigned int            lo,hi;
  181.  
  182.     time((time_t *)&ct);
  183.     p = localtime((time_t *)&ct);
  184.     hi = (p->tm_year - 80) << 9;
  185.     hi += ((p->tm_mon + 1) << 5);
  186.     hi += p->tm_mday;
  187.     lo = (p->tm_hour << 11);
  188.     lo += (p->tm_min << 5);
  189.     lo += p->tm_sec;
  190.     return (lo + ((unsigned long)hi << 16));
  191. }
  192.  
  193. #else
  194.  
  195. unsigned long curtime(void)
  196. {
  197.     unsigned long           ct;
  198.  
  199.     time(&ct);
  200.     return ct;
  201. }
  202.  
  203. #endif
  204.  
  205.  
  206. /*************************************************************************
  207. |                                                                        |
  208. |   TOUCH                                                                |
  209. |                                                                        |
  210. *************************************************************************/
  211.  
  212. #ifdef VMS
  213. #define TOUCH_DONE 1
  214.  
  215. void
  216. touch(np)
  217. struct name *           np;
  218. {
  219.     printf("    file %s not touched:  touch not implemented for VMS\n",
  220.         dollar(np->n_name));
  221. }
  222.  
  223. #endif
  224.  
  225. #ifdef LATTICE
  226. #define TOUCH_DONE 1
  227.  
  228. void
  229. touch(np)
  230. struct name *           np;
  231. {
  232.     int                     fd;
  233.  
  234.     if (!domake || !silent) printf("    touch %s\n", dollar(np->n_name));
  235.     if (domake)
  236.     {
  237.         if ((fd = open(dollar(np->n_name), 0)) < 0)
  238.         {
  239.             if (errno != ENOENT) 
  240.                 fatal("Unable to open file %s: error code %02x", dollar(np->n_name), errno);
  241.             np->n_time = 0L;
  242.             return;
  243.         }
  244.         if (chgft(fd,curtime()))
  245.         {
  246.             fatal("Unable to update modification time for file %s; error code %02x",
  247.                 dollar(np->n_name),errno);
  248.         }
  249.         close(fd);
  250.     }
  251. }
  252.  
  253. #endif
  254.  
  255. #ifdef __TURBOC__
  256. #define TOUCH_DONE 1
  257.  
  258. void
  259. touch(np)
  260. struct name *           np;
  261. {
  262.     int                     fd;
  263.     long                    ret;
  264.  
  265.     if (!domake || !silent) printf("    touch %s\n", dollar(np->n_name));
  266.     if (domake)
  267.     {
  268.         if ((fd = open(dollar(np->n_name), 0)) < 0)
  269.         {
  270.             if (errno != ENOENT) 
  271.                 fatal("Unable to open file %s: error code %02x", dollar(np->n_name), errno);
  272.             np->n_time = 0L;
  273.             return;
  274.         }
  275.         ret = curtime();
  276.         if (setftime(fd,(struct ftime *)&ret))
  277.         {
  278.             fatal("Unable to update modification time for file %s; error code %02x",
  279.                 dollar(np->n_name),errno);
  280.         }
  281.         close(fd);
  282.     }
  283. }
  284.  
  285. #endif
  286.  
  287.  
  288. #ifndef TOUCH_DONE
  289.  
  290. void
  291. touch(np)
  292. struct name *           np;
  293. {
  294.     if (!domake || !silent) printf("    touch %s\n", dollar(np->n_name));
  295.     if (domake)
  296.     {
  297.         if (utime(dollar(np->n_name),(void *)0))
  298.         {
  299.             if (errno != ENOENT) 
  300.                 fatal("Unable to update modification time for file %s; error code %02x",
  301.                     dollar(np->n_name),errno);
  302.             else
  303.                 fprintf(stderr,"%s: '%s' modification time not updated, file not found\n",
  304.                     myname, dollar(np->n_name));
  305.         }
  306.     }
  307. }
  308.  
  309. #endif
  310.  
  311.  
  312. /*************************************************************************
  313. |                                                                        |
  314. |   DODISP                                                               |
  315. |                                                                        |
  316. *************************************************************************/
  317.  
  318. #ifdef PAMAKE_FTIME
  319.  
  320. void
  321. dodisp(name,t)
  322. char *          name;
  323. long            t;
  324. {
  325.     unsigned long u = t;
  326.  
  327.     static char * mn[12] = {"Jan","Feb","Mar","Apr","May","Jun",
  328.                             "Jul","Aug","Sep","Oct","Nov","Dec"};
  329.     if (t < 10)
  330.         printf("... %s Timestamp %ld\n",name,t);
  331.     else 
  332.     {
  333.         printf("... %s Timestamp ",name);
  334.         printf("%s %02d %02d:%02d:%02d %4d\n",
  335.             mn[(int)((u >> 21) & 0xf) - 1],
  336.             (int)((u >> 16) & 0x1f),
  337.             (int)((u >> 11) & 0x1f),
  338.             (int)((u >> 5) & 0x3f),
  339.             (int)(u & 0x1f),
  340.             (int)((u >> 25) + 1980));
  341.     }
  342. }
  343.  
  344. #else
  345.  
  346. void
  347. dodisp(name,t)
  348. char *          name;
  349. long            t;
  350. {
  351.     if (t < 10)
  352.         printf("... %s Timestamp %ld\n",name,t);
  353.     else
  354.         printf("... %s Timestamp %s\n",name,ctime(&t));
  355. }
  356.  
  357. #endif
  358.  
  359.  
  360.