[comp.unix.questions] Question about discrepancy of open

andrew@nsc.nsc.com (Andrew Lue) (05/18/88)

I'm experimenting with file I/O on a BSD system.
According to K&R, the format of the open() function is:

fd = open(name,rwmode);

where rwmode is 0, 1, or 2.

In the BSD documents, the format is

open(path, flags, mode);


With my experiments, I can get the first format to work.  When is
the second format applicable?  When I invoke the C compiler, I don't
pass any flags.
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Andrew H. Lue                                          {decwrl|sun}!nsc!andrew

ok@quintus.UUCP (Richard A. O'Keefe) (05/18/88)

In article <5068@nsc.nsc.com>, andrew@nsc.nsc.com (Andrew Lue) writes:
> According to K&R, the format of the open() function is:
> fd = open(name,rwmode);
> where rwmode is 0, 1, or 2.

> In the BSD documents, the format is
> open(path, flags, mode);

This is *NOT* a discrepancy between BSD and System V.  (K&R 1st edition
precedes System V by years and years.)  If you look at the SVID, you will
find that the open(BA_OS) call is
	int open(char *path, int oflag, int mode)
0 (== O_RDONLY), 1 (== O_WRONLY), and 2 (== O_RDWR) will work fine.
The "mode" argument is a protection mask, just like you'd give to chmod().
It is only heeded if you specify O_CREAT in the flags argument, and the
file didn't previously exist.  So, to open a file for output, creating
it with protection rwxr----- if it didn't previously exist, do
	fd = open(path, O_WRONLY | O_CREAT, 0640);

More briefly, RTFM.

gwyn@brl-smoke.ARPA (Doug Gwyn ) (05/19/88)

In article <5068@nsc.nsc.com>, andrew@nsc.nsc.com (Andrew Lue) writes:
> According to K&R, the format of the open() function is:
> fd = open(name,rwmode);

It used to be like that.

> In the BSD documents, the format is
> open(path, flags, mode);

This was changed when USG assimilated creat() into open();
the extra argument is needed to specify the initial protection mode.

By the way, you're using "format" in a very loose way; see comments
below about imprecision in interface specification.

> With my experiments, I can get the first format to work.  When is
> the second format applicable?  When I invoke the C compiler, I don't
> pass any flags.

You mean you don't pass any `mode' argument.
This happens to work, by accident, on most existing systems
(which is why the USG change was able to get adopted).
To play safe, I always supply all three arguments.

The correct portable declaration according to draft IEEE Std 1003.1 is
	int open(char *path, int oflag, ...);
i.e. a variable-argument function linkage.  (The P1003 Working Group
had to choose some way to fix the technical error, and this is the
one they decided on.)  Actually in Draft 12 they botched it by trying
to mix old- and new-style function declaration syntax, or more
precisely by not having a clear idea about what the various C-like
"declarations" are really meant to signify.  (To be fair, this seems
to be a long-standing UNIX documentation tradition.)  The above is
my straightened-out interpretation of what was intended.  You should
never have to declare the open() function yourself, anyway; just
	#include <fcntl.h>
(I think the revised Draft 12.whatever also shows <sys/types.h> and
<sys/stat.h>; ignore that.)