• C

An EXEC call (UNIX)

Is there any way to know that exec failed.

Suppose i execute
How do i know that this is invalid command, since it
does not return -1.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danil. I found only execl, execv, execle, execve, execlp and execvp functions.
Are you sure that you posted right name ? BTW, I suggest to use
spawn family instead of exec. Because in case of exec:
"A successful call to exec() does not return because the new
program overwrites the calling program."

This is result, taken from "man exec" command made on HP-UX.
This is not a very meaningful answer.

That is, you are not supposed to get rid of exec*() calls, rather you should understand how they work.

If the exec*() call succedes, the new process image replaces the current process. I.e. successful calls to the exec functions do not return because the system overlays the calling process with the new process.

If the exec*() functions returns to the calling process image, an error has occurred; the return value is -1 (at least on my OS), and the function sets errno to indicate the error.

Have a look, on your OS, at exec(2) (man exec, or man 2 exec) for complete reference.

danil, if this is the correct answer, please reopen this question to me, thanks.
julio, answer really answers ;) Take a look on original question:
"How do i know that this is invalid command, since it does not
return -1"
If it doesn't returns -1, that is standard for every OS I know
(maybe except VAX, where I have had troubles to find C compiler ;),
this means that danil actually ran some program "hi". We have
only one way to know that for sure: from danil's feedback.
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Yes, right... DANIL!

Joke apart, if you didn't think "execvl"  could be a typo (but how could the program compile at all then?), that's what i would have asked first, *before* answering.

My comment was mostly generated by your "I suggest to use spawn family instead of exec. Because...", which does not seem neither correct nor anyway an answer to me.

Yes, answer really answers ;-)
Just an additional comment about real answers.

Sometimes users ask something which is simply meanigless. In those cases, IMO, you should tell them their very approach is wrong and give the correct one, instead of giving a *working* solution which keeps the wrong design.

But this is really meant as just my opinion.
danil082697Author Commented:
Actually I am wrigting a shell, and i am executing an
call, which really does not matter.
Anyway my status is different depending on command. For Ex:
"ls" -> status = 0;
"ls>out"status = 1350;/*some meanigless #*/
"hello" status = 256; /*hello does not exist, actually it                               always returns 256 if command is                               invalid*/
How you defined status ? Integer, signed/unsigned ? You print "status" with printf, right ?
What % you use ? status should be signed int and printf('Status= %i",status);
"ls>out" is unlikely to be a valid filename in your PATH
Did you check the value of errno after the execvp fails?
as julio said, exec* spawns a new process replacing the existing
(with the exec() call) and never returning, if it returns exec
failed anyhow.
Error conditions are placed in the common error variable.
So just put a printf behind and you'll see it, like:
 printf("exec failed with error %d\n", error);

Don't forget to have anywhere
      extern int error;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Is this C-shell??
julio, I meant C, you not?
Actually, danil does not in his Tuesday, April 21 1998 - 08:27AM PDT comment.
BTW, i've never seen a common "error" variable in C.
I thought it was called "errno"
But if danil is expecting something to return, maybe system or fork is what's wanted?

it's errno
Why doing a fork? exec* failed if it returns.
With fork() you are actually spawning a child, and, in the parent, you not only know if the fork at all succeded, also will be able to get the child's exit value.

IMO, what ozo also points out is that danil should give some feedback.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.