I have problem I want to open a file with .out extension or if it not exist then it should create it with permissions to read and write and by opening file I want to erase his content
variable = open( "file.out", O_RDWR | O_CREAT, S_IRWXO, O_TRUNC )
I used this command but it always fails I think I have bad flags as far I know:
O_RDWR is to open file with write and read permission
O_CREAT it creates file if not exist
S_IRWXO is used by O_CREAT to make the file readable and writable
O_TRUNC erase all data from file
open doesn't take 4 arguments - only 2 or 3. All of the O_ flags should be OR'ed together in the second argument. Also, S_IRWXO will give permissions only to others, not to the owner or group owner. You meant S_IRWXU | S_IRWXG | S_IRWXO probably. But I'd rather just see an octal number; the macros aren't more readable.
variable = open( "file.out", O_RDWR | O_CREAT | O_TRUNC, 0777 )
If you want read/write/execute permission for the current user/owner use: S_IRWXU:
S_IRWXO
Read, write, and search or execute permission for users other than the
file owner. S_IRWXO is the bitwise inclusive-OR of S_IROTH, S_IWOTH,
and S_IXOTH.
S_IRWXU
Read, write, and search, or execute, for the file owner; S_IRWXG is the
bitwise inclusive-OR of S_IRUSR, S_IWUSR, and S_IXUSR.
Doing open( "file.out", O_RDWR | O_CREAT, S_IRWXU, O_TRUNC ); and ls -l gives:
-rwx------. 1 perreal perreal 0 Apr 29 15:52 file.out
Related
if I have a client descriptor as follows:
int fileDescriptor = open("myfile.txt", O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
how can I edit this statement so that, if I want to open it (double click) I will be asked for the system password?
I want to know which purpose has the following line of code in Linux syscall int open(const char *pathname, int flags):
if (flags & ~(O_RDONLY | O_WRONLY | O_CREAT | O_RDWR | O_TRUNC | O_APPEND))
{
return -1;
}
This line checks that your file have only these properties available :
O_RDONLY : read-only file
O_WRONLY : write-only file
O_CREAT : create the file if it does not exist
O_RDWR : read-write file
O_TRUNC : If the file already exists and is a regular file and the access
mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be
truncated to length 0.
O_APPEND : the file is opened in append mode
Without knowing the actual values of that O_RDONLY, O_WRONLY, and O_RDWR are assigned, it's not really possible to determine what that code actually does.
The code
if (flags & ~(O_RDONLY | O_WRONLY | O_CREAT | O_RDWR | O_TRUNC | O_APPEND))
{
return -1;
}
is non-portable and strictly speaking invalid outside of the actual system implementation that has access to and control of the actual values of the flags. Per POSIX, the flags O_RDONLY, O_WRONLY, and O_RDWR are NOT bit flags:
Values for oflag are constructed by a bitwise-inclusive OR of flags from the following list, defined in . Applications shall specify exactly one of the first five values (file access modes) below in the value of oflag:
O_EXEC
Open for execute only (non-directory files). The result is unspecified if this flag is applied to a directory.
O_RDONLY
Open for reading only.
O_RDWR
Open for reading and writing. The result is undefined if this flag is applied to a FIFO.
O_SEARCH
Open directory for search only. The result is unspecified if this flag is applied to a non-directory file.
O_WRONLY
Open for writing only.
Since those flags are not bit-based but actual value-based, their presence or absence can not be detected via bitwise operations without knowledge of their actual values on the system in use.
This code uses more than one of those flags, violating the POSIX specification:
O_RDONLY | O_WRONLY | O_CREAT | O_RDWR | O_TRUNC | O_APPEND
It can in theory produce a nonsensical bit value that can not be used in a valid bitwise comparison to anything.
The RATIONALE section of the open() POSIX documentation even addresses the non-bit flag-based nature of the open() flags:
RATIONALE
In historical implementations the value of O_RDONLY is zero. Because of that, it is not possible to detect the presence of O_RDONLY and another option. Future implementations should encode O_RDONLY and O_WRONLY as bit flags so that:
O_RDONLY | O_WRONLY == O_RDWR
That comment only makes sense if O_RDONLY, O_WRONLY and O_RDWR are not bit-based flags.
With fopen, by setting it to w will automatically clear the file for me. However now I'm trying to do the same thing with open,
int file = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
This won't guarantee that the file is empty and write at the beginning(?). How can I achieve that?
Add O_TRUNC - Initially clear all data from the file.
O_TRUNC flag truncates the file when opening. It can be used.
int file = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
The access mode bits O_TRUNC and O_APPEND are for system call open()'s 2nd parameter.
They shouldn't be used together, I guess, because they conflict, or 1 will override another, I think.
But I saw following in <The linux programming interface> page 73, is that a bad way?
d = open("w.log", O_WRONLY | O_CREAT | O_TRUNC | O_APPEND,S_IRUSR | S_IWUSR);
I created this file
char *output = "big";
creat(output, O_RDWR);
When I'm trying to read the file
cat big
I'm getting permission denied. Whats wrong with my code? How to create a file with read and write permission mode?
with ls -l, the permission of big looked like this
----------
what does this mean?
You have misinterpeted the mode argument. From the man page:
mode specifies the permissions to use in case a new file is cre‐
ated. This argument must be supplied when O_CREAT is specified
in flags; if O_CREAT is not specified, then mode is ignored.
The effective permissions are modified by the process's umask in
the usual way: The permissions of the created file are
(mode & ~umask). Note that this mode only applies to future
accesses of the newly created file; the open() call that creates
a read-only file may well return a read/write file descriptor.
and also
creat() is equivalent to open() with flags equal to
O_CREAT|O_WRONLY|O_TRUNC.
So, a more appropriate call might look like:
int fd = creat(output, 0644); /*-rw-r--r-- */
If you want to open it O_RDWR though, then just use open():
int fd = open(output, O_CREAT|O_RDWR|O_TRUNC, 0644);
This is obviously a permission issue, start trying to see if creat doesn't returns -1, if so, print the errno value, with perror(""), so that you could resolve the problem.
Imho, i'd rather use open() to do this, because as mentionned in the creat man page,
"Note that open() can open device special files, but creat() cannot create them; ..", and
"creat() is equivalent to open() with flags equals to O_CREAT | O_WRONLY | O_TRUNC", and this doesn't talks about the permissions..
it would be the exact same result if you did this:
char* output = "big";
int fd;
fd = open(output, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
// do whaterver you want to do in your file
close(fd);
For more information, "man 2 open"