home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.lbl.gov / 2014.05.ftp.ee.lbl.gov.tar / ftp.ee.lbl.gov / bmd-1.0beta.tar.Z / bmd-1.0beta.tar / bmd-1.0beta / app / omtd / util.c < prev   
C/C++ Source or Header  |  1991-01-22  |  3KB  |  118 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.3 91/01/21 21:32:49 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 <ctype.h>
  33. #include <varargs.h>
  34. #include <sys/file.h>
  35. #include <sys/stat.h>
  36.  
  37. /* Hex digit to integer. */
  38. static inline int
  39. xdtoi(c)
  40. {
  41.     if (isdigit(c))
  42.         return c - '0';
  43.     else if (islower(c))
  44.         return c - 'a' + 10;
  45.     else
  46.         return c - 'A' + 10;
  47. }
  48.  
  49. /*
  50.  * Convert string to integer.  Just like atoi(), but checks for 
  51.  * preceding 0x or 0 and uses hex or octal instead of decimal.
  52.  */
  53. int
  54. stoi(s)
  55.     char *s;
  56. {
  57.     int base = 10;
  58.     int n = 0;
  59.  
  60.     if (*s == '0') {
  61.         if (s[1] == 'x' || s[1] == 'X') {
  62.             s += 2;
  63.             base = 16;
  64.         }
  65.         else {
  66.             base = 8;
  67.             s += 1;
  68.         }
  69.     }
  70.     while (*s)
  71.         n = n * base + xdtoi(*s++);
  72.  
  73.     return n;
  74. }
  75.  
  76. /* VARARGS */
  77. void
  78. error(va_alist)
  79.     va_dcl
  80. {
  81.     register char *cp;
  82.     va_list ap;
  83.  
  84.     va_start(ap);
  85.     cp = va_arg(ap, char *);
  86.     (void)vfprintf(stderr, cp, ap);
  87.     va_end(ap);
  88.     if (*cp) {
  89.         cp += strlen(cp);
  90.         if (cp[-1] != '\n')
  91.             (void)fputc('\n', stderr);
  92.     }
  93. }
  94.  
  95. /* VARARGS */
  96. void
  97. fatal(va_alist)
  98.     va_dcl
  99. {
  100.     register char *cp;
  101.     va_list ap;
  102.  
  103.     (void)fprintf(stderr, "mtd: ");
  104.  
  105.     va_start(ap);
  106.     cp = va_arg(ap, char *);
  107.     (void)vfprintf(stderr, cp, ap);
  108.     va_end(ap);
  109.     if (*cp) {
  110.         cp += strlen(cp);
  111.         if (cp[-1] != '\n')
  112.             (void)fputc('\n', stderr);
  113.     }
  114.     exit(1);
  115.     /* NOTREACHED */
  116. }
  117.  
  118.