#include #include #include #include #include /* * For reason I do not understand, getopt() and friends are not * declared in unistd.h under cygwin. */ #ifdef __CYGWIN__ int getopt(int argc, char* const argv[], const char* optstring); extern char *optarg; extern int optind; extern int opterr; extern int optopt; #endif static const char* basename(const char* fullpath) { const char* rv = strrchr(fullpath, (int)'/'); if( rv == NULL ) { rv = fullpath; } else { ++rv; } return rv; } static void usage(FILE* f, const char* execname) { int rv = 0; fprintf(f, "Usage: %s [-h|-v] ...\n", basename(execname)); if( f == stderr ) { rv = 1; } exit(rv); } int main(int argc, char* argv[]) { int rv = 0; int verbose = 0; int ch = -1; char** fptr = NULL; char* default_argv[] = { ".", NULL }; /* Process command-line parameters. */ opterr = 0; while( (ch = getopt(argc, argv, "hv")) != -1 ) { switch( ch ) { case 'h': usage(stdout, argv[0]); break; case 'v': verbose = 1; break; case '?': /* fall through */ default: usage(stderr, argv[0]); } } /* Adjust argc. */ argc -= optind - 1; /* Adjust argv. */ { char** src = argv + optind; char** dst = argv + 1; for( ; *src != NULL ; ++src, ++dst ) { *dst = *src; } *dst = NULL; } fptr = default_argv; if( argc > 1 ) { fptr = argv + 1; } for( ; *fptr != NULL ; ++fptr ) { char buf[MAXPATHLEN + 1] = ""; if( realpath(*fptr, buf) == NULL ) { fprintf(stderr, "Warning: failed to resolve \"%s\"\n", *fptr); rv = 1; } else { if( verbose ) { printf("%s --> ", *fptr); } printf("%s\n", buf); } } return rv; }