home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / bootptest-1.1 / part01 / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-11  |  4.9 KB  |  279 lines

  1. /*
  2.  * Copyright (c) 1988-1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static char rcsid[] =
  24.     "@(#) $Header: util.c,v 1.12 91/10/28 22:09:31 mccanne Exp $ (LBL)";
  25. #endif
  26.  
  27. #include <stdio.h>
  28. #ifdef __STDC__
  29. #include <stdlib.h>
  30. #endif
  31. #include <sys/types.h>
  32. #include <sys/time.h>
  33. #include <ctype.h>
  34. #include <varargs.h>
  35. #include <sys/file.h>
  36. #include <sys/stat.h>
  37.  
  38. #include "interface.h"
  39.  
  40. /* Hex digit to integer. */
  41. static inline int
  42. xdtoi(c)
  43. {
  44.     if (isdigit(c))
  45.         return c - '0';
  46.     else if (islower(c))
  47.         return c - 'a' + 10;
  48.     else
  49.         return c - 'A' + 10;
  50. }
  51.  
  52. /*
  53.  * Convert string to integer.  Just like atoi(), but checks for 
  54.  * preceding 0x or 0 and uses hex or octal instead of decimal.
  55.  */
  56. int
  57. stoi(s)
  58.     char *s;
  59. {
  60.     int base = 10;
  61.     int n = 0;
  62.  
  63.     if (*s == '0') {
  64.         if (s[1] == 'x' || s[1] == 'X') {
  65.             s += 2;
  66.             base = 16;
  67.         }
  68.         else {
  69.             base = 8;
  70.             s += 1;
  71.         }
  72.     }
  73.     while (*s)
  74.         n = n * base + xdtoi(*s++);
  75.  
  76.     return n;
  77. }
  78.  
  79. /*
  80.  * Print out a filename (or other ascii string).
  81.  * Return true if truncated.
  82.  */
  83. int
  84. printfn(s, ep)
  85.     register u_char *s, *ep;
  86. {
  87.     register u_char c;
  88.  
  89.     putchar('"');
  90.     while (c = *s++) {
  91.         if (s > ep) {
  92.             putchar('"');
  93.             return(1);
  94.         }
  95.         if (!isascii(c)) {
  96.             c = toascii(c);
  97.             putchar('M');
  98.             putchar('-');
  99.         }
  100.         if (!isprint(c)) {
  101.             c ^= 0x40;    /* DEL to ?, others to alpha */
  102.             putchar('^');
  103.         }
  104.         putchar(c);
  105.     }
  106.     putchar('"');
  107.     return(0);
  108. }
  109.  
  110. /*
  111.  * Print the timestamp
  112.  */
  113. void
  114. ts_print(tvp)
  115.     register struct timeval *tvp;
  116. {
  117.     register int i;
  118.  
  119.     if (tflag > 0) {
  120.         /* Default */
  121.         i = (tvp->tv_sec + thiszone) % 86400;
  122.         (void)printf("%02d:%02d:%02d.%06d ",
  123.             i / 3600, (i % 3600) / 60, i % 60, tvp->tv_usec);
  124.     } else if (tflag < 0) {
  125.         /* Unix timeval style */
  126.         (void)printf("%d.%06d ", tvp->tv_sec, tvp->tv_usec);
  127.     }
  128. }
  129.  
  130. #ifdef NOVFPRINTF
  131. /*
  132.  * Stock 4.3 doesn't have vfprintf. 
  133.  * This routine is due to Chris Torek.
  134.  */
  135. vfprintf(f, fmt, args)
  136.     FILE *f;
  137.     char *fmt;
  138.     va_list args;
  139. {
  140.     int ret;
  141.  
  142.     if ((f->_flag & _IOWRT) == 0) {
  143.         if (f->_flag & _IORW)
  144.             f->_flag |= _IOWRT;
  145.         else
  146.             return EOF;
  147.     }
  148.     ret = _doprnt(fmt, args, f);
  149.     return ferror(f) ? EOF : ret;
  150. }
  151. #endif
  152.  
  153. static char *
  154. stripdir(s)
  155.     register char *s;
  156. {
  157.     register char *cp;
  158.     char *rindex();
  159.  
  160.     cp = rindex(s, '/');
  161.     return (cp != 0) ? cp + 1 : s;
  162. }
  163.  
  164. /* VARARGS */
  165. void
  166. error(va_alist)
  167.     va_dcl
  168. {
  169.     register char *cp;
  170.     va_list ap;
  171.  
  172.     (void)fprintf(stderr, "%s: ", stripdir(program_name));
  173.  
  174.     va_start(ap);
  175.     cp = va_arg(ap, char *);
  176.     (void)vfprintf(stderr, cp, ap);
  177.     va_end(ap);
  178.     if (*cp) {
  179.         cp += strlen(cp);
  180.         if (cp[-1] != '\n')
  181.             (void)fputc('\n', stderr);
  182.     }
  183.     exit(1);
  184.     /* NOTREACHED */
  185. }
  186.  
  187. /* VARARGS */
  188. void
  189. warning(va_alist)
  190.     va_dcl
  191. {
  192.     register char *cp;
  193.     va_list ap;
  194.  
  195.     (void)fprintf(stderr, "%s: warning: ", stripdir(program_name));
  196.  
  197.     va_start(ap);
  198.     cp = va_arg(ap, char *);
  199.     (void)vfprintf(stderr, cp, ap);
  200.     va_end(ap);
  201.     if (*cp) {
  202.         cp += strlen(cp);
  203.         if (cp[-1] != '\n')
  204.             (void)fputc('\n', stderr);
  205.     }
  206. }
  207.  
  208.  
  209. /*
  210.  * Copy arg vector into a new buffer, concatenating arguments with spaces.
  211.  */
  212. char *
  213. copy_argv(argv)
  214.     register char **argv;
  215. {
  216.     register char **p;
  217.     register int len = 0;
  218.     char *buf;
  219.     char *src, *dst;
  220.  
  221.     p = argv;
  222.     if (*p == 0)
  223.         return 0;
  224.  
  225.     while (*p)
  226.         len += strlen(*p++) + 1;
  227.  
  228.     buf = malloc(len);
  229.  
  230.     p = argv;
  231.     dst = buf;
  232.     while (src = *p++) {
  233.         while (*dst++ = *src++)
  234.             ;
  235.         dst[-1] = ' ';
  236.     }
  237.     dst[-1] = '\0';
  238.  
  239.     return buf;
  240. }
  241.  
  242. char *
  243. read_infile(fname)
  244.     char *fname;
  245. {
  246.     struct stat buf;
  247.     int fd;
  248.     char *p;
  249.  
  250.     fd = open(fname, O_RDONLY);
  251.     if (fd < 0)
  252.         error("can't open '%s'", fname);
  253.  
  254.     if (fstat(fd, &buf) < 0)
  255.         error("can't state '%s'", fname);
  256.  
  257.     p = malloc((unsigned)buf.st_size);
  258.     if (read(fd, p, (int)buf.st_size) != buf.st_size)
  259.         error("problem reading '%s'", fname);
  260.     
  261.     return p;
  262. }
  263.  
  264. /*
  265.  * Left justify 'addr' and return its resulting network mask.
  266.  */
  267. u_long
  268. net_mask(addr)
  269.     u_long *addr;
  270. {
  271.     register u_long m = 0xffffffff;
  272.  
  273.     if (*addr)
  274.         while ((*addr & 0xff000000) == 0)
  275.             *addr <<= 8, m <<= 8;
  276.  
  277.     return m;
  278. }
  279.