home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1646 < prev    next >
Text File  |  1990-12-28  |  3KB  |  133 lines

  1. Newsgroups: alt.sources
  2. From: chris@mimsy.umd.edu (Chris Torek)
  3. Subject: [comp.unix.questions...] Re: adjtime(2) hints needed
  4. Message-ID: <1990Aug6.041326.25200@math.lsa.umich.edu>
  5. Date: Mon, 6 Aug 90 04:13:26 GMT
  6.  
  7. Archive-name: adjtime/04-Aug-90
  8. Original-posting-by: chris@mimsy.umd.edu (Chris Torek)
  9. Original-subject: Re: adjtime(2) hints needed
  10. Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
  11.  
  12. [Reposted from comp.unix.questions,alt.sys.sun.
  13. Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti).]
  14.  
  15. In article <2119@cirrusl.UUCP> dhesi%cirrusl@oliveb.ATC.olivetti.com
  16. (Rahul Dhesi) writes:
  17. >I must supply adjtime(2) with a delta time ... [my] delta_time.tv_sec
  18. >and delta_time.tv_usec could have different signs.  Is this acceptable?
  19.  
  20. It may be accepted, but it is not a great idea.
  21.  
  22. The following is a program I wrote some time ago.  error() is a function
  23. that prints an error message (appending strerror(arg2) if arg2!=0) to
  24. stderr, prefixed by the program's name (is there an echo in here? :-) ),
  25. and then quits with exit status arg1 if arg1!=0.  (The program's name
  26. is stashed secretly by the C runtime startup code.)
  27.  
  28. #include <stdio.h>
  29. #include <ctype.h>
  30. #include <sys/types.h>
  31. #include <sys/time.h>
  32.  
  33. struct    timeval delta, olddelta;
  34. extern    int errno;
  35.  
  36. main(argc, argv)
  37.     int argc;
  38.     char **argv;
  39. {
  40.  
  41.     if (argc < 2) {
  42.         timerclear(&delta);
  43.         if (adjtime(&delta, &olddelta))
  44.             error(1, errno, "adjtime");
  45.         if (adjtime(&olddelta, &delta)) {
  46.             int e = errno;
  47.  
  48.             showdelta("current adjustment", &olddelta);
  49.             error(1, e, "adjtime (readjust)");
  50.         }
  51.         showdelta("current adjustment", &olddelta);
  52.         exit(0);
  53.     }
  54.     if (convtime(&delta, argv[1]))
  55.         error(1, 0, "invalid time spec %s (should be sec.usec)\n",
  56.             argv[1]);
  57.     if (adjtime(&delta, &olddelta))
  58.         error(1, errno, "adjtime");
  59.     showdelta("old adjustment", &olddelta);
  60.     showdelta("new adjustment", &delta);
  61.     exit(0);
  62. }
  63.  
  64. convtime(tvp, s)
  65.     register struct timeval *tvp;
  66.     register char *s;
  67. {
  68.     char *usec, *index();
  69.     int neg = 0;
  70.  
  71.     if (*s == '-') {
  72.         neg++;
  73.         s++;
  74.     }
  75.     usec = index(s, '.');    /* strrchr for SysV folks */
  76.     if (usec != NULL) {
  77.         *usec++ = 0;
  78.         if (convone(&tvp->tv_usec, usec))
  79.             return (-1);
  80.         if (tvp->tv_usec > 999999)
  81.             return (-1);
  82.     } else
  83.         tvp->tv_usec = 0;
  84.     if (convone(&tvp->tv_sec, s))
  85.         return (-1);
  86.     if (neg) {
  87.         tvp->tv_usec = -tvp->tv_usec;
  88.         tvp->tv_sec = -tvp->tv_sec;
  89.     }
  90.     return (0);
  91. }
  92.  
  93. /* this really should use strtol(), but I did not have it when I wrote this */
  94. convone(lp, s)
  95.     long *lp;
  96.     register char *s;
  97. {
  98.     register long l = 0;
  99.     register int c;
  100.  
  101.     while ((c = *s++) != 0) {
  102.         if (!isdigit(c))
  103.             return (-1);
  104.         l *= 10;
  105.         if (l < 0)
  106.             return (-1);
  107.         l += c - '0';
  108.         if (l < 0)
  109.             return (-1);
  110.     }
  111.     *lp = l;
  112.     return (0);
  113. }
  114.  
  115. showdelta(what, tvp)
  116.     char *what;
  117.     struct timeval *tvp;
  118. {
  119.     char *p = "";
  120.  
  121.     if (tvp->tv_usec < 0) {
  122.         tvp->tv_usec = -tvp->tv_usec;
  123.         if (tvp->tv_sec == 0)
  124.             p = "-";
  125.     }
  126.     (void) printf("%s: %s%ld.%06ld seconds\n", what,
  127.         p, tvp->tv_sec, tvp->tv_usec);
  128. }
  129. -- 
  130. In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
  131. Domain:    chris@cs.umd.edu    Path:    uunet!mimsy!chris
  132.     (New campus phone system, active sometime soon: +1 301 405 2750)
  133.