Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ open(DOS) — OpenDesktop Software Development System 1.0.0d

Media Vault

Software Library

Restoration Projects

Artifacts Sought

     Name
          open - Opens a file for reading or writing.

     Syntax
          #include <fcntl.h>
          #include <sys/types.h>
          #include <sys/stat.h>
          #include <io.h>

          int open(path, oflag [, pmode])
          char *path;
          int oflag;
          int pmode;

     Description
          The open function opens the file specified by path and
          prepares the file for subsequent reading or writing, as
          defined by oflag. The argument oflag is an integer
          expression formed by combining one or more of the following
          manifest constants, defined in fcntl.h.  When more than one
          manifest constant is given, the constants are joined with
          the bitwise-OR operator ( | ).

            Constant    Meaning

            O_APPEND    Repositions the file pointer to the
                        end of the file before every write
                        operation.

            O_BINARY    Opens file in binary (untranslated)
                        mode. (See fopen for a description
                        of binary mode.)

            O_CREAT     Creates and opens a new file for
                        writing; this has no effect if the
                        file specified by path exists.

            O_EXCL      Returns an error value if the file
                        specified by path exists. Only
                        applies when used with O_CREAT.

            O_RDONLY    Opens file for reading only; if this
                        flag is given, neither O_RDWR nor
                        O_WRONLY can be given.

            O_RDWR      Opens file for both reading and
                        writing; if this flag is given,
                        neither O_RDONLY nor O_WRONLY can be
                        given.

            O_TEXT      Opens file in text (translated)
                        mode. (See fopen for a description
                        of text mode.)

            O_TRUNC     Opens and truncates an existing file
                        to zero length; the file must have
                        write permission. The contents of
                        the file are destroyed.

            O_WRONLY    Opens file for writing only; if this
                        flag is given, neither O_RDONLY nor
                        O_RDWR can be given.

     Notes
          Use the O_TRUNC flag with care, as it destroys the complete
          contents of an existing file.

          Either O_RDONLY, O_RDWR, or O_WRONLY must be given to
          specify the access mode.  There is no default value for the
          access mode.

          The pmode argument is required only when O_CREAT is
          specified. If the file exists, pmode is ignored. Otherwise,
          pmode specifies the file's permission settings, which are
          set when the new file is closed for the first time. The
          pmode is an integer expression containing one or both of the
          manifest constants S_IWRITE and S_IREAD, defined in
          sys/stat.h. When both constants are given, they are joined
          with the bitwise-OR operator ( | ). The meaning of the pmode
          argument is as follows:

            Value                 Meaning

            S_IWRITE              Writing permitted

            S_IREAD               Reading permitted

            S_IREAD | S_IWRITE    Reading and writing permitted

          If write permission is not given, the file is read only.
          Under DOS and OS/2, all files are readable; it is not
          possible to give write-only permission.  Thus the modes
          S_IWRITE and S_IREAD | S_IWRITE are equivalent.

          The open function applies the current file-permission mask
          to pmode before setting the permissions (see umask).

          Under DOS Versions 3.0 and later with SHARE installed, a
          problem occurs when opening a new file with oflag set to
          O_CREAT | O_RDONLY or O_CREAT | O _WRONLY with pmode set to
          S_IREAD. In this case, the operating system will prematurely
          close the file during system calls made within open. This
          problem does not occur in OS/2.

          To get around the problem, open the file with the pmode
          argument set to S_IWRITE. After closing the file, call chmod
          and change the mode back to S_IREAD. Another work-around is
          to open the file with pmode set to S_IREAD  and omode set to
          O_CREAT | O_RDWR.

     Return Value
          The open function returns a file handle for the opened file.
          A return value of -1 indicates an error, and errno is set to
          one of the following values:

            Value     Meaning

            EACCES    Given path name is a directory; or an
                      attempt was made to open a read-only
                      file for writing; or a sharing
                      violation occurred (the file's sharing
                      mode does not allow the specified
                      operations).

            EEXIST    The O_CREAT and O_EXCL flags are
                      specified, but the named file already
                      exists.

            EINVAL    An invalid oflag or shflag argument
                      was given.

            EMFILE    No more file handles available (too
                      many open files).

            ENOENT    File or path name not found.

     See Also
          access(DOS), chmod(DOS), close(S), creat(DOS), dup(S),
          dup2(S), fopen(DOS), sopen(DOS), umask(DOS)

     Example
          #include <fcntl.h> #include <sys/types.h> #include
          <sys/stat.h> #include <io.h> #include <stdlib.h> #include
          <stdio.h>

          main()
           {
                  int fh1, fh2;

                  fh1 = open("data1",O_RDONLY);
                  if (fh1 == -1)
                          perror("open failed on input file");
                  else
                          printf("open succeeded on input file\n");

                  fh2 = open("data2",O_WRONLY|O_CREAT,
                                     S_IREAD|S_IWRITE);
                  if (fh2 == -1)
                          perror("open failed on output file");
                  else
                          printf("open succeeded on output file\n");
           }

          This program uses open to open a file named data1 for input
          and a file named data2 for output.

                                                (printed 6/18/89)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026