endfile.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "f2c.h"
  2. #include "fio.h"
  3. /* Compile this with -DNO_TRUNCATE if unistd.h does not exist or */
  4. /* if it does not define int truncate(const char *name, off_t). */
  5. #ifdef MSDOS
  6. #undef NO_TRUNCATE
  7. #define NO_TRUNCATE
  8. #endif
  9. #ifndef NO_TRUNCATE
  10. //#include "unistd.h"
  11. #endif
  12. #ifdef KR_headers
  13. extern char *strcpy();
  14. extern FILE *tmpfile();
  15. #else
  16. #undef abs
  17. #undef min
  18. #undef max
  19. #include "stdlib.h"
  20. #include "string.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #endif
  25. extern char *f__r_mode[], *f__w_mode[];
  26. #ifdef KR_headers
  27. integer f_end(a) alist *a;
  28. #else
  29. integer f_end(alist *a)
  30. #endif
  31. {
  32. unit *b;
  33. FILE *tf;
  34. if(a->aunit>=MXUNIT || a->aunit<0) err(a->aerr,101,"endfile");
  35. b = &f__units[a->aunit];
  36. if(b->ufd==NULL) {
  37. char nbuf[10];
  38. sprintf(nbuf,"fort.%ld",(long)a->aunit);
  39. if (tf = FOPEN(nbuf, f__w_mode[0]))
  40. fclose(tf);
  41. return(0);
  42. }
  43. b->uend=1;
  44. return(b->useek ? t_runc(a) : 0);
  45. }
  46. #ifdef NO_TRUNCATE
  47. static int
  48. #ifdef KR_headers
  49. copy(from, len, to) FILE *from, *to; register long len;
  50. #else
  51. copy(FILE *from, register long len, FILE *to)
  52. #endif
  53. {
  54. int len1;
  55. char buf[BUFSIZ];
  56. while(fread(buf, len1 = len > BUFSIZ ? BUFSIZ : (int)len, 1, from)) {
  57. if (!fwrite(buf, len1, 1, to))
  58. return 1;
  59. if ((len -= len1) <= 0)
  60. break;
  61. }
  62. return 0;
  63. }
  64. #endif /* NO_TRUNCATE */
  65. int
  66. #ifdef KR_headers
  67. t_runc(a) alist *a;
  68. #else
  69. t_runc(alist *a)
  70. #endif
  71. {
  72. OFF_T loc, len;
  73. unit *b;
  74. int rc;
  75. FILE *bf;
  76. #ifdef NO_TRUNCATE
  77. FILE *tf;
  78. #endif
  79. b = &f__units[a->aunit];
  80. if(b->url)
  81. return(0); /*don't truncate direct files*/
  82. loc=FTELL(bf = b->ufd);
  83. FSEEK(bf,(OFF_T)0,SEEK_END);
  84. len=FTELL(bf);
  85. if (loc >= len || b->useek == 0)
  86. return(0);
  87. #ifdef NO_TRUNCATE
  88. if (b->ufnm == NULL)
  89. return 0;
  90. rc = 0;
  91. fclose(b->ufd);
  92. if (!loc) {
  93. if (!(bf = FOPEN(b->ufnm, f__w_mode[b->ufmt])))
  94. rc = 1;
  95. if (b->uwrt)
  96. b->uwrt = 1;
  97. goto done;
  98. }
  99. if (!(bf = FOPEN(b->ufnm, f__r_mode[0]))
  100. || !(tf = tmpfile())) {
  101. #ifdef NON_UNIX_STDIO
  102. bad:
  103. #endif
  104. rc = 1;
  105. goto done;
  106. }
  107. if (copy(bf, (long)loc, tf)) {
  108. bad1:
  109. rc = 1;
  110. goto done1;
  111. }
  112. if (!(bf = FREOPEN(b->ufnm, f__w_mode[0], bf)))
  113. goto bad1;
  114. rewind(tf);
  115. if (copy(tf, (long)loc, bf))
  116. goto bad1;
  117. b->uwrt = 1;
  118. b->urw = 2;
  119. #ifdef NON_UNIX_STDIO
  120. if (b->ufmt) {
  121. fclose(bf);
  122. if (!(bf = FOPEN(b->ufnm, f__w_mode[3])))
  123. goto bad;
  124. FSEEK(bf,(OFF_T)0,SEEK_END);
  125. b->urw = 3;
  126. }
  127. #endif
  128. done1:
  129. fclose(tf);
  130. done:
  131. f__cf = b->ufd = bf;
  132. #else /* NO_TRUNCATE */
  133. if (b->urw & 2)
  134. fflush(b->ufd); /* necessary on some Linux systems */
  135. #ifndef FTRUNCATE
  136. #define FTRUNCATE ftruncate
  137. #endif
  138. rc = FTRUNCATE(fileno(b->ufd), loc);
  139. /* The following FSEEK is unnecessary on some systems, */
  140. /* but should be harmless. */
  141. FSEEK(b->ufd, (OFF_T)0, SEEK_END);
  142. #endif /* NO_TRUNCATE */
  143. if (rc)
  144. err(a->aerr,111,"endfile");
  145. return 0;
  146. }
  147. #ifdef __cplusplus
  148. }
  149. #endif