home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / ns2tab / part01 / p_time.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-04  |  982 b   |  56 lines

  1. #include <sys/types.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <arpa/nameser.h>
  5. #include <arpa/inet.h>
  6. #include <resolv.h>
  7.  
  8. static  char nbuf[40];
  9. /*
  10.  * Return a mnemonic for a time to live
  11.  */
  12. char *
  13. P_time(value)
  14.     u_long value;
  15. {
  16.     int secs, mins, hours;
  17.     register char *p;
  18.  
  19.     if (value == 0) {
  20.         strcpy(nbuf, "0 secs");
  21.         return(nbuf);
  22.     }
  23.  
  24.     secs = value % 60;
  25.     value /= 60;
  26.     mins = value % 60;
  27.     value /= 60;
  28.     hours = value % 24;
  29.     value /= 24;
  30.  
  31. #define    PLURALIZE(x)    x, (x == 1) ? "" : "s"
  32.     p = nbuf;
  33.     if (value) {
  34.         (void)sprintf(p, "%d day%s", PLURALIZE(value));
  35.         while (*++p);
  36.     }
  37.     if (hours) {
  38.         if (value)
  39.             *p++ = ' ';
  40.         (void)sprintf(p, "%d hour%s", PLURALIZE(hours));
  41.         while (*++p);
  42.     }
  43.     if (mins) {
  44.         if (value || hours)
  45.             *p++ = ' ';
  46.         (void)sprintf(p, "%d min%s", PLURALIZE(mins));
  47.         while (*++p);
  48.     }
  49.     if (secs || ! (value || hours || mins)) {
  50.         if (value || hours || mins)
  51.             *p++ = ' ';
  52.         (void)sprintf(p, "%d sec%s", PLURALIZE(secs));
  53.     }
  54.     return(nbuf);
  55. }
  56.