Missing output on file while using execvp in C

80 Views Asked by At

I am working on a Shell Command Execution Simulation in C.

I'm trying to redirect the output of execvp function into a file. The output looks okay when printed to terminal, but when tried to redirect it to a txt file, there is no output visible on the output.txt file that is created.

Here is how the code looks:

int file = open("output.txt", O_WRONLY | O_CREAT);
if (file == -1) {
    perror("open");
    exit(EXIT_FAILURE);
}
if (dup2(file, STDOUT_FILENO) == -1) {
    perror("dup2");
    exit(EXIT_FAILURE);
}
char* command1= "grep";
char* myList[]={"grep", "URL", NULL};
close(file);
execvp(command1, myList);
perror("execvp");
exit(EXIT_FAILURE);

FOUND SOLUTION:

Apparently the problem caused because I forgot to close read and write side of the pipe.

I should've added:

close(fd[0]);
close(fd[1]);

after using dup2 function.

Thanks for everyone who tried to help me even though my problem was could not be solved by the info I gave.

1

There are 1 best solutions below

2
Andrew Henle On
int file = open("output.txt", O_WRONLY | O_CREAT);

is not correct.

You are not properly setting the file's permission bits, so later use of the file likely causes problems.

When creating a file, open() requires a mode argument:

SYNOPSIS

#include <sys/stat.h>
#include <fcntl.h>

int open(const char *path, int oflag, ...);

...

O_CREAT

If the file exists, this flag has no effect except as noted under O_EXCL below. Otherwise, if O_DIRECTORY is not set the file shall be created as a regular file; the user ID of the file shall be set to the effective user ID of the process; the group ID of the file shall be set to the group ID of the file's parent directory or to the effective group ID of the process; and the access permission bits (see <sys/stat.h>) of the file mode shall be set to the value of the argument following the oflag argument taken as type mode_t modified as follows: a bitwise AND is performed on the file-mode bits and the corresponding bits in the complement of the process' file mode creation mask. Thus, all bits in the file mode whose corresponding bit in the file mode creation mask is set are cleared. When bits other than the file permission bits are set, the effect is unspecified. The argument following the oflag argument does not affect whether the file is open for reading, writing, or for both. Implementations shall provide a way to initialize the file's group ID to the group ID of the parent directory. Implementations may, but need not, provide an implementation-defined way to initialize the file's group ID to the effective group ID of the calling process.

Instead of

int file = open("output.txt", O_WRONLY | O_CREAT);

you should probably have something like:

int file = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644 );

The current output.txt file likely has some strange permissions that could be causing problems. Delete the file, and update your code to ensure the file is created with controlled permissions.