Get process name w/o using argv[0] in C function?
Jon Brawn
jonb at specialix.co.uk
Mon Aug 6 21:52:04 AEST 1990
davecb at yunexus.YorkU.CA (David Collier-Brown) writes:
>jonb at specialix.co.uk (Jon Brawn) writes:
>>Try this sort of thing:
>[function that saves argv[0] in a static]
> Er, that substitutes a globally callable function for a globally
>referable variable. Net gain? more code to do the same thing, value
>is read-only.
^^^^^^^^^^
Course its read only! When was the last time you wanted to change the name
that you where invoked as? Shouldn't be allowed!
There are often occasions when I would like to be able to label something
as read only (yeah, I could grock the MMU on my machine, create a page
entry, set the appropriate flags and stuff, but its a hell of a chore!).
Hiding data in statics within functions is a relatively easy way of
achieving the same net effect.
You can also use the same approach for controlling access to objects. Keep
the actual data storage within one .c file as a static, and have functions
within that file that can be callable from outside the file which do
error checking on their arguments (wow! what a concept - error checking!)
before asigning to the object in question.
It also means that you can change the algorithm within the module at any
time, change the shape of the data, and still maintain the same interface
to the rest of the code. It would be perfectly possible to re-write the
routine I posted so that it did scan the proc table for the program name,
and still not have to change a single line of the main program. Or perhaps
a line could be added to the function which only saved the last part of
the programs name, stripping of any leading path. (e.g. store "myprog" rather
than "/usr/jonb/ace_progs/myprog"). Or perhaps it could store the name in
EBCDIC rather than ASCII (perhaps for efficiancy reasons?)
> Looks like a bad tradeoff unless you have an imposed religious
>requirement to avoid global variables...
Hmm. I remember getting 0% for a piece of course work at college for using
a GOTO statement in Pascal, because it wasn't the sort of thing one did
in Pascal. Religious convictions? Possibly a more open outlook on future
trends....maybe the global will go the same way as the goto (I can but
hope).
What if the original question had been about something somewhat larger than
the program name? where would you stand then? Look at the streams routines -
compare them with the routines for clists. Whereas the cblock manipulation
was left mainly to the programmer (forming linked lists and all that) the
streams interface has lots of little functions to do it for you. Nuff said?
>--dave
--Jon.
--
Jon Brawn, Specialix, 3 Wintersells Road, Byfleet, Surrey, KT14 7LF, UK.
Tel: +44(0)9323-54254, Fax:+44(0)9323-52781, jonb at specialix.co.uk
or: {backbone}!mcsun!ukc!slxsys!jonb
``Once upon a time, not so very long ago, in a land, not so very far away''
More information about the Comp.unix.questions
mailing list