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:*/ /*------------------------------------------------------------*/
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:*/ /*------------------------------------------------------------*/