Mail Archives: djgpp/1994/03/28/04:28:33
> but I don't want the full path listed in the output of printing
> argv[0] .....
Inclusion of the full path in argv[0] is the standard behavior of Unix
C run-time environment, and DOS-based C compilers rightfully imitate this.
For one thing, it lets you catch instances where you inadvertently invoke
program in another directory (e.g., PATH mess-ups).
If you don't want this for some reason, write thusly:
#include <string.h>
....
frpintf(stderr, "Usage: %s <filename1> <filename2>\n",
strrchr(argv[0], '/') + 1);
The library function strrchr(s, c) returns a pointer to the LAST occurence of
a character c in the string s. If you like being defensive (and you should!),
then test the value returned by strrchr() lest it returns NULL pointer (if
somehow there is no slash character in argv[0]), before you send it to fprintf().
If you use to port your code to other MS-DOS-based compilers, test for a back-
slash as well as forward slash, as many DOS C compiler don't bother to make the
conversion.
Eli
- Raw text -