xref: /csrg-svn/usr.bin/f77/libI77/util.c (revision 2602)
1 /*
2 char id_util[] = "@(#)util.c	1.2";
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) 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->ublnk = p->uscrtch = p->uprnt = p->uend = NO;
21 	p->url = 0;
22 	p->uinode = finode(F);
23 }
24 
25 canseek(f) FILE *f; /*SYSDEP*/
26 {	struct stat x;
27 	return( (fstat(fileno(f),&x)==0) &&
28 	(x.st_nlink > 0 /*!pipe*/) && !isatty(fileno(f)) );
29 }
30 
31 nowreading(x) unit *x;
32 {
33 	long loc;
34 	x->uwrt = NO;
35 	loc=ftell(x->ufd);
36 	freopen(x->ufnm,"r",x->ufd);
37 	fseek(x->ufd,loc,0);
38 }
39 
40 nowwriting(x) unit *x;
41 {
42 	long loc;
43 	x->uwrt = YES;
44 	loc=ftell(x->ufd);
45 	freopen(x->ufnm,"a",x->ufd);
46 	fseek(x->ufd,loc,0);
47 }
48 
49 g_char(a,alen,b) char *a,*b; ftnlen alen;
50 {	char *x=a+alen-1, *y=b+alen-1;
51 	while (x >= a  &&  *x == ' ') {x--; y--;}
52 	*(y+1) = '\0';
53 	while (x >= a) *y-- = *x--;
54 }
55 
56 b_char(from, to, tolen) char *from, *to; ftnlen tolen;
57 {	int i=0;
58 	while (*from && i < tolen) {
59 		*to++ = *from++;
60 		i++;
61 	}
62 	while (i++ < tolen)
63 		*to++ = ' ';
64 }
65 
66 inode(a) char *a;
67 {	struct stat x;
68 	if(stat(a,&x)==0) return(x.st_ino);
69 	else return(-1);
70 }
71 
72 finode(f) FILE *f;
73 {	struct stat x;
74 	if(fstat(fileno(f),&x)==0) return(x.st_ino);
75 	else return(-1);
76 }
77 
78 char
79 last_char(f) FILE *f;
80 {
81 	fseek(f,-2L,1);
82 	if(ftell(f)) return(getc(f));
83 	else return('\n');
84 }
85