0

I'm learning x86 assembly Linux from the book "Programming from the Ground up", and currently I'm learning how to open a file and read from or write to it.
I'm having trouble with the options for opening files. I know 0 is for read-only, 03101 is for write and truncate, but where can I get the full documentation to all the open options?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Kain
  • 329
  • 1
  • 10
  • In the C include files. On my system they are in _/usr/include/x86_64-linux-gnu/bits/fcntl-linux.h_ – Jester Mar 21 '22 at 14:13
  • I looked at it and I didn't see anything that's related to the file opening options, am I missing something here ? – Kain Mar 21 '22 at 14:24
  • May be different on your system. Mine has e.g. `#define O_WRONLY 01` and `# define O_TRUNC 01000` – Jester Mar 21 '22 at 14:45
  • May be it's because i'm using WSL interesting,... – Kain Mar 21 '22 at 15:07
  • I've found it int /usr/include/asm-generic/fcntl.h thanks for the help I'll close this question now – Kain Mar 21 '22 at 15:17

2 Answers2

3

It's not about WSL, it's about what Linux distro you chose to install in your WSL. ​Different distros will put things in different places in the filesystem.
locate '*fcntl*.h' is a good way to find the appropriate headers.

You can always compile a C program that includes the documented headers (which will pull in the "real" headers), and look at its gcc -E -dM macro defines. Or even

gcc -E -dM /usr/include/fcntl.h | grep ' O_'

to filter just the O_ macro constants. (That fcntl.h is I think likely to be in the plain /usr/include, not buried somewhere, but maybe that's just my Arch GNU/Linux distro keeping it simple. It keeps Linux-specific libc headers like <asm/unistd.h> in /usr/include/asm/, where you can find unistd_32.h and unistd_64.h for 32 and 64-bit call numbers, respectively.) Or let the usual include-path search happen:

echo '#include <fcntl.h>' | gcc -E -dM - | grep ' O_'

Or write code that does printf("%x, %x\n", O_CREAT, O_TRUNC) or whatever to print out some constants you're interested in, whatever header they came from. (Or print out their bitwise OR, like O_CREAT|O_TRUNC).

The permission mode bit constants like S_IRUSR are defined in terms of other constants like __S_IREAD so it's a bit of a rats nest to follow; probably just printing it out is a good idea. Or simply write permission bits in octal, like mov edx, 0o666 (NASM) or mov $0666, %edx (GAS). (Letting umask clear the write-for-other bit on file creation).


The names of the constants to look for can be found in the man page, open(2).

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
0

Using: Windows Subsystem I have found the correct file for x86 asm linux in /usr/include/asm-generic/fcntl.h

Kain
  • 329
  • 1
  • 10