ack/lang/pc/libpc/opn.c

120 lines
2.1 KiB
C
Raw Permalink 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 <stdlib.h>
2018-06-17 20:30:27 +00:00
#include <fcntl.h>
#include <unistd.h>
2018-06-17 20:30:27 +00:00
#include "pc.h"
1984-07-20 10:44:57 +00:00
2018-06-17 20:30:27 +00:00
static int tmpfil(void)
2018-06-17 14:11:29 +00:00
{
static char namebuf[] = "/tmp/plf.xxxxx";
2018-06-17 14:11:29 +00:00
int i;
char *p, *q;
1984-07-20 10:44:57 +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';
2018-06-17 14:11:29 +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;
if (close(i) != 0)
1984-07-20 10:44:57 +00:00
goto error;
2018-06-17 14:11:29 +00:00
if ((i = open(p, 2)) < 0)
1984-07-20 10:44:57 +00:00
goto error;
2018-06-17 20:30:27 +00:00
if (unlink(p) != 0)
2018-06-17 14:11:29 +00:00
error:
_trp(EREWR);
return (i);
1984-07-20 10:44:57 +00:00
}
2018-06-17 20:30:27 +00:00
static int initfl(int descr, int sz, struct file* f)
2018-06-17 14:11:29 +00:00
{
1984-07-20 10:44:57 +00:00
int i;
_curfil = f;
2018-06-17 14:11:29 +00:00
if (sz == 0)
{
1984-07-20 10:44:57 +00:00
sz++;
descr |= TXTBIT;
}
2018-06-17 14:11:29 +00:00
for (i = 0; i < _extflc; i++)
1989-05-03 09:53:25 +00:00
if (f == _extfl[i])
1984-07-20 10:44:57 +00:00
break;
2018-06-17 14:11:29 +00:00
if (i >= _extflc)
{ /* local file */
1984-07-20 10:44:57 +00:00
f->fname = "LOCAL";
2018-06-17 14:11:29 +00:00
if ((descr & WRBIT) == 0 && (f->flags & 0377) == MAGIC)
{
1984-07-20 10:44:57 +00:00
_xcls(f);
2018-06-17 14:11:29 +00:00
if (lseek(f->ufd, (long)0, 0) == -1)
1984-07-20 10:44:57 +00:00
_trp(ERESET);
2018-06-17 14:11:29 +00:00
}
else
{
1984-07-20 10:44:57 +00:00
_cls(f);
f->ufd = tmpfil();
}
2018-06-17 14:11:29 +00:00
}
else
{ /* external file */
1989-05-03 09:53:25 +00:00
if (--i <= 0)
2018-06-17 14:11:29 +00:00
return (0);
1984-07-20 10:44:57 +00:00
if (i >= _pargc)
_trp(EARGC);
f->fname = _pargv[i];
_cls(f);
2018-06-17 14:11:29 +00:00
if ((descr & WRBIT) == 0)
{
if ((f->ufd = open(f->fname, 0)) < 0)
1984-07-20 10:44:57 +00:00
_trp(ERESET);
2018-06-17 14:11:29 +00:00
}
else
{
if ((f->ufd = creat(f->fname, 0644)) < 0)
1984-07-20 10:44:57 +00:00
_trp(EREWR);
}
}
2018-06-17 14:11:29 +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;
2018-06-17 14:11:29 +00:00
return (1);
1984-07-20 10:44:57 +00:00
}
2018-06-17 20:30:27 +00:00
void _opn(int sz, struct file* f)
2018-06-17 14:11:29 +00:00
{
1984-07-20 10:44:57 +00:00
2018-06-17 14:11:29 +00:00
if (initfl(MAGIC, sz, f))
1984-07-20 10:44:57 +00:00
f->count = 0;
}
2018-06-17 20:30:27 +00:00
void _cre(int sz, struct file* f)
2018-06-17 14:11:29 +00:00
{
1984-07-20 10:44:57 +00:00
2018-06-17 14:11:29 +00:00
if (initfl(WRBIT | EOFBIT | ELNBIT | MAGIC, sz, f))
1984-07-20 10:44:57 +00:00
f->count = f->buflen;
}