home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 December / PCO_1298.ISO / filesbbs / os2 / fn128os2.arj / FN128OS2.ZIP / fn128os2 / src / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-02  |  3.2 KB  |  154 lines

  1. /*
  2.  # $Id: misc.c,v 1.5 1998/03/10 11:39:42 fbm Exp fbm $
  3.  # Copyright (C) 1997,1998 Farrell McKay
  4.  # All rights reserved.
  5.  #
  6.  # This file is part of the Fortify distribution, a toolkit for
  7.  # upgrading the cryptographic strength of the Netscape Navigator
  8.  # web browser, authored by Farrell McKay.
  9.  #
  10.  # This toolkit is provided to the recipient under the
  11.  # following terms and conditions:-
  12.  #   1.  This copyright notice must not be removed or modified.
  13.  #   2.  This toolkit may not be reproduced or included in any commercial
  14.  #       media distribution, or commercial publication (for example CD-ROM,
  15.  #       disk, book, magazine, journal) without first obtaining the author's
  16.  #       express permission.
  17.  #   3.  This toolkit, or any component of this toolkit, may not be
  18.  #       commercially resold, redeveloped, rewritten, enhanced or otherwise
  19.  #       used as the basis for commercial venture, without first obtaining
  20.  #       the author's express permission.
  21.  #   4.  Subject to the above conditions being observed (1-3), this toolkit
  22.  #       may be freely reproduced or redistributed.
  23.  #   5.  This software is provided "as-is", without express or implied
  24.  #       warranty.  In no event shall the author be liable for any direct,
  25.  #       indirect or consequential damages however caused.
  26.  #   6.  Subject to the above conditions being observed (1-5),
  27.  #       this toolkit may be used at no cost to the recipient.
  28.  #
  29.  # Farrell McKay
  30.  # Wayfarer Systems Pty Ltd        contact@fortify.net
  31.  */
  32.  
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include <sys/types.h>
  39. #include "misc.h"
  40.  
  41. void *
  42. _malloc(size_t sz)
  43. {
  44.     void    *p;
  45.     p = malloc(sz);
  46.     if (p == (void *)0) {
  47.         fprintf(stderr, "Not enough memory\n");
  48.         exit(1);
  49.     }
  50.     return p;
  51. }
  52.  
  53. void *
  54. _realloc(void *s, size_t sz)
  55. {
  56.     void    *p;
  57.     p = s? realloc(s, sz): malloc(sz);
  58.     if (p == (void *)0) {
  59.         fprintf(stderr, "Not enough memory\n");
  60.         exit(1);
  61.     }
  62.     return p;
  63. }
  64.  
  65. void *
  66. _calloc(size_t n, size_t sz)
  67. {
  68.     void    *p;
  69.     p = calloc(n, sz);
  70.     if (p == (void *)0) {
  71.         fprintf(stderr, "Not enough memory\n");
  72.         exit(1);
  73.     }
  74.     return p;
  75. }
  76.  
  77. char *
  78. _strdup(const char *s)
  79. {
  80.     char    *p;
  81.     p = strdup(s);
  82.     if (p == (char *)0) {
  83.         fprintf(stderr, "Not enough memory\n");
  84.         exit(1);
  85.     }
  86.     return p;
  87. }
  88.  
  89. char *
  90. _strndup(const char *s, int n)
  91. {
  92.     char    *p;
  93.     p = (char *) _calloc(n+1, sizeof(char));
  94.     strncpy(p, s, n);
  95.     return p;
  96. }
  97.  
  98. char *
  99. seg_name(int i)
  100. {
  101.     if (i == SEG_TEXT)
  102.         return "text";
  103.     if (i == SEG_DATA)
  104.         return "data";
  105.     if (i == SEG_RODATA)
  106.         return "rodata";
  107.     return "?unknown?";
  108. }
  109.  
  110. int
  111. isnumeric(char *s)
  112. {
  113.     while (isdigit(*s))
  114.         s++;
  115.     return (*s == '\0');
  116. }
  117.  
  118. int
  119. zread(int fd, char *b, int n, char *msg)
  120. {
  121.     int    i;
  122.  
  123.     i = read(fd, b, n);
  124.     if (i != n) {
  125.         fprintf(stderr, "Read error: %s: (%d != %d)\n", msg, i, n);
  126.         exit(1);
  127.     }
  128.     return i;
  129. }
  130.  
  131. void
  132. zlseek(int fd, off_t off, char *msg)
  133. {
  134.     off_t    o;
  135.  
  136.     o = lseek(fd, off, SEEK_SET);
  137.     if (o != off) {
  138.         fprintf(stderr, "Lseek error: %s: (%d != %d)\n", msg, (int) o, (int) off);
  139.         exit(1);
  140.     }
  141. }
  142.  
  143. unsigned short
  144. toShort(unsigned char *p)
  145. {
  146.     return (unsigned short) (p[0] | (p[1] << 8));
  147. }
  148.  
  149. unsigned long
  150. toLong(unsigned char *p)
  151. {
  152.     return (unsigned long) ((p[0]) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
  153. }
  154.