

open()                 COHERENT System Call                open()




Open a file

iinntt ooppeenn(_f_i_l_e, _t_y_p_e) cchhaarr *_f_i_l_e; iinntt _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:


             00       Read only
             11       Write
             22       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


