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 9190315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:26:01+00:00 2026-06-17T20:26:01+00:00

In my C program, I am creating a child process and running execvp in

  • 0

In my C program, I am creating a child process and running execvp in the child. But I’m trying to change the error message to something else, for the execvp command (if there was an error).

I know that if it returned, then it was an error, then I can print my own custom error message on the next line. That’s one type of error that can occur, for example this happens if I give the command “sdfsd” to execvp. This part is working for me.

But if I type, “find sdfsd” then it does not return and prints “find: `sdfsd’: No such file or directory”.

I want to change this message (and essentially any kind of error message coming from exevcp) to my own custom one.

I believe I can use dup2 to do this, but I’m not sure how…

In the child process I tried

dup2(STDERR_FILENO, 1); 
fclose(stderr);

But this just stops the child process from writing any error messages. I still can’t print my own message in all cases..

Does anyone know how to do this?
thanks

  • 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-06-17T20:26:02+00:00Added an answer on June 17, 2026 at 8:26 pm

    Since execvp never returns if it successfully starts the new program, you won’t be able to print your own error message in the child process after the program run by execvp fails. One option would be to pipe stderr to the parent process, intercept the error message there, and then print your own error message.

    For example:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char **argv)
    {
      int ff, p[2];
      FILE *f;
      char *vv[] = {"find", "garbage", (char *)NULL};
      char msg[100];
    
      if (pipe(p) != 0)
      {
        fprintf(stderr, "Pipe failed\n");
        return 1;
      }
    
      if ((ff = fork()) == -1 )
      {
        fprintf(stderr, "Fork failed\n");
        return 1;
      }
    
      if (ff == 0)
      {
        /* In the child process */
        close(2);
        close(p[0]);
        dup2(p[1], 2);
        execvp("find", vv);
        return 1;
      };
    
      /* In the parent process */
      close(p[1]);
      f = fdopen(p[0], "r");
      if (f == NULL)
      {
        fprintf(stderr, "Fdopen failed\n");
        return 1;
      }
      if (fgets(msg, sizeof(msg), f) == NULL)
      {
        fprintf(stderr, "Fgets failed\n");
        return 1;
      }
      printf("Error message was: %s", msg);
      /* and so on */
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im getting this error when trying to run a program in creating, how shold
I have a long-running python program that I'm trying to run on a remote
Program calculates sum of numbers from 1 to N.. Child process calculates sum of
My current program is creating child processes and giving them work (CPU intensive work).
I have a program wherein i use fork. In the child process, i just
I'm trying to learn Objective-C and my program (creating a calculator) gets linker or
I'm creating an automated web app build process with node.js on windows. I'm trying
My Windows XP/7 program launches a child process using the Windows API CreateProcess() function
I creating a program which send newsletter with a background image. It works fine
Bassicly im creating a program that reads information from an xml file into a

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.