Opening file modes in x86 assembly Linux

Cloth

I'm learning x86 assembly Linux from the book Programming from the Ground up, currently I'm learning how to open a file and read 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, where can I get the full documentation to all the open options ?

Peter Cordes

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 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).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324223915&siteId=291194637