xref: /original-bsd/usr.bin/f77/libI77/util.c (revision 2301fdfb)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)util.c	5.1	06/07/85
7  */
8 
9 /*
10  * utility routines
11  */
12 
13 #include "fio.h"
14 
15 extern short	ccntrl_, blzero_;
16 
17 nowreading(x) unit *x;
18 {
19 	return(now_acc(x,"r"));
20 }
21 
22 nowwriting(x) unit *x;
23 {
24 	return(now_acc(x,"a"));
25 }
26 
27 LOCAL now_acc(x,mode)
28 unit *x; char *mode;
29 {
30 	long loc;
31 
32 	if (!x->ufnm)
33 	{
34 		errno = EBADF;
35 		return(NO);
36 	}
37 	if (x->useek)
38 		loc=ftell(x->ufd);
39 	if (freopen(x->ufnm,mode,x->ufd))
40 	{
41 		if (x->useek)
42 			fseek(x->ufd,loc,0);
43 		x->uwrt = (*mode=='a');
44 		return(YES);
45 	}
46 	if (x->ufd = fopen(x->ufnm, (*mode=='a')? "r":"a"))
47 		if (x->useek)
48 			fseek(x->ufd,loc,0);
49 	return(NO);
50 }
51 
52 g_char(a,alen,b) char *a,*b; ftnlen alen;
53 {	char *x=a+alen-1, *y=b+alen-1;
54 	while (x >= a  &&  *x == ' ') {x--; y--;}
55 	*(y+1) = '\0';
56 	while (x >= a) *y-- = *x--;
57 }
58 
59 b_char(from, to, tolen) char *from, *to; ftnlen tolen;
60 {	int i=0;
61 	while (*from && i < tolen) {
62 		*to++ = *from++;
63 		i++;
64 	}
65 	while (i++ < tolen)
66 		*to++ = ' ';
67 }
68 
69 char
70 last_char(f) FILE *f;
71 {
72 	fseek(f,-2L,1);
73 	if(ftell(f)) return(getc(f));
74 	else return('\n');
75 }
76