home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / binutils-1.8.x.tar.gz / binutils-1.8.x.tar / binutils / hp-bin / ioutil.c < prev    next >
C/C++ Source or Header  |  1989-03-03  |  7KB  |  334 lines

  1. /* I/O Utilities
  2.    Copyright (C) 1988 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <varargs.h>
  20. #include <sys/file.h>
  21. #include <string.h>
  22. #define index(s, c) strchr (s, c)
  23. #include <errno.h>
  24. #include <setjmp.h>
  25. #include "ioutil.h"
  26.  
  27. char *iou_program_name;
  28.  
  29. void
  30. iou_set_program_name (argv)
  31.      char *argv [];
  32. {
  33.   register char *start;
  34.   register char *slash;
  35.  
  36.   start = (argv [0]);
  37.   while (1)
  38.     {
  39.       slash = (index (start, '/'));
  40.       if (slash == NULL) break;
  41.       start = (slash + 1);
  42.     }
  43.   iou_program_name = start;
  44.   return;
  45. }
  46.  
  47. struct abort_list
  48.   {
  49.     struct abort_list *next;
  50.     void (* handler) ();
  51.     jmp_buf catch_point;
  52.   };
  53. typedef struct abort_list *abort_list;
  54.  
  55. static abort_list abort_handlers = NULL;
  56.  
  57. void
  58. iou_abort ()
  59. {
  60.   register abort_list handlers;
  61.  
  62.   handlers = abort_handlers;
  63.   if (handlers == NULL) abort ();
  64.   longjmp ((handlers -> catch_point), 1); /*NOTREACHED*/
  65. }
  66.  
  67. void
  68. iou_abort_handler_bind (thunk, handler)
  69.      void (* thunk) ();
  70.      void (* handler) ();
  71. {
  72.   register abort_list handlers;
  73.   register int setjmp_result;
  74.  
  75.   handlers = ((abort_list) (iou_malloc (sizeof (struct abort_list))));
  76.   (handlers -> next) = abort_handlers;
  77.   (handlers -> handler) = handler;
  78.   abort_handlers = handlers;
  79.   setjmp_result = (setjmp (handlers -> catch_point));
  80.   switch (setjmp_result)
  81.     {
  82.     case 0:
  83.       (* thunk) ();
  84.       if (handlers != abort_handlers)
  85.     iou_fatal_error ("iou_abort_handler_bind: sync error");
  86.       abort_handlers = (handlers -> next);
  87.       iou_free (handlers);
  88.       return;
  89.  
  90.     case 1:
  91.       {
  92.     register abort_list handlers;
  93.     register void (* handler) ();
  94.  
  95.     handlers = abort_handlers;
  96.     handler = (handlers -> handler);
  97.     abort_handlers = (handlers -> next);
  98.     iou_free (handlers);
  99.     (* handler) ();
  100.     return;
  101.       }
  102.  
  103.     default:
  104.       iou_fatal_error ("Illegal setjmp value: %d", setjmp_result);
  105.       /*NOTREACHED*/
  106.     }
  107. }
  108.  
  109. #ifndef hpux
  110. #ifdef bsd
  111.  
  112. static void
  113. vfprintf (stream, format, args)
  114.      FILE *stream;
  115.      char *format;
  116.      char *args;
  117. {
  118.   _doprnt (format, args, stderr);
  119.   return;
  120. }
  121.  
  122. #else
  123. #include "error: no definition for `vfprintf' procedure"
  124. #endif
  125. #endif
  126.  
  127. #define WARN()                                \
  128. {                                    \
  129.   va_list args;                                \
  130.   char *format;                                \
  131.                                     \
  132.   fprintf (stderr, "%s: ", iou_program_name);                \
  133.   va_start (args);                            \
  134.   format = (va_arg (args, char *));                    \
  135.   vfprintf (stderr, format, args);                    \
  136.   va_end (args);                            \
  137.   putc ('\n', stderr);                            \
  138. }
  139.  
  140. /*VARARGS0*/ void
  141. iou_warning (va_alist)
  142.      va_dcl
  143. {
  144.   WARN();
  145.   return;
  146. }
  147.  
  148. /*VARARGS0*/ void
  149. iou_error (va_alist)
  150.      va_dcl
  151. {
  152.   WARN();
  153.   iou_abort (); /*NOTREACHED*/
  154. }
  155.  
  156. /*VARARGS0*/ void
  157. iou_fatal_error (va_alist)
  158.      va_dcl
  159. {
  160.   WARN();
  161.   abort (); /*NOTREACHED*/
  162. }
  163.  
  164. void
  165. iou_call_warning (name)
  166.      char *name;
  167. {
  168.   extern int sys_nerr;
  169.   extern char *sys_errlist [];
  170.  
  171.   if (errno < sys_nerr)
  172.     iou_warning ("%s during system call: %s", (sys_errlist [errno]), name);
  173.   else
  174.     iou_warning ("error code %d during system call: %s", errno, name);
  175.   return;
  176. }
  177.  
  178. void
  179. iou_call_error (name)
  180.      char *name;
  181. {
  182.   iou_call_warning (name);
  183.   iou_abort (); /*NOTREACHED*/
  184. }
  185.  
  186. void
  187. iou_stat (path, buf)
  188.      char *path;
  189.      struct stat *buf;
  190. {
  191.   extern int stat ();
  192.   register int result;
  193.  
  194.   while (1)
  195.     {
  196.       result = (stat (path, buf));
  197.       if (result == 0) break;
  198.       if (errno != EINTR) iou_call_error ("stat");
  199.     }
  200.   return;
  201. }
  202.  
  203. void
  204. iou_unlink (path)
  205.      char *path;
  206. {
  207.   extern int unlink ();
  208.  
  209.   if ((unlink (path)) != 0)
  210.     iou_call_error ("unlink");
  211.   return;
  212. }
  213.  
  214. int
  215. iou_open (path, oflag, mode)
  216.      char *path;
  217.      int oflag;
  218.      int mode;
  219. {
  220.   extern int open ();
  221.   register int result;
  222.  
  223.   while (1)
  224.     {
  225.       result = (open (path, oflag, mode));
  226.       if (result >= 0) break;
  227.       if (errno != EINTR) iou_call_error ("open");
  228.     }
  229.   return (result);
  230. }
  231.  
  232. void
  233. iou_close (descriptor)
  234.      int descriptor;
  235. {
  236.   if ((close (descriptor)) != 0)
  237.     iou_call_error ("close");
  238.   return;
  239. }
  240.  
  241. long
  242. iou_lseek (descriptor, offset, whence)
  243.      int descriptor;
  244.      long offset;
  245.      int whence;
  246. {
  247.   extern long lseek ();
  248.   register long result;
  249.  
  250.   while (1)
  251.     {
  252.       result = (lseek (descriptor, offset, whence));
  253.       if (result >= 0) break;
  254.       if (errno != EINTR) iou_call_error ("lseek");
  255.     }
  256.   return (result);
  257. }
  258.  
  259. int
  260. iou_read (input_file, buffer, length)
  261.      int input_file;
  262.      register char *buffer;
  263.      int length;
  264. {
  265.   extern int read ();
  266.   register int bytes_remaining;
  267.   register int result;
  268.  
  269.   bytes_remaining = length;
  270.   while (bytes_remaining > 0)
  271.     {
  272.       result = (read (input_file, buffer, bytes_remaining));
  273.       if (result == 0) return (length - bytes_remaining);
  274.       if (result > 0)
  275.     {
  276.       buffer += result;
  277.       bytes_remaining -= result;
  278.       continue;
  279.     }
  280.       if (errno != EINTR) iou_call_error ("read");
  281.     }
  282.   return (length);
  283. }
  284.  
  285. void
  286. iou_write (output_file, buffer, length)
  287.      int output_file;
  288.      register char *buffer;
  289.      register int length;
  290. {
  291.   extern int write ();
  292.   register int bytes_remaining;
  293.   register int result;
  294.  
  295.   bytes_remaining = length;
  296.   while (bytes_remaining > 0)
  297.     {
  298.       result = (write (output_file, buffer, bytes_remaining));
  299.       if (result >= 0)
  300.     {
  301.       buffer += result;
  302.       bytes_remaining -= result;
  303.       continue;
  304.     }
  305.       if (errno != EINTR) iou_call_error ("write");
  306.     }
  307.   return;
  308. }
  309.  
  310. char *
  311. iou_malloc (length)
  312.      unsigned length;
  313. {
  314.   extern char *malloc ();
  315.   register char *result;
  316.  
  317.   result = (malloc (length));
  318.   if (result == NULL) iou_error ("virtual memory exhausted");
  319.   return (result);
  320. }
  321.  
  322. char *
  323. iou_realloc (pointer, length)
  324.      char *pointer;
  325.      unsigned length;
  326. {
  327.   extern char *realloc ();
  328.   register char *result;
  329.  
  330.   result = (realloc (pointer, length));
  331.   if (result == NULL) iou_error ("virtual memory exhausted");
  332.   return (result);
  333. }
  334.