home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / f2c / i77lib / endfile.c < prev    next >
C/C++ Source or Header  |  2000-05-29  |  2KB  |  117 lines

  1. #include "f2c.h"
  2. #include "fio.h"
  3. #include "sys/types.h"
  4. #include "fcntl.h"
  5. #include "rawio.h"
  6. #ifndef O_RDONLY
  7. #define O_RDONLY 0
  8. #endif
  9.  
  10. #ifdef KR_headers
  11. extern char *strcpy();
  12. #else
  13. #undef abs
  14. #undef min
  15. #undef max
  16. #include "stdlib.h"
  17. #include "string.h"
  18. #endif
  19.  
  20.  
  21. #ifdef KR_headers
  22. integer f_end(a) alist *a;
  23. #else
  24. integer f_end(alist *a)
  25. #endif
  26. {
  27.     unit *b;
  28.     if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
  29.     b = &f__units[a->aunit];
  30.     if(b->ufd==NULL) {
  31.         char nbuf[10];
  32.         (void) sprintf(nbuf,"fort.%ld",a->aunit);
  33.         close(creat(nbuf, 0666));
  34.         return(0);
  35.         }
  36.     b->uend=1;
  37.     return(b->useek ? t_runc(a) : 0);
  38. }
  39.  
  40.  static int
  41. #ifdef KR_headers
  42. copy(from, len, to) char *from, *to; register long len;
  43. #else
  44. copy(char *from, register long len, char *to)
  45. #endif
  46. {
  47.     register int n;
  48.     int k, rc = 0, tmp;
  49.     char buf[BUFSIZ];
  50.  
  51.     if ((k = open(from, O_RDONLY)) < 0)
  52.         return 1;
  53.     if ((tmp = creat(to,0666)) < 0)
  54.         return 1;
  55.     while((n = read(k, buf, len > BUFSIZ ? BUFSIZ : (int)len)) > 0) {
  56.         if (write(tmp, buf, n) != n)
  57.             { rc = 1; break; }
  58.         if ((len -= n) <= 0)
  59.             break;
  60.         }
  61.     close(k);
  62.     close(tmp);
  63.     return n < 0 ? 1 : rc;
  64.     }
  65.  
  66. #ifndef L_tmpnam
  67. #define L_tmpnam 16
  68. #endif
  69.  
  70.  int
  71. #ifdef KR_headers
  72. t_runc(a) alist *a;
  73. #else
  74. t_runc(alist *a)
  75. #endif
  76. {
  77.     char nm[L_tmpnam];
  78.     long loc, len;
  79.     unit *b;
  80.     int rc = 0;
  81.  
  82.     b = &f__units[a->aunit];
  83.     if(b->url) return(0);    /*don't truncate direct files*/
  84.     loc=ftell(b->ufd);
  85.     (void) fseek(b->ufd,0L,SEEK_END);
  86.     len=ftell(b->ufd);
  87.     if (loc >= len || b->useek == 0 || b->ufnm == NULL)
  88.         return(0);
  89.     rewind(b->ufd);    /* empty buffer */
  90.     if (!loc) {
  91.         if (close(creat(b->ufnm,0666)))
  92.             { rc = 1; goto done; }
  93.         if (b->uwrt)
  94.             b->uwrt = 1;
  95.         return 0;
  96.         }
  97. #ifdef _POSIX_SOURCE
  98.     tmpnam(nm);
  99. #else
  100. #ifdef _AMIGA
  101.     tmpnam(nm);
  102. #else
  103.     (void) strcpy(nm,"tmp.FXXXXXX");
  104.     (void) mktemp(nm);
  105. #endif
  106. #endif
  107.     if (copy(b->ufnm, loc, nm)
  108.      || copy(nm, loc, b->ufnm))
  109.         rc = 1;
  110.     unlink(nm);
  111. done:
  112.     fseek(b->ufd, loc, SEEK_SET);
  113.     if (rc)
  114.         err(a->aerr,111,"endfile");
  115.     return 0;
  116.     }
  117.