Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 618115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:30:35+00:00 2026-05-13T18:30:35+00:00

My program basically runs a executable file with command line arguments. A child process

  • 0

My program basically runs a executable file with command line arguments.
A child process is forked and the output of the child process is taken in the file “filename”.

The problem is that the file is made and the data is written but it can only be opened by the root user.. How can make it readable to the user who invoked the program?

The code is:

    #include<stdio.h>
    #include<string.h>      //strcpy() used
    #include<malloc.h>      //malloc() used
    #include<unistd.h>      //fork() used
    #include<stdlib.h>      //exit() function used
    #include<sys/wait.h>    //waitpid() used
    #include<fcntl.h>

    int main(int argc, char **argv)
    {
char *command;
char input[256];
char **args=NULL;
char *arg;
int count=0;
char *binary;
pid_t pid;
int fdw;

printf("Enter the name of the executable(with full path)");
fgets(input,256,stdin);

command = malloc(strlen(input));
strncpy(command,input,strlen(input)-1);

binary=strtok(command," ");
args=malloc(sizeof(char*));

args[0]=malloc(strlen(binary)+1);
strcpy(args[0],binary);

while ((arg=strtok(NULL," "))!=NULL)
{
    if ( count%10 == 0) args=realloc(args,sizeof(char*)*10);
    count++;
    args[count]=malloc(strlen(arg));
    strcpy(args[count],arg);
}
args[++count]=NULL;

if ((fdw=open("filename",O_WRONLY|O_EXCL|O_CREAT|0700)) == -1)
    perror("Error making file");
close(1);
dup(fdw);

if ((pid = fork()) == -1)
{
    perror("Error forking...\n");
    exit(1);
}
if (pid == 0)   execvp(args[0],&args[0]);
else
{
    int status;
    waitpid(-1, &status, 0);
}
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T18:30:35+00:00Added an answer on May 13, 2026 at 6:30 pm

    Re-read the manpage for open, you are not passing the file mode argument correctly and causing the flags to be messed up in the process.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically the child process runs indefinitely until killed in the background, and I want
I basically have a program that filters records from one excel file to another
So what I need is basically to create a java program that runs from
Basically my situation is as follows. Upload file Run external process on file (which
I'm writing a Python program for Linux and the program basically uses the terminal
I am using a facade design pattern for a C# program. The program basically
I've got a program made basically from a ListCtrl and has an add button,
Right what im trying to accomplish is a program that basically sets the active
Basically the program takes customer information and dumps it in a database. In order
I'm using the .NET Socket class. Basically my program is to send XML commands

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.