home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / microcode / bchutl.c < prev    next >
C/C++ Source or Header  |  2000-12-05  |  3KB  |  128 lines

  1. /* -*-C-*-
  2.  
  3. $Id: bchutl.c,v 1.11 2000/12/05 21:23:43 cph Exp $
  4.  
  5. Copyright (c) 1991-2000 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "config.h"
  23. #include <stdio.h>
  24.  
  25. #include <errno.h>
  26. #ifndef EINTR
  27. #  define EINTR 1999
  28. #endif
  29.  
  30. #ifdef HAVE_UNISTD_H
  31. #  include <unistd.h>
  32. #endif
  33.  
  34. #ifdef __WIN32__
  35.  
  36. #define lseek _lseek
  37.  
  38. char *
  39. DEFUN (error_name, (code), int code)
  40. {
  41.   static char buf[512];
  42.  
  43.   sprintf (&buf[0], "%d, unknown error", code);
  44.   return (&buf[0]);
  45. }
  46.  
  47. #else /* not __WIN32__ */
  48. #ifdef __OS2__
  49.  
  50. #if defined(__IBMC__) || defined(__WATCOMC__) || defined(__EMX__)
  51. #include <io.h>
  52. #endif
  53.  
  54. char *
  55. DEFUN (error_name, (code), int code)
  56. {
  57.   static char buf [512];
  58.   sprintf ((&buf[0]), "%d, unknown error", code);
  59.   return (&buf[0]);
  60. }
  61.  
  62. #else /* not __OS2__ */
  63.  
  64. char *
  65. DEFUN (error_name, (code), int code)
  66. {
  67.   static char buf[512];
  68.  
  69.   if ((code >= 0) && (code <= sys_nerr))
  70.     sprintf (&buf[0], "%d, %s", code, sys_errlist[code]);
  71.   else
  72.     sprintf (&buf[0], "%d, unknown error", code);
  73.   return (&buf[0]);
  74. }
  75.  
  76. #endif /* not __OS2__ */
  77. #endif /* not __WIN32__ */
  78.  
  79. #ifndef SEEK_SET
  80. #define SEEK_SET 0
  81. #endif
  82.  
  83. int
  84. DEFUN (retrying_file_operation,
  85.        (operation, fid, ptr, position, nbytes, name, noise, curpos, abort_p),
  86.        int EXFUN ((*operation), (int, char *, unsigned int))
  87.        AND int fid AND char * ptr AND long position AND long nbytes
  88.        AND char * name AND char * noise AND long * curpos
  89.        AND int EXFUN ((*abort_p), (char *, char *)))
  90. {
  91.   char * membuf = ptr;
  92.   long
  93.     bytes_to_transfer = nbytes,
  94.     bytes_transferred;
  95.  
  96.   if (*curpos != position)
  97.     while ((lseek (fid, position, SEEK_SET)) == -1)
  98.       if ((errno != EINTR) && ((*abort_p) ("lseek", noise)))
  99.     goto fail;
  100.  
  101.   while ((bytes_to_transfer > 0)
  102.      && ((bytes_transferred =
  103.           ((*operation) (fid, membuf, ((unsigned int) bytes_to_transfer))))
  104.          != bytes_to_transfer))
  105.     if (bytes_transferred == -1)
  106.     {
  107.       if ((errno != EINTR) && ((*abort_p) (name, noise)))
  108.     goto fail;
  109.  
  110.       while ((lseek (fid, (position + (nbytes - bytes_to_transfer)), SEEK_SET))
  111.          == -1)
  112.     if ((errno != EINTR) && ((*abort_p) ("lseek", noise)))
  113.       goto fail;
  114.     }
  115.     else
  116.     {
  117.       bytes_to_transfer -= bytes_transferred;
  118.       membuf += bytes_transferred;
  119.     }
  120.   *curpos = (position + nbytes);
  121.   return (nbytes);
  122.  
  123. fail:
  124.   *curpos = -1;
  125.   return (-1);
  126. }
  127.  
  128.