Commit | Line | Data |
---|---|---|
3443cb26 | 1 | /* |
ff26b80a | 2 | char id_util[] = "@(#)util.c 1.5"; |
3443cb26 DW |
3 | * |
4 | * utility routines | |
5 | */ | |
6 | ||
7 | #include <sys/types.h> | |
8 | #include <sys/stat.h> | |
9 | #include "fio.h" | |
10 | ||
11 | ||
dba44bf1 | 12 | ini_std(u,F,w,i66) FILE *F; |
3443cb26 DW |
13 | { unit *p; |
14 | p = &units[u]; | |
15 | p->ufd = F; | |
16 | p->ufnm = NULL; | |
17 | p->useek = canseek(F); | |
18 | p->ufmt = YES; | |
19 | p->uwrt = (w==WRITE)? YES : NO; | |
656fbd79 DW |
20 | p->uscrtch = p->uend = NO; |
21 | p->ublnk = p->uprnt = (i66!=0)? YES : NO; | |
3443cb26 DW |
22 | p->url = 0; |
23 | p->uinode = finode(F); | |
24 | } | |
25 | ||
26 | canseek(f) FILE *f; /*SYSDEP*/ | |
27 | { struct stat x; | |
28 | return( (fstat(fileno(f),&x)==0) && | |
29 | (x.st_nlink > 0 /*!pipe*/) && !isatty(fileno(f)) ); | |
30 | } | |
31 | ||
32 | nowreading(x) unit *x; | |
33 | { | |
ff26b80a | 34 | return(now_acc(x,"r")); |
3443cb26 DW |
35 | } |
36 | ||
37 | nowwriting(x) unit *x; | |
ff26b80a DW |
38 | { |
39 | return(now_acc(x,"a")); | |
40 | } | |
41 | ||
42 | now_acc(x,mode) | |
43 | unit *x; char *mode; | |
3443cb26 DW |
44 | { |
45 | long loc; | |
ff26b80a DW |
46 | |
47 | if (!x->ufnm) | |
48 | { | |
49 | errno = EBADF; | |
50 | return(NO); | |
51 | } | |
52 | if (x->useek) | |
53 | loc=ftell(x->ufd); | |
54 | if (freopen(x->ufnm,mode,x->ufd)) | |
55 | { | |
56 | if (x->useek) | |
57 | fseek(x->ufd,loc,0); | |
58 | x->uwrt = (*mode=='a'); | |
59 | return(YES); | |
60 | } | |
61 | if (x->ufd = fopen(x->ufnm, (*mode=='a')? "r":"a")) | |
62 | if (x->useek) | |
63 | fseek(x->ufd,loc,0); | |
64 | return(NO); | |
3443cb26 DW |
65 | } |
66 | ||
67 | g_char(a,alen,b) char *a,*b; ftnlen alen; | |
68 | { char *x=a+alen-1, *y=b+alen-1; | |
69 | while (x >= a && *x == ' ') {x--; y--;} | |
70 | *(y+1) = '\0'; | |
71 | while (x >= a) *y-- = *x--; | |
72 | } | |
73 | ||
74 | b_char(from, to, tolen) char *from, *to; ftnlen tolen; | |
75 | { int i=0; | |
76 | while (*from && i < tolen) { | |
77 | *to++ = *from++; | |
78 | i++; | |
79 | } | |
80 | while (i++ < tolen) | |
81 | *to++ = ' '; | |
82 | } | |
83 | ||
84 | inode(a) char *a; | |
85 | { struct stat x; | |
86 | if(stat(a,&x)==0) return(x.st_ino); | |
87 | else return(-1); | |
88 | } | |
89 | ||
90 | finode(f) FILE *f; | |
91 | { struct stat x; | |
92 | if(fstat(fileno(f),&x)==0) return(x.st_ino); | |
93 | else return(-1); | |
94 | } | |
95 | ||
96 | char | |
97 | last_char(f) FILE *f; | |
98 | { | |
99 | fseek(f,-2L,1); | |
100 | if(ftell(f)) return(getc(f)); | |
101 | else return('\n'); | |
102 | } |