473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

argv[0], etc

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

Nov 14 '05 #1
7 5589

"Hal Styli" <no_spam@all> wrote in message news:40********@127.0.0.1...

As a side issue is fname robust enough? <<>>

Apparenlty not!
/*:49:*/ myname= NULL;
/*:50:*/ PRS(myname); /* quick test 3*/
should have been:
/*:49:*/ myname= NULL;
/*:50:*/ PRS(fname(myname)); /* quick test 3*/


which bombs on Borland, presumably everywhere else too!
Nov 14 '05 #2
# 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.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Elvis was an artist. But that didn't stop him from joining the service
in time of war. That's why he is the king, and you're a shmuck.
Nov 14 '05 #3
"Derk Gwen" <de******@HotPOP.com> wrote in message
news:10*************@corp.supernews.com...
You need to verify your system doesn't set argv[0] to 0.


doesnt making the change: /*:24:*/ if(!p) return p;
catch this ?



Nov 14 '05 #4
On Fri, 16 Jan 2004 12:44:28 -0000, in comp.lang.c , "Hal Styli"
<no_spam@all> wrote:
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.....

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #5
> 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;
}
/*------------------------------------------------------------*/

Nov 14 '05 #6
> The macro is effectively a so here goes....

a 'puts'!

Nov 14 '05 #7
On Sat, 17 Jan 2004 23:24:17 -0000, in comp.lang.c , "Hal Styli"
<no_spam@all> wrote:
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....

(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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Robin Sanderson | last post by:
Sorry in advance if this is a stupid question - I am new to C++. In the process of converting program to be run from the command line into a function to be run from another program I noticed...
9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
28
by: Charles Sullivan | last post by:
I'm working on a program which has a "tree" of command line arguments, i.e., myprogram level1 ]] such that there can be more than one level2 argument for each level1 argument and more than one...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
32
by: mnaydin | last post by:
Assume the main function is defined with int main(int argc, char *argv) { /*...*/ } So, is it permitted to modify the argv array? The standard says "The parameters argc and argv and the strings...
22
by: Joe Smith | last post by:
It is nothing short of embarrassing to feel the need to ask for help on this. I can't see how I would make the main control for this. What I want is a for loop and a test condition. And while I...
4
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
11
by: vicky | last post by:
hi all, please tell me with example, how the *argv point to the the no of strings.
4
by: Romulo Carneiro | last post by:
Hi, I'm programming in Windows XP and i'm trying to get all arguments of some application, but i only have gotten five argv. When i put more then five(5), it didn't display. =>Input Command Line:...
12
by: kevin.eugene08 | last post by:
Hello all, Forgive the somewhat simple question I am sure this will be, but I am having some problem understanding what: char **argv looks like. For instance, i know through trial and error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.