name of running code

I

Ian Collins

Albretch said:
I need to get the name of the very code running in order to include it in a
time stamped log file.
.
As I remember, there is an ANSI standard way to ask a running program for
its name. But I forgot the specifics of it.
.
How is it that you do that?
.
argv[0] ?
 
A

Albretch Mueller

I need to get the name of the very code running in order to include it in a
time stamped log file.
..
As I remember, there is an ANSI standard way to ask a running program for
its name. But I forgot the specifics of it.
..
How is it that you do that?
..
lbrtchx
 
A

Andrew Poelstra

Albretch said:
I need to get the name of the very code running in order to include it in a
time stamped log file.
.
As I remember, there is an ANSI standard way to ask a running program for
its name. But I forgot the specifics of it.
.
How is it that you do that?
.
argv[0] ?

/* You must define main() this way: */
int main (int argc, char *argv[])
{
/* Code here */
return 0;
}

argc contains the number of arguments plus one, and argv[]
is the array of them. argv[0] is almost always the program
name, as Ian stated.
 
S

spibou

In the thread "Can u tell me the explanation reg this problem" Eric
Sosman
points out that it is not guaranteed by the standard that argv[0] will
not be a
NULL pointer and furthermore that the name of an executable may not be
uniquely defined. So it is not even clear what a standard function
would be
supposed to provide as "name of running code".

By the way is there a way to refer to a specific post in a newsgroup as
opposed
to thead title ?
 
W

Walter Roberson

Andrew Poelstra said:
argc contains the number of arguments plus one, and argv[]
is the array of them. argv[0] is almost always the program
name, as Ian stated.

On freestanding systems, argv[0] is unlikely to be the program name.

[NB: the standards indicate that if no program name is available,
then argv[0][0] shall be the null character.]


On Unix systems, argv[0] might be a path to the executable, or
might be just the last component of the path, or might be the
path without the file extension... or might be something totally
unrelated to the path, since most Unix systems allow the invoking
program to set the new argv[0] independantly of the path to the
executable.

The C89 standard does explicitly use the phrase "program name"
in the relevant clause explaining the "intent" of the values, but
there are a lot of different semantic interpretations about what
exactly a "program name" -is-.
 
A

Andrew Poelstra

Andrew Poelstra said:
argc contains the number of arguments plus one, and argv[]
is the array of them. argv[0] is almost always the program
name, as Ian stated.

On freestanding systems, argv[0] is unlikely to be the program name.

True; but the OP wants logging, and logging implies a file system, which
implies that the executable is a file, which implies that it has a name.
Flimsy logic, obviously, but it may very well apply to the OP's situation.
[NB: the standards indicate that if no program name is available,
then argv[0][0] shall be the null character.]

Yes; I should have mentioned that.
On Unix systems, argv[0] might be a path to the executable, or
might be just the last component of the path, or might be the
path without the file extension... or might be something totally
unrelated to the path, since most Unix systems allow the invoking
program to set the new argv[0] independantly of the path to the
executable.

Well, a log file should be aware of all that. I assume he needs the
name to write something like "myprog (10/10/10 10:10AM): Deleted home
folder" to the log file. If he's using argv to determine the folder
name, it is true that that is unreliable.
The C89 standard does explicitly use the phrase "program name"
in the relevant clause explaining the "intent" of the values, but
there are a lot of different semantic interpretations about what
exactly a "program name" -is-.

True;
 
A

Andrew Poelstra

In the thread "Can u tell me the explanation reg this problem" Eric
Sosman
points out that it is not guaranteed by the standard that argv[0] will
not be a
NULL pointer and furthermore that the name of an executable may not be
uniquely defined. So it is not even clear what a standard function
would be
supposed to provide as "name of running code".

By the way is there a way to refer to a specific post in a newsgroup as
opposed
to thead title ?

Quote context. Judging by your GMail address, I assume that you are using
GGroups, and you should take note of the following:

Click 'More Options' and then 'Add Reply'. Don't use the broken reply link
at the bottom of the post. See CBFalconer's sig for more information.

The answer to your last question is yes. You can refer to it by <where newsid will be a long random-looking string available when you click
'More Options'.
 
A

Andrew Poelstra

