]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/Extras/libh/fseek.c
patch: Maildir support for "scan -file"
[nmh] / docs / historical / mh-jun-1982 / Extras / libh / fseek.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 #include "/h/stat.h"
9 #include "/h/iobuf.h"
10 /* used for input using getc/getw */
11 /* does equivalent to requested seek */
12 /* by doing seek by blocks if necessary, */
13 /* and then adjusting nleft/nextp in iobuf */
14 /* first arg is iobuf pointer, others are identical to seek or lseek */
15 struct { int hiword, loword; };
16 struct { char lobyte, hibyte; };
17
18 fseek(bp, offs, dir)
19 struct iobuf *bp;
20 {
21 long loff;
22
23 loff = offs;
24 if (dir%3 == 0) loff.hiword = 0;
25 return(lfseek(bp, loff, dir));
26 }
27
28 lfseek(abp, aloff, adir)
29 struct iobuf *abp;
30 long aloff;
31 {
32 register byteoff, blockoff;
33 long loff;
34 register struct iobuf *bp;
35 int dir;
36 struct inode statb;
37 long lsize;
38
39 bp = abp;
40 dir = adir;
41 loff = aloff;
42
43 if (dir >= 3) {
44 loff =<< 9;
45 dir =- 3;
46 }
47 switch(dir) {
48 case 1:
49 /* relative seek */
50 if (bp->b_nleft >= 0) {
51 if (bp->b_nextp) {
52 byteoff = bp->b_nextp-bp->b_buff;
53 loff =+ byteoff;
54 bp->b_nleft =+ byteoff;
55 }
56 break;
57 }
58 /* negative nleft means must be at e.o.f */
59 /* drop into rel.-to-end seek */
60 case 2:
61 if (fstat(bp->b_fildes, &statb) < 0)
62 return(-1);
63 lsize.hiword.hibyte = 0;
64 lsize.hiword.lobyte = statb.i_size0;
65 lsize.loword = statb.i_size1;
66 /* adjust loff by size of file */
67 loff =+ lsize;
68 dir = 0; /* now have offset rel.-to-beg. */
69 case 0:
70 /* rel.-to-beginning of file seek */
71 bp->b_nleft = 0;
72 break;
73 default:
74 return(-1);
75 }
76
77 bp->b_nextp = bp->b_buff;
78 blockoff = loff>>9;
79 byteoff = loff;
80 byteoff =& 0777;
81 if (dir != 1 || blockoff != 0) {
82 /* must do the seek */
83 if (seek(bp->b_fildes, blockoff, dir+3) < 0)
84 return(-1);
85 bp->b_nleft = read(bp->b_fildes, &bp->b_buff, 512);
86 }
87 bp->b_nleft =- byteoff;
88 bp->b_nextp =+ byteoff;
89 retur