ack/lang/pc/libpc/opn.c

119 lines
2.3 KiB
C
Raw Normal View History

1994-06-24 14:02:31 +00:00
/* $Id$ */
1984-07-20 10:44:57 +00:00
/*
* (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
*
* This product is part of the Amsterdam Compiler Kit.
*
* Permission to use, sell, duplicate or disclose this software must be
* obtained in writing. Requests for such permissions may be sent to
*
* Dr. Andrew S. Tanenbaum
* Wiskundig Seminarium
* Vrije Universiteit
* Postbox 7161
* 1007 MC Amsterdam
* The Netherlands
*
*/
/* Author: J.W. Stevenson */
#include <pc_file.h>
#include <pc_err.h>
1989-05-03 09:53:25 +00:00
extern struct file **_extfl;
extern int _extflc;
1984-07-20 10:44:57 +00:00
extern struct file *_curfil;
extern int _pargc;
extern char **_pargv;
extern char **_penvp;
extern _cls();
extern _xcls();
extern _trp();
1991-02-22 16:57:42 +00:00
extern int _getpid();
extern int _creat();
extern int _open();
extern int _close();
extern int _unlink();
extern long _lseek();
1984-07-20 10:44:57 +00:00
static int tmpfil() {
static char namebuf[] = "/tmp/plf.xxxxx";
1984-07-20 10:44:57 +00:00
int i; char *p,*q;
1991-02-22 16:57:42 +00:00
i = _getpid();
1991-03-20 11:30:35 +00:00
p = namebuf;
1984-07-20 10:44:57 +00:00
q = p + 13;
do
*q++ = (i & 07) + '0';
while (i >>= 3);
*q = '\0';
1991-02-22 16:57:42 +00:00
if ((i = _creat(p,0644)) < 0)
if ((i = _creat(p += 4,0644)) < 0)
if ((i = _creat(p += 5,0644)) < 0)
1984-07-20 10:44:57 +00:00
goto error;
1991-02-22 16:57:42 +00:00
if (_close(i) != 0)
1984-07-20 10:44:57 +00:00
goto error;
1991-02-22 16:57:42 +00:00
if ((i = _open(p,2)) < 0)
1984-07-20 10:44:57 +00:00
goto error;
1991-02-22 16:57:42 +00:00
if (_unlink(p) != 0)
1984-07-20 10:44:57 +00:00
error: _trp(EREWR);
return(i);
}
static int initfl(descr,sz,f) int descr; int sz; struct file *f; {
int i;
_curfil = f;
if (sz == 0) {
sz++;
descr |= TXTBIT;
}
1989-05-03 09:53:25 +00:00
for (i=0; i<_extflc; i++)
if (f == _extfl[i])
1984-07-20 10:44:57 +00:00
break;
1989-05-03 09:53:25 +00:00
if (i >= _extflc) { /* local file */
1984-07-20 10:44:57 +00:00
f->fname = "LOCAL";
if ((descr & WRBIT) == 0 && (f->flags & 0377) == MAGIC) {
_xcls(f);
1991-02-22 16:57:42 +00:00
if (_lseek(f->ufd,(long)0,0) == -1)
1984-07-20 10:44:57 +00:00
_trp(ERESET);
} else {
_cls(f);
f->ufd = tmpfil();
}
} else { /* external file */
1989-05-03 09:53:25 +00:00
if (--i <= 0)
1984-07-20 10:44:57 +00:00
return(0);
if (i >= _pargc)
_trp(EARGC);
f->fname = _pargv[i];
_cls(f);
if ((descr & WRBIT) == 0) {
1991-02-22 16:57:42 +00:00
if ((f->ufd = _open(f->fname,0)) < 0)
1984-07-20 10:44:57 +00:00
_trp(ERESET);
} else {
1991-02-22 16:57:42 +00:00
if ((f->ufd = _creat(f->fname,0644)) < 0)
1984-07-20 10:44:57 +00:00
_trp(EREWR);
}
}
1989-05-03 09:53:25 +00:00
f->buflen = (sz>PC_BUFLEN ? sz : PC_BUFLEN-PC_BUFLEN%sz);
1984-07-20 10:44:57 +00:00
f->size = sz;
f->ptr = f->bufadr;
f->flags = descr;
return(1);
}
_opn(sz,f) int sz; struct file *f; {
if (initfl(MAGIC,sz,f))
f->count = 0;
}
_cre(sz,f) int sz; struct file *f; {
if (initfl(WRBIT|EOFBIT|ELNBIT|MAGIC,sz,f))
f->count = f->buflen;
}