9

I finally got myself to look at some Linux code. I am looking right now at ls.c.

At the function usage() at the bottom I found a lot of these statements:

fputs (_("\
List information about the FILEs (the current directory by default).\n\
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n\
\n\
"), stdout);

What does _("") mean? Is it something like L"string" or _T"string" or something completely new? I must also admit I don't know what words to use to search for something like this.

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 4
    Look through the source for a `#define _`. Or compile with `gcc -PE` to see the preprocessor output. – Kerrek SB May 12 '12 at 17:54
  • 3
    Just a small nitpick, this is not actually Linux code. The code you are looking at is part of GNU coreutils. While GNU coreutils are a part of many Linux distributions, they are not a part of Linux. It is possible to have a working Linux system without GNU coreutils. – Greg Inozemtsev May 12 '12 at 19:21
  • One widely used Linux without gnu utils is Android – Gunther Piez May 12 '12 at 19:30
  • @GregInozemtsev yeah I know, but I have somewhere to beginn and I decided to beginn with core-utils :p –  May 12 '12 at 19:59
  • 1
    possible duplicate of [Underscore function](http://stackoverflow.com/questions/3336056/underscore-function) – Wooble May 21 '12 at 14:24
  • Does this answer your question? [What does \_("text"), i.e. underscore bracket char, do?](https://stackoverflow.com/questions/15244397/what-does-text-i-e-underscore-bracket-char-do) – phuclv Oct 15 '22 at 05:01

3 Answers3

11

It's a convention used by libintl a.k.a. gettext, for translatable strings. When it runs, gettext function (which _ is aliased to) will return either original or translated string, depending on locale settings and availability of said string.

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • thx about the extra info with gettext, gettext seems to be very important and I will have a further look at this topic. :) –  May 12 '12 at 18:02
4

_ is a macro often used with the GNU gettext package.

GNU gettext is a package that:

  • takes lists of message strings intended for humans to read, and translations of those strings into other languages, and compiles them into databases;
  • provides a routine, named gettext(), to look up message strings in that database and return the translation for the message into a particular language.

If a program wanted to print a message in the language selected by the user in an environment variable and picked up by a setlocale() call, it would normally do something such as

fprintf(stderr, gettext("I cannot open the file named %s\n"), filename);

gettext() would look up the appropriate translation of the string "I cannot find the file named %s\n" in the database and return the translated string.

However, that's a bit awkward; as the documentation for GNU gettext notes, many programs use a macro to make just _(string) be an alias for gettext(string).

1

Function names can, of course, contain an _, and an _ can begin a function name. So, it's possible to name a function simply _.

All that's happening is that a #define or a real function is called _.

DigitalRoss
  • 143,651
  • 25
  • 248
  • 329