linux coreutils 'basename' program question

D

David d'Angers

hi group
the following lines are taken from basename.c of the linux coreutil
group of programs
i don't understand the meaning of several of the functions:

initialize_main (&argc, &argv); why initialize main ?

atexit (close_stdout); when do we need to do a close stdout ?


setlocale (LC_ALL, ""); why do we care about locale ?

bindtextdomain (PACKAGE, LOCALEDIR); ??
textdomain (PACKAGE); ??


btw: the coreutils are application level programs yet it seems to have
many elements only found in system level programs (a total newbie's
comment), and i find it quite hard to read and learn


int
main (int argc, char **argv)
{
char *name;

initialize_main (&argc, &argv);
program_name = argv[0];
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);

atexit (close_stdout);
 
M

Mark Bluemel

David said:
hi group
the following lines are taken from basename.c of the linux coreutil
group of programs

You'd probably do a lot better asking in a linux or gnu newsgroup or
forum, I'm afraid. We're not great on retrospective mindreading...

I'll make a few comments, but they're totally on the basis of ignorance
tempered by a brief scan of the manual pages for a few functions and a
quick Google.
int
main (int argc, char **argv)
{
char *name;

initialize_main (&argc, &argv);

I can't add much beyond the final comment in
http://www.hostingforum.ca/170773-writing-linux-utilities.html
program_name = argv[0];
setlocale (LC_ALL, "");

According to my reading of the manual page, this ensures the locale has
been initialized in accordance with the environment settings.
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);

Again, according to my reading of the manual pages, these initialize the
subsystem which will deal with message handling (ensuring that messages
tailored to the locale are displayed, I imagine).
atexit (close_stdout);

This is to ensure that the close_stdout routine (which perhaps does more
than just closing stdout) is called before the program exits.
 
C

Clever Monkey

David said:
the following lines are taken from basename.c of the linux coreutil
group of programs
i don't understand the meaning of several of the functions:

initialize_main (&argc, &argv); why initialize main ?

atexit (close_stdout); when do we need to do a close stdout ?
Best ask in a Linux programming group.
setlocale (LC_ALL, ""); why do we care about locale ?
Turn the question around. Why do we not care about locale. Linux is
intended to be used on a variety of platforms in a variety of locales.
bindtextdomain (PACKAGE, LOCALEDIR); ??
textdomain (PACKAGE); ??
Ask your local Linux hacker.

btw: the coreutils are application level programs yet it seems to have
many elements only found in system level programs (a total newbie's
comment), and i find it quite hard to read and learn
See me comment about where Linux is expected to be run. In these sorts
of cases the difference between application and kernel takes on a new,
more specific, meaning.
 
S

santosh

David said:
hi group
the following lines are taken from basename.c of the linux coreutil
group of programs
i don't understand the meaning of several of the functions:

initialize_main (&argc, &argv); why initialize main ?

This functions apparently performs some operations on main's arguments.
It is very probably checking and parsing the program's invocation
arguments, i.e. command-line parameters.
atexit (close_stdout); when do we need to do a close stdout ?

No idea. atexit() registers a function to be called upon normal
termination. Presumably the close_stdout() function closes open streams
and does some clean-up. Refer to it's source for more information.

All open streams are automatically closed upon normal program
termination, but still, it is better form to explicitly close them
yourself, if only to detect and report on errors.
setlocale (LC_ALL, ""); why do we care about locale ?

Just because you don't care about locale doesn't mean that a widely used
piece of software like basename can be so cavalier. This particular
call sets the entire locale to values based on implementation specific
environment variables.
bindtextdomain (PACKAGE, LOCALEDIR); ??

man bindtextdomain
textdomain (PACKAGE); ??

man textdomain
btw: the coreutils are application level programs yet it seems to have
many elements only found in system level programs (a total newbie's
comment), and i find it quite hard to read and learn

They are arguably system level programs. There is no sharp demarcation
between application level and system level programs. Programs vary from
end-user applications to system utilities, (like coreutils), and
libraries, to the OS kernel proper and perhaps even lower level code.

Also the precise meaning of these terms depend on the architecture and
operating system in use.

<snipped incomplete code>
 
D

David d'Angers

Thanks Mark for the link
it's quite helpful
and everyone else too

next time i'll do more research first myself before making a post
 
O

otavio.ribeiro

Hi David,

about the setlocale(LC_ALL,""). We do that because by standard, all C
applications are started with the 'C' locale. So, localizations will
never work at all. To setup your application with the environment
locales you have to call setlocale(LC_ALL,""). Other else, you will
always receive English messages.

regards,
Otavio Ribeiro
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,223
Latest member
Jurgen2087

Latest Threads

Top