1 | /* $Header: /cvsup/minix/src/lib/ack/libp/opn.c,v 1.1 2005/10/10 15:27:47 beng Exp $ */
|
---|
2 | /*
|
---|
3 | * (c) copyright 1983 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
---|
4 | *
|
---|
5 | * This product is part of the Amsterdam Compiler Kit.
|
---|
6 | *
|
---|
7 | * Permission to use, sell, duplicate or disclose this software must be
|
---|
8 | * obtained in writing. Requests for such permissions may be sent to
|
---|
9 | *
|
---|
10 | * Dr. Andrew S. Tanenbaum
|
---|
11 | * Wiskundig Seminarium
|
---|
12 | * Vrije Universiteit
|
---|
13 | * Postbox 7161
|
---|
14 | * 1007 MC Amsterdam
|
---|
15 | * The Netherlands
|
---|
16 | *
|
---|
17 | */
|
---|
18 |
|
---|
19 | /* Author: J.W. Stevenson */
|
---|
20 |
|
---|
21 | #include <pc_file.h>
|
---|
22 | #include <pc_err.h>
|
---|
23 |
|
---|
24 | extern struct file **_extfl;
|
---|
25 | extern int _extflc;
|
---|
26 | extern struct file *_curfil;
|
---|
27 | extern int _pargc;
|
---|
28 | extern char **_pargv;
|
---|
29 | extern char ***_penviron;
|
---|
30 |
|
---|
31 | extern _cls();
|
---|
32 | extern _xcls();
|
---|
33 | extern _trp();
|
---|
34 | extern int _getpid();
|
---|
35 | extern int _creat();
|
---|
36 | extern int _open();
|
---|
37 | extern int _close();
|
---|
38 | extern int _unlink();
|
---|
39 | extern long _lseek();
|
---|
40 |
|
---|
41 | static int tmpfil() {
|
---|
42 | static char namebuf[] = "/usr/tmp/plf.xxxxx";
|
---|
43 | int i; char *p,*q;
|
---|
44 |
|
---|
45 | i = _getpid();
|
---|
46 | p = namebuf;
|
---|
47 | q = p + 13;
|
---|
48 | do
|
---|
49 | *q++ = (i & 07) + '0';
|
---|
50 | while (i >>= 3);
|
---|
51 | *q = '\0';
|
---|
52 | if ((i = _creat(p,0644)) < 0)
|
---|
53 | if ((i = _creat(p += 4,0644)) < 0)
|
---|
54 | if ((i = _creat(p += 5,0644)) < 0)
|
---|
55 | goto error;
|
---|
56 | if (_close(i) != 0)
|
---|
57 | goto error;
|
---|
58 | if ((i = _open(p,2)) < 0)
|
---|
59 | goto error;
|
---|
60 | if (_unlink(p) != 0)
|
---|
61 | error: _trp(EREWR);
|
---|
62 | return(i);
|
---|
63 | }
|
---|
64 |
|
---|
65 | static int initfl(descr,sz,f) int descr; int sz; struct file *f; {
|
---|
66 | int i;
|
---|
67 |
|
---|
68 | _curfil = f;
|
---|
69 | if (sz == 0) {
|
---|
70 | sz++;
|
---|
71 | descr |= TXTBIT;
|
---|
72 | }
|
---|
73 | for (i=0; i<_extflc; i++)
|
---|
74 | if (f == _extfl[i])
|
---|
75 | break;
|
---|
76 | if (i >= _extflc) { /* local file */
|
---|
77 | f->fname = "LOCAL";
|
---|
78 | if ((descr & WRBIT) == 0 && (f->flags & 0377) == MAGIC) {
|
---|
79 | _xcls(f);
|
---|
80 | if (_lseek(f->ufd,(long)0,0) == -1)
|
---|
81 | _trp(ERESET);
|
---|
82 | } else {
|
---|
83 | _cls(f);
|
---|
84 | f->ufd = tmpfil();
|
---|
85 | }
|
---|
86 | } else { /* external file */
|
---|
87 | if (--i <= 0)
|
---|
88 | return(0);
|
---|
89 | if (i >= _pargc)
|
---|
90 | _trp(EARGC);
|
---|
91 | f->fname = _pargv[i];
|
---|
92 | _cls(f);
|
---|
93 | if ((descr & WRBIT) == 0) {
|
---|
94 | if ((f->ufd = _open(f->fname,0)) < 0)
|
---|
95 | _trp(ERESET);
|
---|
96 | } else {
|
---|
97 | if ((f->ufd = _creat(f->fname,0644)) < 0)
|
---|
98 | _trp(EREWR);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | f->buflen = (sz>PC_BUFLEN ? sz : PC_BUFLEN-PC_BUFLEN%sz);
|
---|
102 | f->size = sz;
|
---|
103 | f->ptr = f->bufadr;
|
---|
104 | f->flags = descr;
|
---|
105 | return(1);
|
---|
106 | }
|
---|
107 |
|
---|
108 | _opn(sz,f) int sz; struct file *f; {
|
---|
109 |
|
---|
110 | if (initfl(MAGIC,sz,f))
|
---|
111 | f->count = 0;
|
---|
112 | }
|
---|
113 |
|
---|
114 | _cre(sz,f) int sz; struct file *f; {
|
---|
115 |
|
---|
116 | if (initfl(WRBIT|EOFBIT|ELNBIT|MAGIC,sz,f))
|
---|
117 | f->count = f->buflen;
|
---|
118 | }
|
---|