Andrew Poelstra said:
argc contains the number of arguments plus one, and argv[]
is the array of them. argv[0] is almost always the program
name, as Ian stated.

On freestanding systems, argv[0] is unlikely to be the program name.

True; but the OP wants logging, and logging implies a file system, which
implies that the executable is a file, which implies that it has a name.
Flimsy logic, obviously, but it may very well apply to the OP's situation.
[NB: the standards indicate that if no program name is available,
then argv[0][0] shall be the null character.]

Yes; I should have mentioned that.
On Unix systems, argv[0] might be a path to the executable, or
might be just the last component of the path, or might be the
path without the file extension... or might be something totally
unrelated to the path, since most Unix systems allow the invoking
program to set the new argv[0] independantly of the path to the
executable.

Well, a log file should be aware of all that. I assume he needs the
name to write something like "myprog (10/10/10 10:10AM): Deleted home
folder" to the log file. If he's using argv to determine the folder
name, it is true that that is unreliable.
The C89 standard does explicitly use the phrase "program name"
in the relevant clause explaining the "intent" of the values, but
there are a lot of different semantic interpretations about what
exactly a "program name" -is-.

True;

Should have been a period there, not a semicolon. I have no additions to
that thought.
 
D

Dik T. Winter

> argc contains the number of arguments plus one, and argv[]
> is the array of them. argv[0] is almost always the program
> name, as Ian stated.

But not always. It can be an empty string, and the contents can also
be thoroughly misleading.
 
S

spibou

Andrew said:
Quote context.

I'm not sure what context you would have me quote in this case , the
whole
Sosman post I was referring to ?
Judging by your GMail address, I assume that you are using
GGroups, and you should take note of the following:

Click 'More Options' and then 'Add Reply'. Don't use the broken reply link
at the bottom of the post. See CBFalconer's sig for more information.

I don't see any "More Options" link. I have clicked on the links on
someone's
signature , can't remember who , and read what was there. As far as I
can remember
it was talking about top-posting and I haven't done that.
The answer to your last question is yes. You can refer to it by <where newsid will be a long random-looking string available when you click
'More Options'.

If I click on "show options" and then on "Show original" I get the
header. On the header
there is a line starting with "Message-ID:". Is this what you are
referring to ?
 
C

CBFalconer

In the thread "Can u tell me the explanation reg this problem"
Eric Sosman points out that it is not guaranteed by the standard
that argv[0] will not be a NULL pointer and furthermore that the
name of an executable may not be uniquely defined. So it is not
even clear what a standard function would be supposed to provide
as "name of running code".

By the way is there a way to refer to a specific post in a
newsgroup as opposed to thead title ?

Not reliably. There is no reason to assume that any such specific
post is available to your reader. That is why we always include
adequate context.

--
"Our enemies are innovative and resourceful, and so are we.
They never stop thinking about new ways to harm our country
and our people, and neither do we." -- G. W. Bush.
"The people can always be brought to the bidding of the
leaders. All you have to do is tell them they are being
attacked and denounce the pacifists for lack of patriotism
and exposing the country to danger. It works the same way
in any country." --Hermann Goering.
 
D

Default User

Andrew Poelstra wrote:

I don't see any "More Options" link. I have clicked on the links on
someone's
signature , can't remember who , and read what was there. As far as I
can remember
it was talking about top-posting and I haven't done that.

See below. As of this time, the US version seems to have changed to
quoting with the default reply.


Brian
 
N

Nelu

In the thread "Can u tell me the explanation reg this problem"
Eric Sosman points out that it is not guaranteed by the standard
that argv[0] will not be a NULL pointer and furthermore that the
name of an executable may not be uniquely defined. So it is not
even clear what a standard function would be supposed to provide
as "name of running code".

By the way is there a way to refer to a specific post in a
newsgroup as opposed to thead title ?

Not reliably. There is no reason to assume that any such specific
post is available to your reader. That is why we always include
adequate context.
Since we are a little OT:
Although I have nothing against anyone's opinion toward
politics, no matter if they are right, wrong or gray, I think
your old signature (Google netiquette) was much more useful :).
 
E

Eric Sosman

