Computers, Science, Technology, Xen Virtualization, Hosting, Photography, The Internet, Geekdom And More

Add an –inpath option to your programs.

Posted on | February 7, 2008 | No Comments

If your writing a program that is designed to run on any modern computer, please consider adding an –inpath option to your program, or -Z as a short option, just to signal that your program ran. Here’s why:

Many other programs need to know if your program exists in the system path. Save a lot of people time, trouble and effort of writing code to recursively search said path to find out if your program is present and executable. Most C libs come with inherent memory leaks, don’t make us use inherent search functions, don’t make us write our own stuff.

–inpath switches should exit with a status of 0, regardless of the EXIT_SUCCESS macro. This saves malloc()’s, contiguous cache and a whole lot of headache.

A good example is here:

Lets say I’m writing a CLI that gets an env variable named EDITOR to find out what editor you want to use. When I call getenv() to fetch EDITOR, I should be able to issue a system call + –inpath to get a result of 0. This lets me know, the user was sane and set the variable correctly, else, do something else.

Otherwise, I have no choice but to over-malloc() needlessly, robbing your computer of precious contiguous memory space for no good reason. What fun is that? If your program is not in the system path, well, duh, its not going to return what I expect. I can even test against EXIT_SUCCESS to make sure I’m testing it right. If EXIT_FAILURE is 0, I could adjust to that by trying to call hkajshdksdhsskjhsdk, or some random number as an executable :)

If your stuff is not in the path, its up to the user (not us programmers) to deal with it. Yes, many functions exist for this, but needlessly, if only we added 3 lines of code to our programs:

case 'Z': fprintf(stderr, "Im Here!\n"); exit 0;

It doesn’t have to be ’0′, it can be up to 255, as long as it doesn’t equal EXIT_FAILURE on that platform. Just make sure you don’t allocate ram (at all) if that option is called. How hard is that? A fork for something that doesn’t allocate is kinder, gentler and more polite to not only your user’s PC, but other people who hope to see if your damn creation exists. Think about it?


Leave a Reply

  • Monkey Plus Typewriter
  • Stack Overflow

  • Me According To Ohloh

  • Meta