linux coreutils 'basename' program question

Discussion in 'C Programming' started by David d'Angers, Oct 22, 2007.

  1. 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);
    David d'Angers, Oct 22, 2007
    #1
    1. Advertising

  2. David d'Angers

    Mark Bluemel Guest

    David d'Angers wrote:
    > 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.
    Mark Bluemel, Oct 22, 2007
    #2
    1. Advertising

  3. David d'Angers wrote:
    > 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.
    Clever Monkey, Oct 22, 2007
    #3
  4. David d'Angers

    santosh Guest

    David d'Angers wrote:

    > 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>
    santosh, Oct 22, 2007
    #4
  5. 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
    David d'Angers, Oct 22, 2007
    #5
  6. David d'Angers

    Guest

    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

    On 22 out, 14:21, David d'Angers <> wrote:
    > 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);
    , Dec 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sean Berry

    Confused on using basename...

    Sean Berry, Apr 14, 2004, in forum: Perl
    Replies:
    1
    Views:
    486
    Joe Smith
    Apr 14, 2004
  2. Ronald Fischer
    Replies:
    1
    Views:
    15,229
    Jacob
    Jul 22, 2003
  3. Xah Lee
    Replies:
    14
    Views:
    947
    Michele Dondi
    Jan 27, 2004
  4. Peter Saffrey

    basename with extensions

    Peter Saffrey, Aug 30, 2006, in forum: Python
    Replies:
    1
    Views:
    3,261
    Tal Einat
    Aug 30, 2006
  5. basename in C++

    , Oct 5, 2006, in forum: C++
    Replies:
    4
    Views:
    17,141
    Jens Theisen
    Oct 5, 2006
Loading...

Share This Page