Error handling and the exec

Some programmer's show off: They don't use an if condition to check whether the exec API failed; they just write the line of code after an exec to be the failure case!

As an example, take the previous program, but change the code to this, the wrong way to do it:

execl("/bin/uname", "uname", argv[1], (char *)0);FATAL("execl failed\n");

It works, yes: The only reason control will ever reach the 'FATAL()' line is if the exec operation failed. This sounds cool, but please, do not code like that. Be professional, follow the rules and good coding style guidelines; you'll be a better programmer and glad for it! (An innocent freshly minted programmer might not even realize that what follows the execl above is actually ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.