|
|
1.1 root 1:
2:
3: open() COHERENT System Call open()
4:
5:
6:
7:
8: Open a file
9:
10: iinntt ooppeenn(_f_i_l_e, _t_y_p_e) cchhaarr *_f_i_l_e; iinntt _t_y_p_e;
11:
12: open opens a file to receive data, or to have its data read.
13: When it opens file, open returns a file descriptor, which is a
14: small, positive integer that identifies the open file for subse-
15: quent calls to read, write, close, dup, or dup2. type determines
16: how the file is opened, as follows:
17:
18:
19: 00 Read only
20: 11 Write
21: 22 Read and write
22:
23:
24: After file is opened, reading or writing begins at byte 0.
25:
26: ***** Example *****
27:
28: This example copies the file named in argv[1] to the one named in
29: argv[2] by using COHERENT system calls. It demonstrates the sys-
30: tem calls open, close, read, write, and creat.
31:
32:
33: #include <stdio.h>
34: #define BUFSIZE (20*512)
35: char buf[BUFSIZE];
36:
37:
38:
39: void fatal(s)
40: char *s;
41: {
42: fprintf(stderr, "copy: %s\n", s);
43: exit(1);
44: }
45:
46:
47:
48: main(argc, argv)
49: int argc; char *argv[];
50: {
51: register int ifd, ofd;
52: register unsigned int n;
53:
54:
55:
56: if (argc != 3)
57: fatal("Usage: copy source destination");
58: if ((ifd = open(argv[1], 0)) == -1)
59: fatal("cannot open input file");
60: if ((ofd = creat(argv[2], 0666)) == -1)
61: fatal("cannot open output file");
62:
63:
64: COHERENT Lexicon Page 1
65:
66:
67:
68:
69: open() COHERENT System Call open()
70:
71:
72:
73:
74:
75:
76: while ((n = read(ifd, buf, BUFSIZE)) != 0) {
77: if (n == -1)
78: fatal("read error");
79: if (write(ofd, buf, n) != n)
80: fatal("write error");
81: }
82:
83:
84:
85: if (close(ifd) == -1 || close(ofd) == -1)
86: fatal("cannot close");
87: exit(0);
88: }
89:
90:
91: ***** See Also *****
92:
93: COHERENT system calls, fopen()
94:
95: ***** Diagnostics *****
96:
97: open returns -1 if the file is nonexistent, if the caller lacks
98: permission, or if a system resource is exhausted.
99:
100: ***** Notes *****
101:
102: open is a low-level call that passes data directly to COHERENT.
103: It should not be mixed with high-level calls, such as fread,
104: fwrite, or fopen.
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130: COHERENT Lexicon Page 2
131:
132:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.