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

  1. /*
  2.  * getabsdate absolute_date ... - convert absolute_date to seconds since epoch
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <time.h>
  8. #include <sys/types.h>
  9. #include <sys/timeb.h>
  10. #include <stdlib.h>
  11.  
  12. /* privates */
  13. static struct timeb ftnow;
  14. static int exitstatus = 0;
  15.  
  16. char *progname;
  17.  
  18. /* imports */
  19. extern struct tm *gmtime();
  20. extern time_t time();
  21. extern int optind;
  22. extern char *optarg;
  23.  
  24. extern char *strsave();
  25. extern time_t getabsdate();
  26.  
  27. /* Forwards. */
  28. extern void process();
  29.  
  30. /*
  31.  - main - parse arguments and handle options
  32.  */
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     register int c, errflg = 0;
  38.  
  39.     progname = argv[0];
  40.     ftime(&ftnow);
  41.  
  42.     while ((c = getopt(argc, argv, "")) != EOF)
  43.         switch (c) {
  44.         case '?':
  45.         default:
  46.             errflg++;
  47.             break;
  48.         }
  49.     if (errflg || optind == argc) {
  50.         (void) fprintf(stderr, "Usage: %s ascii_time ...\n", progname);
  51.         exit(2);
  52.     }
  53.  
  54.     for (; optind < argc; optind++)
  55.         process(argv[optind]);
  56.     exit(exitstatus);
  57. }
  58.  
  59. /*
  60.  * process - print time_t of tm
  61.  */
  62. void
  63. process(timestr)
  64. char *timestr;
  65. {
  66.     register time_t tstime;
  67.     register char *copy = strsave(timestr);
  68.  
  69.     if (copy == NULL) {
  70.         exitstatus = 1;
  71.         return;
  72.     }
  73.     tstime = getabsdate(copy, &ftnow);
  74.     if (tstime < 0) {
  75.         (void) fprintf(stderr, "%s: `%s' not a valid date\n",
  76.                    progname, timestr);
  77.         exitstatus = 1;
  78.     } else
  79.         (void) printf("%ld\n", tstime);
  80.     free(copy);
  81. }
  82.