home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / quartz / quartz10.lha / src / common / util.c next >
Encoding:
C/C++ Source or Header  |  1990-05-08  |  1.6 KB  |  108 lines

  1. #include <stdio.h>
  2.  
  3. extern char *malloc(), *realloc();
  4. extern char *sys_errlist[];
  5. extern int sys_nerr, errno;
  6. void MyError();
  7.  
  8. void MySysError (s, file)
  9.     char *s, *file;
  10.     {
  11.     fprintf(stderr, "%s %s", s, file); 
  12.     if (errno < sys_nerr)
  13.         fprintf(stderr, "%s", sys_errlist[errno]);
  14.     fprintf(stderr, "\n");
  15.     fflush(stderr);
  16.     exit(1);
  17.     }
  18.  
  19. FILE *MyOpen (file, mode)
  20.     char *file, *mode;
  21.     {
  22.     FILE *fd;
  23.  
  24.     if ((fd = fopen(file, mode)) == NULL)
  25.         MyError("Unable to open", file);
  26.     return(fd);
  27.     }
  28.  
  29. void MyClose (fd)
  30.     FILE *fd;
  31.     {
  32.     if (fclose(fd) == EOF)
  33.         perror("Warning: ");
  34.     }
  35.  
  36. void myread (p, s, n, fd)
  37.     char *p;
  38.     int s, n;
  39.     FILE *fd;
  40.     {
  41.     if (fread(p, s, n, fd) != n)
  42.         MySysError("Unable to read", "");
  43.     }
  44.  
  45. void mywrite (p, s, n, fd)
  46.     char *p;
  47.     int s, n;
  48.     FILE *fd;
  49.     {
  50.     if (fwrite(p, s, n, fd) != n)
  51.         MySysError("Unable to write", "");
  52.     }
  53.  
  54. int MyFileLen (fd)
  55.     FILE *fd;
  56.     {
  57.     long old;
  58.     long n;
  59.  
  60.     if ((old = ftell(fd)) == -1)
  61.         MySysError("Unable to ftell", "");
  62.     if (fseek(fd, (long)0, 2) == -1)
  63.         MySysError("Unable to seek", "");
  64.     if ((n = ftell(fd)) == -1)
  65.         MySysError("Unable to ftell", "");
  66.     if (fseek(fd, old, 0) == -1)
  67.         MySysError("Unable to seek", "");
  68.     return((int)(n - old));
  69.     }
  70.  
  71. void MySeek (fd, off, ptr)
  72.     FILE *fd;
  73.     long off;
  74.     {
  75.     if (fseek(fd, off, ptr) == -1)
  76.         MySysError("Unable to seek", "");
  77.     }
  78.  
  79. char *mymalloc (n) 
  80.     unsigned n;
  81.     {
  82.     char *p;
  83.  
  84.     if ((p = malloc(n)) == NULL) 
  85.         MyError("Out of space", "");
  86.     return(p);
  87.     }
  88.  
  89. char *myrealloc (p, n) 
  90.     char *p;
  91.     unsigned n;
  92.     {
  93.     char *np;
  94.  
  95.     if ((np = realloc(p, n)) == NULL) 
  96.         MyError("Out of space", "");
  97.     return(np);
  98.     }
  99.  
  100. void MyError (s, t)
  101.     char *s, *t;
  102.     {
  103.     fprintf(stderr, "%s %s\n", s, t);
  104.     fflush(stderr);
  105.     exit(1);
  106.     }
  107.  
  108.