|
|
coherent
open() COHERENT System Call open()
Open a file
iinntt ooppeenn(_f_i_l_e, _t_y_p_e) cchhaarr *_f_i_l_e; iinntt _t_y_p_e;
open opens a file to receive data, or to have its data read.
When it opens file, open returns a file descriptor, which is a
small, positive integer that identifies the open file for subse-
quent calls to read, write, close, dup, or dup2. type determines
how the file is opened, as follows:
00 Read only
11 Write
22 Read and write
After file is opened, reading or writing begins at byte 0.
***** Example *****
This example copies the file named in argv[1] to the one named in
argv[2] by using COHERENT system calls. It demonstrates the sys-
tem calls open, close, read, write, and creat.
#include <stdio.h>
#define BUFSIZE (20*512)
char buf[BUFSIZE];
void fatal(s)
char *s;
{
fprintf(stderr, "copy: %s\n", s);
exit(1);
}
main(argc, argv)
int argc; char *argv[];
{
register int ifd, ofd;
register unsigned int n;
if (argc != 3)
fatal("Usage: copy source destination");
if ((ifd = open(argv[1], 0)) == -1)
fatal("cannot open input file");
if ((ofd = creat(argv[2], 0666)) == -1)
fatal("cannot open output file");
COHERENT Lexicon Page 1
open() COHERENT System Call open()
while ((n = read(ifd, buf, BUFSIZE)) != 0) {
if (n == -1)
fatal("read error");
if (write(ofd, buf, n) != n)
fatal("write error");
}
if (close(ifd) == -1 || close(ofd) == -1)
fatal("cannot close");
exit(0);
}
***** See Also *****
COHERENT system calls, fopen()
***** Diagnostics *****
open returns -1 if the file is nonexistent, if the caller lacks
permission, or if a system resource is exhausted.
***** Notes *****
open is a low-level call that passes data directly to COHERENT.
It should not be mixed with high-level calls, such as fread,
fwrite, or fopen.
COHERENT Lexicon Page 2
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.