tags:

views:

48

answers:

2

I'm working on a networking assignment and we are tasked with creating a remote file access server with a protocol we were given. My difficulties come in the lack of information I can find that explains the process of calculating the bits for the oflag argument in open().

I receive a message from a client to open a file and in the message I parse characters for the flags to use in oflag. Specifically they are:

  • R - O_RDONLY
  • W - O_WRONLY
  • RW - O_RDWR
  • A - O_APPEND
  • C - O_CREAT
  • T - O_TRUNC
  • E - O_EXCL

I went around Google and searched bitwise operations, enumeration flags, bit flags, calculating bit flags, etc. and couldn't find something that was useful in figuring out how to create the bits for oflag. Maybe I just didn't know what I was looking for and overlooked useful information?

Could someone please:

  • Point me in the direction/provide links to documentation/example of how to calculate the bits/# I ought to put into oflags given my parsed characters or
  • Show me the enumeration types for the flags and the ordering they ought to go in

Thanks a bunch for you help and if I wasn't clear on my problem or what I am trying to do, just let me know and I will clarify ASAP.

A: 

The usual expression would be something like O_RDWR | O_CREAT. And note that O_RDWR is exactly O_RDONLY | O_WRONLY

You can do something like this:

char *flags = "r";
int oflag = 0;

if (strchr(flags,'r')) oflag |= O_RDONLY;

and so on for the rest of the flags.

Andrew McGregor
Ummmm... Maybe I was a lil confusing. Lets say I wanted to open a file with O_WRONLY | O_CREAT | O_APPEND, would there and how do I calculate some int/bits to put in? For example could I enter open("myfile", <some #>) that would correspond to those flgas?
Chris
@Chris: Use the flags, not the number. You'd use `open("myfile", O_WRONLY | O_CREAT | O_APPEND)` in that case. It's easier to read, if nothing else.
David Thornley
Use the flags... these flag bits are not all standardised, so you can't assume they will have the same value on every platform. Also, the symbolic form is much more readable and therefore maintainable... the source code is for your benefit, not the machine's.
Andrew McGregor
+2  A: 

The O_... flags are numbers each with a different single bit set. For example on my system they are defined in fcntl.h as

#define O_RDONLY             00
#define O_WRONLY             01
#define O_RDWR               02
#define O_CREAT            0100 /* not fcntl */
#define O_EXCL             0200 /* not fcntl */
#define O_NOCTTY           0400 /* not fcntl */
#define O_TRUNC           01000 /* not fcntl */
#define O_APPEND          02000

You use | (logical OR) to combine the flags and pass in a single number to open with all the bits set for each option you want. So e.g. open("file", O_RDWR | O_CREAT).

You can compute an int and pass that into open too if you want.

int flags = 0;
if (...)
    flags |= O_RDWR;
...
open('file', flags);
ccmonkey
So for example if I parsed RWC then would be open("file", O_RDWR | O_CREAT). But since only know the chars would I do something like int oflag = (02 | 0100) and then my open would look like open("file", oflag)? I need to go from my characters that I parse to binary.
Chris