backspac.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "f2c.h"
  2. #include "fio.h"
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifdef KR_headers
  7. integer f_back(a) alist *a;
  8. #else
  9. integer f_back(alist *a)
  10. #endif
  11. { unit *b;
  12. OFF_T v, w, x, y, z;
  13. uiolen n;
  14. FILE *f;
  15. f__curunit = b = &f__units[a->aunit]; /* curunit for error messages */
  16. if(a->aunit >= MXUNIT || a->aunit < 0)
  17. err(a->aerr,101,"backspace")
  18. if(b->useek==0) err(a->aerr,106,"backspace")
  19. if(b->ufd == NULL) {
  20. fk_open(1, 1, a->aunit);
  21. return(0);
  22. }
  23. if(b->uend==1)
  24. { b->uend=0;
  25. return(0);
  26. }
  27. if(b->uwrt) {
  28. t_runc(a);
  29. if (f__nowreading(b))
  30. err(a->aerr,errno,"backspace")
  31. }
  32. f = b->ufd; /* may have changed in t_runc() */
  33. if(b->url>0)
  34. {
  35. x=FTELL(f);
  36. y = x % b->url;
  37. if(y == 0) x--;
  38. x /= b->url;
  39. x *= b->url;
  40. (void) FSEEK(f,x,SEEK_SET);
  41. return(0);
  42. }
  43. if(b->ufmt==0)
  44. { FSEEK(f,-(OFF_T)sizeof(uiolen),SEEK_CUR);
  45. fread((char *)&n,sizeof(uiolen),1,f);
  46. FSEEK(f,-(OFF_T)n-2*sizeof(uiolen),SEEK_CUR);
  47. return(0);
  48. }
  49. w = x = FTELL(f);
  50. z = 0;
  51. loop:
  52. while(x) {
  53. x -= x < 64 ? x : 64;
  54. FSEEK(f,x,SEEK_SET);
  55. for(y = x; y < w; y++) {
  56. if (getc(f) != '\n')
  57. continue;
  58. v = FTELL(f);
  59. if (v == w) {
  60. if (z)
  61. goto break2;
  62. goto loop;
  63. }
  64. z = v;
  65. }
  66. err(a->aerr,(EOF),"backspace")
  67. }
  68. break2:
  69. FSEEK(f, z, SEEK_SET);
  70. return 0;
  71. }
  72. #ifdef __cplusplus
  73. }
  74. #endif