xref: /original-bsd/usr.bin/f77/libI77/util.c (revision 6c57d260)
1 /*
2 char id_util[] = "@(#)util.c	1.4";
3  *
4  * utility routines
5  */
6 
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include "fio.h"
10 
11 
12 ini_std(u,F,w,i66) FILE *F;
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;
20 	p->uscrtch = p->uend = NO;
21 	p->ublnk = p->uprnt = (i66!=0)? YES : NO;
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 {
34 	long loc;
35 	x->uwrt = NO;
36 	loc=ftell(x->ufd);
37 	freopen(x->ufnm,"r",x->ufd);
38 	fseek(x->ufd,loc,0);
39 }
40 
41 nowwriting(x) unit *x;
42 {
43 	long loc;
44 	x->uwrt = YES;
45 	loc=ftell(x->ufd);
46 	freopen(x->ufnm,"a",x->ufd);
47 	fseek(x->ufd,loc,0);
48 }
49 
50 g_char(a,alen,b) char *a,*b; ftnlen alen;
51 {	char *x=a+alen-1, *y=b+alen-1;
52 	while (x >= a  &&  *x == ' ') {x--; y--;}
53 	*(y+1) = '\0';
54 	while (x >= a) *y-- = *x--;
55 }
56 
57 b_char(from, to, tolen) char *from, *to; ftnlen tolen;
58 {	int i=0;
59 	while (*from && i < tolen) {
60 		*to++ = *from++;
61 		i++;
62 	}
63 	while (i++ < tolen)
64 		*to++ = ' ';
65 }
66 
67 inode(a) char *a;
68 {	struct stat x;
69 	if(stat(a,&x)==0) return(x.st_ino);
70 	else return(-1);
71 }
72 
73 finode(f) FILE *f;
74 {	struct stat x;
75 	if(fstat(fileno(f),&x)==0) return(x.st_ino);
76 	else return(-1);
77 }
78 
79 char
80 last_char(f) FILE *f;
81 {
82 	fseek(f,-2L,1);
83 	if(ftell(f)) return(getc(f));
84 	else return('\n');
85 }
86