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