@FaheemMitha What do you mean? That is a failure mode. You can make a trivial little script like this:
#!/bin/sh
echo "Exited with $1"
exit "$1"
And then:
$ for i in {0..5}; do foo.sh $i && echo "Success" || echo "Failure"; done
Exited with 0
Success
Exited with 1
Failure
Exited with 2
Failure
Exited with 3
Failure
Exited with 4
Failure
Exited with 5
Failure
@StephenKitt @StephenKitt Thank you! Indeed the notification popped up ~ half an hour ago. I'm somewhat flattered that someone took notice of the progress; I hope that I didn't sign off on too many faulty edits in recent times ... ;)
@AdminBee none that I noticed! I was just looking at the stats, and saw that you had reached 2,002 with 14 reviews today which meant you’d crossed the thousand mark today.
@StephenKitt Thank you. And btw. also thank you for notifying me about these review errors; that was a good reminder not to get careless and actually try to understand the implications of the suggested edit.
@AdminBee you’re welcome, and thank you for caring!
I find myself skipping reviews quite a bit these days, when I can’t be bothered actually figuring out whether the edit is correct or not (when it’s technical).
In a nutshell, the second (modified) C code exits with 0, which is not what I would expect here.
Given that the child exits with error. From what I read, execl overwrites the child process, so its exit code is the child exit code. But it's possible I got this wrong.
BTW, would such a question be more on topic on U&L or SO?
This is probably not worth posting, since it's most likely the outcome of a stupid misunderstanding.
@FaheemMitha If you strace -f it, you'll see that the child is returning 0. Actually, it is returning what the return statement after the if block is given as its argument.
(Not a C programmer here, so not going to give any advice ;-)
@ToxicFrog Thanks for the explanation. Right now I'm trying to figure out how to get return codes. Apparently using waitpid and some macros with the string WAIT.
I've found a version that works, and one that doesn't. Currently I'm not sure why one does and the other doesn't.
TeX is also having an election, after a long time.
Seems like they're making it unreasonably difficult to get the return value. Though I suppose exec cannot return, since it wipes out its calling process (if I understand correctly).
@FaheemMitha I wouldn't venture into the perils of giving a formal definition :-)
But, in the context of a casual chat, I would mean something more than having compiled the "Hello World!" program -- which is roughly the level I am at.
@fra-san Ah, yes. Hello World. Due to Kernighan, I think.
I tend to assume Unix people have a working knowledge of C. Seeing that it was created as roughly the same time as Unix, by the same people, that Unix is traditionally written in it, and it has been closely associated with Unix for 50 years.
Then again, if people were to scream and run at the sight of C, I wouldn't really blame them either.
@FaheemMitha in general, C stdlib functions will return some sentinel value on error and set errno, and it's then up to you to check for that and, if an error occurred, read errno to find out what it is
In the case of exec(), the fact that it returned at all is what indicates an error, and you should check errno to find out exactly what
man 3 errno and man 3 perror for some starting points on how to do that
Like I said, C doesn't have any sort of exception-throw mechanism (unless you build your own on top of setjmp/longjmp, which some people do); your options for error handling in C are basically some combination of "return a value indicating that an error occurred and require the caller to check it", "set a global indicating that an error occurred and require the caller to check it", or "raise a signal and terminate the program"
For a related but somewhat more typical example of error handling, see man 2 fork; it returns 0 in the child and the PID of the child in the parent, but on error, it returns -1 and sets errno
or malloc(), which returns a pointer to the allocated memory on success, or null (and sets errno) on failure.