home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / util / ctime.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  1KB  |  87 lines

  1. /*
  2.  * ctime time_t ... - print the ascii time of time_t(s)
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include <sys/types.h>
  9. #include <stdlib.h>
  10.  
  11. /* imports */
  12. extern char *ctime(), *asctime();
  13. extern struct tm *gmtime();
  14. extern time_t time();
  15. extern char *ascingmtime();
  16.  
  17. /* Forwards. */
  18. extern void process();
  19. extern int optind;
  20. extern char *optarg;
  21.  
  22. char *progname;
  23.  
  24. /* privates */
  25. static int gmt = 0;
  26. static int inetfmt = 0;
  27.  
  28. /*
  29.  - main - parse arguments and handle options
  30.  */
  31. main(argc, argv)
  32. int argc;
  33. char *argv[];
  34. {
  35.     register int c, errflg = 0;
  36.  
  37.     progname = argv[0];
  38.  
  39.     while ((c = getopt(argc, argv, "iu")) != EOF)
  40.         switch (c) {
  41.         case 'i':
  42.             inetfmt++;
  43.             gmt++;        /* too painful to do local time */
  44.             break;
  45.         case 'u':
  46.             ++gmt;
  47.             break;
  48.         case '?':
  49.         default:
  50.             errflg++;
  51.             break;
  52.         }
  53.     if (errflg || optind == argc) {
  54.         (void) fprintf(stderr, "Usage: %s [-iu] ascii_time ...\n",
  55.                    progname);
  56.         exit(2);
  57.     }
  58.  
  59.     for (; optind < argc; optind++)
  60.         process(argv[optind]);
  61.     exit(0);
  62. }
  63.  
  64. /*
  65.  * process - print time_t of tm
  66.  */
  67. void
  68. process(tms)
  69. char *tms;
  70. {
  71.     time_t tm;
  72.  
  73.     if (strcmp(tms, "now") == 0)
  74.         tm = time(&tm);
  75.     else
  76.         tm = atol(tms);
  77.     if (gmt) {
  78.         register struct tm *prstime = gmtime(&tm);
  79.  
  80.         if (inetfmt)
  81.             (void) fputs(ascingmtime(prstime), stdout);
  82.         else
  83.             (void) fputs(asctime(prstime), stdout);
  84.     } else
  85.         (void) fputs(ctime(&tm), stdout);
  86. }
  87.