forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopn.c
More file actions
executable file
·118 lines (106 loc) · 2.33 KB
/
Copy pathopn.c
File metadata and controls
executable file
·118 lines (106 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* $Header$ */
/*
* (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>
extern struct file **_extfl;
extern int _extflc;
extern struct file *_curfil;
extern int _pargc;
extern char **_pargv;
extern char ***_penviron;
extern _cls();
extern _xcls();
extern _trp();
extern int _getpid();
extern int _creat();
extern int _open();
extern int _close();
extern int _unlink();
extern long _lseek();
static int tmpfil() {
static char namebuf[] = "/usr/tmp/plf.xxxxx";
int i; char *p,*q;
i = _getpid();
p = namebuf;
q = p + 13;
do
*q++ = (i & 07) + '0';
while (i >>= 3);
*q = '\0';
if ((i = _creat(p,0644)) < 0)
if ((i = _creat(p += 4,0644)) < 0)
if ((i = _creat(p += 5,0644)) < 0)
goto error;
if (_close(i) != 0)
goto error;
if ((i = _open(p,2)) < 0)
goto error;
if (_unlink(p) != 0)
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;
}
for (i=0; i<_extflc; i++)
if (f == _extfl[i])
break;
if (i >= _extflc) { /* local file */
f->fname = "LOCAL";
if ((descr & WRBIT) == 0 && (f->flags & 0377) == MAGIC) {
_xcls(f);
if (_lseek(f->ufd,(long)0,0) == -1)
_trp(ERESET);
} else {
_cls(f);
f->ufd = tmpfil();
}
} else { /* external file */
if (--i <= 0)
return(0);
if (i >= _pargc)
_trp(EARGC);
f->fname = _pargv[i];
_cls(f);
if ((descr & WRBIT) == 0) {
if ((f->ufd = _open(f->fname,0)) < 0)
_trp(ERESET);
} else {
if ((f->ufd = _creat(f->fname,0644)) < 0)
_trp(EREWR);
}
}
f->buflen = (sz>PC_BUFLEN ? sz : PC_BUFLEN-PC_BUFLEN%sz);
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;
}