#include #include #include #include #include const char* progname; const char* progversion = "1.0"; static void usage(FILE* f) { fprintf(f, "\n"); fprintf(f, "Usage: %s [options] \n", progname); fprintf(f, "\n"); fprintf(f, " options:\n"); fprintf(f, " -h or --help : Print help.\n"); fprintf(f, " -v or --version : Display version.\n"); fprintf(f, "\n"); } static const char* basename(const char* fullpath) { const char* rv = NULL; assert(fullpath); rv = strrchr(fullpath, '/'); return rv ? ++rv : fullpath; } int main(int argc, char* argv[]) { int rv = -1; int i = 0; progname = basename(argv[0]); if( argc == 1 ) { usage(stdout); rv = 1; } /* * Check command-line parameters. */ for( i = 1 ; i < argc ; ++i ) { if( (strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0) ) { usage(stdout); rv = 0; break; } if( (strcmp(argv[i], "-v") == 0) || (strcmp(argv[i], "--version") == 0) ) { printf("%s: v%s\n", progname, progversion); rv = 0; break; } if( argv[i][0] == '-' ) { fprintf(stderr, "\n"); fprintf(stderr, "%s: Error: Unknown option \"%s\"\n", progname, argv[i]); usage(stderr); rv = 1; break; } /* Leave i pointing to the first non-option parameter. */ break; } /* * i should be pointing to the last parameter on the command line. */ if( i < argc - 1 ) { int j = 0; fprintf(stderr, "\n"); fprintf(stderr, "%s: Error: Too many arguments: \"", progname); for( j = i + 1 ; j < argc ; ++j ) { fprintf(stderr, "%s%s", argv[j], (j == argc - 1) ? "" : " "); } fprintf(stderr, "\"\n"); usage(stderr); rv = 1; } /* * Check to see if the value for is valid. */ if( rv == -1 ) { char* endptr = NULL; long usecs = 0; usecs = strtol(argv[i], &endptr, 0); if( *endptr != '\0' ) { fprintf(stderr, "\n"); fprintf(stderr, "%s: Error: Unable to convert \"%s\" to a long integer\n", progname, argv[i]); usage(stderr); rv = 1; /* * usleep */ } else { usleep(usecs); rv = 0; } } return (rv == -1) ? 0 : rv; }