open() error File exists even if O_EXCL is removed - c

I have the simple part of code that tries to open the file or create a new one:
desc = open(path, 0600 | O_RDWR | O_CREAT);
if (desc == -1){
fprintf(stderr, "process %d: open: ", i);
perror("");
}
But even without O_EXCL I get an error "File exists"
process: open: File exists
So what's the matter of this issue could be?

Related

create new file with system calls

Im trying to create a new file / overwrite an existing file using systemcalls , but for some reason I have two problems:
1. When I'm first running the program it exits with value 0, so it seems like it created the file successfully, but I can't see anything in my project directory.
then when I secondly running the program the file is created, but an error message is printed on the screen.
2. Also after the first iteration of the program, I can't see the prinf message at the end of the main function.
Thanks for helping.
int readFileDesc = 0, writeFiledesc = 0;
int sourceFile = 1, destFile = 2, bufferSize = 3, isOverwrite;
if (argc != 4 && argc != 5) {
printf("Invalid number of arguments\n");
printf("Usage:\n");
printf(" ex1 [-f] SOURCE DEST BUFFER_SIZE");
exit(EXIT_FAILURE);
}
//Checking if -f [OP] is activated.
isOverwrite = (strcmp(argv[1], "-f") == 0);
if (isOverwrite) {
sourceFile++;
destFile++;
bufferSize++;
}
//Opening the source file
readFileDesc = open(argv[sourceFile], O_RDONLY);
if (readFileDesc < 0) {
perror("Unable to open source file for reading: ");
exit(EXIT_FAILURE);
}
//opening the destination file
if (!isOverwrite) {
//Case we dont have the -f [op] so we create the file.
writeFiledesc = open(argv[destFile],
O_CREAT | O_EXCL | O_WRONLY ,
S_IRUSR | S_IWUSR);
if (writeFiledesc < 0) {
perror("Unable to open destination file for reading: ");
exit(EXIT_FAILURE);
}
} else {
//Case we have the -f [op] so we override existing file.
writeFiledesc = open(argv[destFile], O_RDONLY | O_WRONLY | O_TRUNC);
if (writeFiledesc < 0) {
perror("Unable to open destination file for writing: ");
exit(EXIT_FAILURE);
}
}
//Assume the buffersize is legal.
bufferSize = atoi(argv[bufferSize]);
char data[bufferSize];
int nread, nwrite;
while ((nread = read(readFileDesc, data, bufferSize)) > 0) {
if ((nwrite = write(writeFiledesc, data, nread)) != nread) {
printf("write problem: ");
}
}
// cant see this!
printf("File %s was copied to %s" , argv[sourceFile] , argv[destFile]);
//handling errors
close(sourceFile);
close(destFile);
return EXIT_SUCCESS;
}
This is wrong:
writeFiledesc = open(argv[destFile], O_RDONLY | O_WRONLY | O_TRUNC);
Using both O_RDONLY and O_WRONLY is wrong. You need to use O_RDWR.
Per the POSIX standard for open():
SYNOPSIS
#include <sys/stat.h> #include <fcntl.h>
int open(const char *path, int oflag, ...);
...
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.
Any combination of the following may be used:
...
Also, read() and write() return ssize_t, not int.

Redirection out using open

Im making a shell and working on output redirection, specifically appending to files. Problem is, the following code always overwrites the contents of the file and does not simply append.
int val = -1;
//Flag issue?
val = open(destination, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
if (val != -1) {
close(1);
dup(val);
close(val);
}

Open() for output not creating file

I have this function that utilizes open to set i/o redirection:
void setOutput(char * buffer){
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
if(file < 0){ printf("error opening %s for output\n", buffer); }
if(dup2(file, 1) < 0){ printf("error with dup2 opening %s for output\n", buffer); }
}
When I run it, it works fine for files that are already defined but returns -1 when it receives a non-created file. Not sure why
You need to change the following
int file = open(buffer, O_WRONLY || O_CREAT, S_IWUSR);
To
int file = open(buffer, O_WRONLY | O_CREAT, S_IWUSR);
Format :
int open( char *filename, int access, int permission );
access : Should be provided as a bit wise OR operator, that means using | not || which is logical OR

Redirection doesn't work in shell:: ls: cannot access >: No such file or directory

I don't know why redirection doesn't work in the shell I have written. Here's my code"
int i;
for (i=1; !args[i];i++)
{
if (args[i]== ">")
{
printf("argv[i] %s %d \n", args[i], i);
int out;
// out = open("out", O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
out=open("out", O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
int fdl=dup2(out,1);
close(out);
execvp(args[0],args);
}
}
Also here's the error I receive :
mysh> ls
basic_shell basic_shell.c~ fork fork_2 fork_cp.c
basic_shell.c basic_shell_OK.c fork_1 fork.c
mysh> ls > file
ls: cannot access >: No such file or directory
ls: cannot access file: No such file or directory
Please let me know what's wrong?
If args is an array of char*, then this condition
if (args[i]== ">")
does not do what you think it does. It compares the pointers and not what they point to. To compare string you have to use strcmp.

c open() Undefined error: 0 on os x

On a mac running 10.8 i am trying to open a serial port.
ls /dev/cu* returns:
/dev/cu.Bluetooth-Modem /dev/cu.Bluetooth-PDA-Sync /dev/cu.usbserial-A1009TT7
i can see the port is there but when i try to open it i get Undefined error: 0(0). This is my code i use to open the port.
char *path = "/dev/cu.usbserial-A1009TT7";
open(path , O_RDWR | O_NOCTTY | O_NONBLOCK); // open the port
if (file == -1) {
printf("Error opening port : %s(%d).\n", strerror(errno), errno);
close(file);
return -1;
}
anyone have any idea why the port wont open?
thanks in advance.
Whoops! You meant to type this:
file = open(path , O_RDWR | O_NOCTTY | O_NONBLOCK);
^^^^^^^
Also, there is no need to close a file descriptor that isn't open.
if (file == -1) {
printf(...);
// close(file); Completely unnecessary. It's not valid!
return -1;
}

Resources