Redirection out using open - c

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);
}

Related

permission denied with open()

The issue arises with fopen(), stating that permission is denied. I have tried to input all the modes however this was unsuccessful. Same when adding all the writing modes. I am attempting to open a file to with which i can write. Thanks to anyone who helps
if(filename != NULL && redirectArrow == true){
mode_t modes = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
int fd = open(filename, O_WRONLY | O_CREAT , S_IWUSR | S_IWGRP | S_IWOTH); //opening file in write only, and creating file if it does not exist
if(fd == -1){
perror("Failed to create and open the file\n");
exit(3);
}

How to replace a char from char array in C Program

I am developing a shell code in C. Currently, my task is to redirect stdin to stdout with different commands, for instance cat < hello.c -ne > test.txt
I am able to do it, but my code tries to open "<" and ">" file as well, so for that I tried to assign 0 value which doesn't give me any error but ignores -ne.
Here is my code,
int in, out;
int i;
for (i = 0; i < argc; i++) {
if(strcmp( argv[i],"<")==0) {
in = open(argv[i+1], O_RDONLY);
if (!in)
exit(EXIT_FAILURE);
// argv[i]=0;
// array[i+1]=0;
}
else if(strcmp( argv[i],">")==0) {
out = open(argv[i+1], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
if (!out)
exit(EXIT_FAILURE);
// argv[i]=0;
// array[i+1]=0;
}
}
dup2(in, 0);
dup2(out, 1);
// close unused file descriptors
close(in);
close(out);
char *bname;
char *path2 = strdup(*argv);
bname = basename(path2);
//printf("%d",argc);
/*the command will be executed using execvp*/
execvp(bname, argv);
argv array would be like
argv[0]="cat"
argv[1]="<"
argv[2]="hello.c"
argv[3]="-ne"
argv[4]=">"
argv[5]="test.txt"
Basically, I want to open multiple files in between "<" and ">" and then redirect to the given file after ">".
The current Output-
$ ./shell
Enter Shell Command -- cat < hello.c -ne > te1.txt
$ cat: : No such file or directory
cat: : No such file or directory
cat: te1.txt: input file is output file

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

Named pipes and background process

I'm trying to make a background process named server that uses a named pipe to receive data from a "client" program, and the process saves that data in a file called log. My code is as follows:
server:
main(){
int fd2,fd_log;
char *fifo ="/home/me/fifo";
char c;
mkfifo(fifo, 0666);
int x;
while(fd2 = open(fifo, O_RDONLY)>=0){
fd_log=open("/home/me/log.txt",O_WRONLY | O_CREAT | O_APPEND);
while(read(fd2,&c,1)>=0){
write(fd_log,&c,1);
}
close(fd2);
close(fd_log);
}
}
client:
main(){
int fd1;
char *fifo ="/home/me/fifo";
char c;
fd1 = open(fifo, O_WRONLY);
while(read(0, &c, 1)>=0){
write(fd1,&c,1);
}
close(fd1);
}
However, this doesn't seem to work. There is no action in the log file, and I think the read call in the server file it's not done. Any suggestions?
The problem is in your line here:
while(fd2 = open(fifo, O_RDONLY)>=0){
Due to C operator precedence, this is really evaluated as:
while((fd2 = (open(fifo, O_RDONLY)>=0)) != 0){
I.e. call open(), check if it's return value is greater than 0, then assign that boolean result to fd2, then check if it is zero or not.
That way you are then reading from file no. 1, which is stdout and that surely blocks indefinitely.
Change it to this and everything starts working of course:
while((fd2 = open(fifo, O_RDONLY)) >=0){
Also you are opening the log file without any permissions, you should specify some so that you can access it afterwards somehow, e.g:
fd_log=open("/home/me/log.txt",O_WRONLY | O_CREAT | O_APPEND, 0600);

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.

Resources