Andrew said:
[...]
True; but the OP wants logging, and logging implies a file system, which
implies that the executable is a file, [...]

Non sequitur. "The executable" could be a nameless chunk
of code permanently burned into ROM, but could nonetheless work
with "files" on other sorts of media. Going back further yet,
logging does not imply a file system: the log data might just
be "fire and forget" across a communications link, perhaps to a
hard-copy printer or a remote telemetry hookup.

Still, though, argv[0] is usually a good tag for log
entries. On days when I'm feeling particularly paranoid I'll
write things like

static const char *progname = __FILE__;
...
int main(int argc, char *argv[]) {
if (argv[0] != NULL && argv[0][0] != '\0')
progname = argv[0];
...
...
...
if (something_went_wrong) {
perror (progname);
exit (EXIT_FAILURE);
} ...

Then I usually shake my head ruefully over such an overblown
"belt *and* suspenders" attitude, and overreact foolishly by
omitting all error-checking in my next three programs. ;-)

(I must confess that I actually have written code like
the above, and that never once has it proven necessary ...)
 
A

Andrew Poelstra

I'm not sure what context you would have me quote in this case , the
whole
Sosman post I was referring to ?

Sure. It'd better than nothing.
I don't see any "More Options" link. I have clicked on the links on
someone's
signature , can't remember who , and read what was there. As far as I
can remember
it was talking about top-posting and I haven't done that.

I meant 'Show options'. Sorry about that.
If I click on "show options" and then on "Show original" I get the
header. On the header
there is a line starting with "Message-ID:". Is this what you are
referring to ?

Yep. Glad you thought to click 'Show Original'; I wouldn't have remembered to
tell you that.
 
A

Albretch Mueller

OK, this is what/how I did it, which caomplied without errors or wanings
using the compiler flags: "-ansi -pedantic -Wall"
..
and BTW I am obviously working with some plain files (off a file system)
..
/* x platform (standard ANCI C) */
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

char *getYYYYmmDDHHMMSS(char*, time_t);

int main(int argc, char **argv){
time_t start_t;
int iR;
char *aLogFl;
FILE *LOGFL = NULL;
struct tm *timeinfo;

iR = EXIT_FAILURE;
if((argv != NULL) && (argv[0] != NULL) && (strlen(argv[0]) > 0)){
iR = EXIT_SUCCESS;
time(&start_t);
aLogFl = getYYYYmmDDHHMMSS(argv[0], start_t);
if((LOGFL = fopen(aLogFl, "w+t")) == NULL){
fprintf(stderr, "\n Cannot open log file: %s\n", aLogFl);
goto end;
}
timeinfo = localtime (&start_t);
fprintf(LOGFL, "// __ run date and time are: %s", asctime (timeinfo));
fprintf(LOGFL, "// __ log file is: %s\n", aLogFl);
}

if(LOGFL != NULL){ fclose(LOGFL); }
end:;

exit(iR);
}

char *getYYYYmmDDHHMMSS(char *KdNm, time_t start_t){
int i, iSL = strlen(KdNm), ISS = 0, iBuf, iSz;
char *aKdNm02 = NULL;
char *aLogFl = NULL;

/* gcc [.|./] "/usr/bin/ld: [*]: No such file: File format not recognized"
*/
if(strncmp("./", KdNm, 2) == 0){ ISS = 2; }
else if(strncmp(".", KdNm, 1) == 0){ ISS = 1; }

iBuf = (iSL - ISS) + 1;
aKdNm02 = (char *)malloc(iBuf);
for(i = 0; (i < (iSL - ISS)); ++i){ aKdNm02 = KdNm[i + ISS]; }

iSz = strlen("YYYYMMDDHHMMSS_") + strlen(aKdNm02) + strlen(".log") + 1;
aLogFl = (char*)malloc(iSz);

strftime(aLogFl, sizeof("YYYYmmDDHHMMSS"), "%Y%m%d%H%M%S",
localtime(&start_t));
strcat(aLogFl, "_"); strcat(aLogFl, aKdNm02); strcat(aLogFl, ".log");

return(aLogFl);
}
 

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,183
Messages
2,570,969
Members
47,524
Latest member
ecomwebdesign

Latest Threads

Top