> Thank you. It works now. I had figured out how to take command line
> arguments, but needed the atoi() function to finish this off.
>
> shell> vi usleep.c
>
> int main(int argc, char** argv) {
> if ( argc > 1 )
> usleep(atoi(argv[1]));
> }
>
> shell> make usleep
> shell> cp usleep /usr/local/bin/
> shell> usleep 250000 # sleep for a quarter second
Nice job :-) Wanna make it slightly more robust? Me, bored? Don't
mistake this for Good Code. Btw, every programmer places curly braces
'{', whitespace and comments differently, and gets annoyed by every
other programmer's standard. Strange, but true. ;-)
#include <stdio.h>
int main(int argc, char** argv)
{
if ( argc > 1 ) // is there an argument?
/*
* Inline comments are yer bud, but beware of swiney editors that
wordwrap your comments into syntax errors.
* I hate it when that happens.
* There are elegant ways to parse a command line, and this really
* isn't one of them, fwiw.
*/
{
if (isdigit(argv[1][0])) // if arg smells like a number, just do it.
{
// using 'atol' instead of 'atoi', since we want a long
usleep((unsigned long) atol(argv[1])); // cast result to ulong
return(0); // say "goodnight" Dick.
}
if ((strncmp(argv[1],"--usage") == 0)) // like this'll happen...
{
printf("\nUsage: usleep [--usage] [microseconds]\n");
return(0);
}
else // this might happen...
printf("\nusleep: bad argument %s: unknown option\n",argv[1]);
return(-1);
}
// oh noes!!! They didn't give any argument!
printf("\nUsage: usleep [--usage] [microseconds]\n");
return(-1); // didn't check what or if usleep should ret on error.
}
Steve
---------------------------------------------------
PLUG-discuss mailing list -
PLUG-discuss@lists.plug.phoenix.az.us
To subscribe, unsubscribe, or to change you mail settings:
http://lists.PLUG.phoenix.az.us/mailman/listinfo/plug-discuss