delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/03/28/04:28:33

To: meetze AT charlie DOT ece DOT scarolina DOT edu (Murle C. Meetze III)
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: argv[0] problem(unwanted path in printout)
Date: Mon, 28 Mar 94 10:38:35 +0200
From: eliz AT is DOT elta DOT co DOT il

> 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019