argv[0], etc

H

Hal Styli

hello,

Could someone tell me if the following code is standard C or am I being
naughty.
Essentially I want to point to the progam name by scanning along the
pathname pointed to by argv[0] but Im not sure if I can pass around the
'address' copied into myname in the manner shown. Are there scenarios /
implementations where accessing data pointed to by myname is not allowed?

Im a bit confused on pointer scope and afriad of memory access violations.

As a side issue is fname robust enough?

Thanks in advance.
Hal.

/*: 1:*/ /*showname*/
/*: 2:*/
/*: 3:*/ #include <stdio.h>
/*: 4:*/ #include <stdlib.h>
/*: 5:*/ #include <string.h>
/*: 6:*/
/*: 7:*/ #define PATHDELIM '\\'
/*: 8:*/
/*: 9:*/ char *myname;
/*:10:*/
/*:11:*/ #define PR(x) fprintf(stderr, #x " = %d\n", x)
/*:12:*/ #define PRS(x) fprintf(stderr, #x " = \"%s\"\n", x)
/*:13:*/
/*:14:*/ /*------------------------------------------------------------*/
/*:15:*/ char *fname(char *p)
/*:16:*/ {
/*:17:*/ /*similar to platform specific....
/*:18:*/ fnsplit(p, NULL, NULL, name, NULL)
/*:19:*/ and _splitpath(p, NULL, NULL, name, NULL)
/*:20:*/ ...but hopefully this is fully standard C?
/*:21:*/ */
/*:22:*/
/*:23:*/ char *q;
/*:24:*/
/*:25:*/ while(q=strchr(p,PATHDELIM))
/*:26:*/ p=q+1;
/*:27:*/
/*:28:*/ return p;
/*:29:*/ }
/*:30:*/ /*------------------------------------------------------------*/
/*:31:*/ void fred()
/*:32:*/ {
/*:33:*/ char *myname2;
/*:34:*/
/*:35:*/ PRS(myname); /* ok to use outside of main? */
/*:36:*/
/*:37:*/ myname2=myname;
/*:38:*/
/*:39:*/ PRS(myname2);
/*:40:*/ }
/*:41:*/ /*------------------------------------------------------------*/
/*:42:*/ int main(int argc, char *argv[] )
/*:43:*/ {
/*:44:*/ PR(argc); /* to avoid annoying warnings about not using argc*/
/*:45:*/
/*:46:*/ PRS(fname("no delimiter")); /* quick test 1*/
/*:47:*/ PRS(fname("")); /* quick test 2*/
/*:48:*/
/*:49:*/ myname= NULL;
/*:50:*/ PRS(myname); /* quick test 3*/
/*:51:*/
/*:52:*/ /* If argv[0] or fname(argv[0]) gives back nothing then I dont get
any output - fair enough */
/*:53:*/ PRS(fname(argv[0]));
/*:54:*/
/*:55:*/ myname=fname(argv[0]); /*meaningful to assign a global pointer to
one in main?*/
/*:56:*/
/*:57:*/ PRS(myname); /*should bomb on some systems?*/
/*:58:*/
/*:59:*/ fred();
/*:60:*/
/*:61:*/ return 0;
/*:62:*/ }
/*:63:*/ /*------------------------------------------------------------*/
 
D

Derk Gwen

# Essentially I want to point to the progam name by scanning along the
# pathname pointed to by argv[0] but Im not sure if I can pass around the

You need to verify your system doesn't set argv[0] to 0.

Also for systems like unix, the exec functions allows the executable file and
argv[0] to be completely unrelated.
 
M

Mark McIntyre

hello,

Could someone tell me if the following code is standard C or am I being
naughty.

/*:10:*/
/*:11:*/ #define PR(x) fprintf(stderr, #x " = %d\n", x)
/*:12:*/ #define PRS(x) fprintf(stderr, #x " = \"%s\"\n", x)

yuck. Get rid of the BASIC style line numbering, and the pointless
macros, and then repost. This is unreadable. If your editor can't
tell you which line is which, get a better editor.....
 
H

Hal Styli

yuck. Get rid of the BASIC style line numbering, and the pointless
macros, and then repost. This is unreadable. If your editor can't
tell you which line is which, get a better editor.....

The intention was to cut down on reposts which are more painful in my view.
The macro is effectively a so here goes....

/*showname*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PATHDELIM '\\'

char *myname;

/*------------------------------------------------------------*/
char *fname(char *p)
{
/*similar to platform specific....
fnsplit(p, NULL, NULL, name, NULL)
and _splitpath(p, NULL, NULL, name, NULL)
...but hopefully this is fully portable/ANSI/etc?
*/

char *q;

if(!p)
return p;

while(q=strchr(p,PATHDELIM))
p=q+1;

return p;
}
/*------------------------------------------------------------*/
void fred()
{
char *myname2;

puts(myname); /* ok to use outside of main? */

myname2=myname;

puts(myname2);
}
/*------------------------------------------------------------*/
int main(int argc, char **argv )
{
/* to avoid annoying warnings about not using argc*/
printf("argc=%d\n", argc);

puts(fname("no delimiter")); /* quick test 1*/
puts(fname("")); /* quick test 2*/

myname= NULL;
puts(fname(myname)); /* quick test 3*/

puts(fname(argv[0]));

myname=fname(argv[0]);

puts(myname);

fred();

return 0;
}
/*------------------------------------------------------------*/
 
M

Mark McIntyre

The intention was to cut down on reposts which are more painful in my view.
The macro is effectively a so here goes....
(snippage)
char *q;

if(!p)
return p;

Just return NULL here... same difference, gives the compiler less to
do.

(snippage)
puts(myname); /* ok to use outside of main? */

yes, its a file-scope variable.
myname= NULL;
puts(fname(myname)); /* quick test 3*/

this is not allowed - you can't pass NULL to puts, its not a string.
You could pass an empty string tho.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,218
Latest member
GracieDebo

Latest Threads

Top