How can I get current localization (ru-RU, en-US, en-GB, e.t.c.) of Linux through C?
Thank you.
On a POSIX-compliant system, setlocale(LC_CTYPE, NULL); would return the name of the locale currently selected for category LC_CTYPE.
Have a look at the nl_langinfo and the
localeconv man pages.
How to search for subroutines and/or system calls on a Unix system:
query via apropos.
e.g.: apropos locale
look for results which reference man section 2 (system calls) or man section 3. (subroutines).
e.g. on mac:
querylocale(3) - Get locale name for a specified category
setlocale(3) - natural language formatting for C
have a look at the man pages
Related
I am trying to collect the vendor of an executable file under differents UNIX-LIKE systems ( Red Hat, AIX, Solaris, Oracle Linux ...).
I would like to do the same as the following command on Windows :
WMIC /LOCALE:MS_409 DATAFILE WHERE (Name="C:\\Program Files\\Java\\jdk-10\\bin\\java.exe") GET manufacturer
I have looked at the file and strings command without success.
No clue how to achieve it.
Any help will be appreciated.
As pointed out UNIX files do not have a vendor, mostly because there is no standard for it, but RPM-based distributions like Red Hat provide a "vendor" that can be queried with a command like this:
rpm -qa --queryformat '%{NAME}TAB%{DISTRIBUTION}TAB%{VENDOR}\n'
(Use a TAB character for TAB in the code above if you want to load it into a spreadsheet program or similar)
rpmkeys --querytags lists all known query tags like NAME.
I'd like to find linux analogs of EnumDisplayDevices and EnumDisplaySettingsEx WinAPI functions.
The info I need to get is display name and state (if it active or not), width, height, bits per pixel and frequency.
How can I get this info using C (C++)?
Thanks.
As mentioned by 'Some programmer dude' in the comments you can have to go through the X window system. Most specifically one option would be the RandR protocol. Here is the protocol specification as well as the source code of the command xrandr that invokes the XRR functions and outputs most of the info that you want on the terminal. Look for the place where the
XRRScreenResources *res
is populated and then how the modes are fetched from it using the find_mode() function.
other commands that might assist you and don't go over the RandR extensions could be xprop(1), xdpyinfo(1), xwininfo(1)
Some programmer dude and ramrunner are absolutely correct. For most Linux systems, the graphical "desktop" is based on X Windows. Command-line tools for querying your X "display" include xrandr and xdpyinfo. The C-language source code for both are freely available; you can find many example programs with a Google search.
... HOWEVER ...
X Windows is "client/server". Your Linux "desktop" need not be on your physical PC; your X "display" could just as easily be a Windows desktop. In this case - xrandr and xdpyinfo are still applicable: they refer to where you're displaying (e.g. an XMing client on Windows), and not the physical host Linux is running on.
If you want to query the graphics devicews on your physical server ... then you'll instead use commands like lshw -c display or get-edid
I'm studying c++ and I have found this function of C++ library:
setlocale (http://www.cplusplus.com/reference/clocale/setlocale/) but I'm not able to understand what is it for.
I have used on Ubuntu:
printf ("Locale is: %s\n", setlocale(LC_ALL,"") );
it prints Locale is: en_US.UTF-8
but on macOs it prints:
Locale is: C what does means this C?
In what context and how should it be used?
Read on Linux the setlocale(3) and locale(7) man pages. Read also the internationalization and localization wikipage.
On Debian and Ubuntu, you might run (as root) the dpkg-reconfigure locales to add more locales.
Then you could set your LANG (and LC_ALL and others) environment variables (see environ(7)) to change the language of message.
For example, I have the French UTF-8 locale installed. If I do
% env LC_ALL=fr_FR.UTF-8 ls /tmp/nonexisting
I'm getting the error message in French:
ls: impossible d'accéder à '/tmp/nonexisting': Aucun fichier ou dossier de ce type
If I use the C locale (which is the default one), it is in English:
% env LC_ALL=C ls /tmp/nonexisting
ls: cannot access '/tmp/nonexisting': No such file or directory
As a rule of thumb, you want to export LC_ALL=C LANG=C before running commands that you exhibit on this forum (e.g. because you don't want to show error messages from the compiler or the shell in French).
If you are coding a program which you want to internationalize (ez.g. to be easily usable by people understanding only French or Chinese), you need at least to use gettext(3) (notably for printf format strings!) and perhaps textdomain(3) and you'll need to use msgfmt(1) for dealing with message catalogs. Of course you'll need to build catalogs of translated messages.
Localization also influences how numbers are parsed and printed (with a comma or a dot separating the thousands or the decimal digits) and how money and time get printed and parsed (e.g. strftime(3)).
It's locale specific settings. For example, in some countries comma is used for decimal separator, in others, it's dot. In the US 22,001 is often understood to be 22 thousand and 1, in some European countries that's 22 point 001.
Dates can be given in DD/MM/YYYYY (most of Europe) or MM/DD/YYYY (in the US) format and so forth.
For example: man(1), find(3), updatedb(2)?
What do the numbers in parentheses (Brit. "brackets") mean?
It's the section that the man page for the command is assigned to.
These are split as
General commands
System calls
C library functions
Special files (usually devices, those found in /dev) and drivers
File formats and conventions
Games and screensavers
Miscellanea
System administration commands and daemons
Original descriptions of each section can be seen in the Unix Programmer's Manual (page ii).
In order to access a man page given as "foo(5)", run:
man 5 foo
The section the command is documented in the manual. The list of sections is documented on man's manual. For example:
man 1 man
man 3 find
This is useful for when similar or exactly equal commands exist on different sections
The reason why the section numbers are significant is that many years ago when disk space was more of an issue than it is now the sections could be installed individually.
Many systems only had 1 and 8 installed for instance. These days people tend to look the commands up on google instead.
As #Ian G says, they are the man page sections. Let's take this one step further though:
1. See the man page for the man command with man man, and it shows the 9 sections as follows:
DESCRIPTION
man is the system's manual pager. Each page argument given
to man is normally the name of a program, utility or func‐
tion. The manual page associated with each of these argu‐
ments is then found and displayed. A section, if provided,
will direct man to look only in that section of the manual.
The default action is to search in all of the available sec‐
tions following a pre-defined order ("1 n l 8 3 2 3posix 3pm
3perl 5 4 9 6 7" by default, unless overridden by the SEC‐
TION directive in /etc/manpath.config), and to show only the
first page found, even if page exists in several sections.
The table below shows the section numbers of the manual fol‐
lowed by the types of pages they contain.
1 Executable programs or shell commands
2 System calls (functions provided by the kernel)
3 Library calls (functions within program libraries)
4 Special files (usually found in /dev)
5 File formats and conventions eg /etc/passwd
6 Games
7 Miscellaneous (including macro packages and conven‐
tions), e.g. man(7), groff(7)
8 System administration commands (usually only for root)
9 Kernel routines [Non standard]
A manual page consists of several sections.
2. man <section_num> <cmd>
Let's imagine you are Googling around for Linux commands. You find the OPEN(2) pg online: open(2) — Linux manual page.
To see this in the man pages on your pc, simply type in man 2 open.
For FOPEN(3) use man 3 fopen, etc.
3. man <section_num> intro
To read the intro pages to a section, type in man <section_num> intro, such as man 1 intro, man 2 intro, man 7 intro, etc.
To view all man page intros in succession, one-after-the-other, do man -a intro. The intro page for Section 1 will open. Press q to quit, then press Enter to view the intro for Section 8. Press q to quit, then press Enter to view the intro for Section 3. Continue this process until done. Each time after hitting q, it'll take you back to the main terminal screen but you'll still be in an interactive prompt, and you'll see this line:
--Man-- next: intro(8) [ view (return) | skip (Ctrl-D) | quit (Ctrl-C) ]
Note that the Section order that man -a intro will take you through is:
Section 1
Section 8
Section 3
Section 2
Section 5
Section 4
Section 6
Section 7
This search order is intentional, as the man man page explains:
The default action is to search in all of the available sections follow‐
ing a pre-defined order ("1 n l 8 3 2 3posix 3pm 3perl 5 4 9 6 7" by default, unless overrid‐
den by the SECTION directive in /etc/manpath.config)
Why did they choose this order? I don't know (please answer in the comments if you know), but just realize this order is correct and intentional.
Related:
Google search for "linux what does the number mean in parenthesis after a function?"
SuperUser: What do the parentheses and number after a Unix command or C function mean?
Unix & Linux: What do the numbers in a man page mean?
Note also that on other unixes, the method of specifying the section differs. On solaris, for example, it is:
man -s 1 man
It indicates the section of the man pages the command is found in. The -s switch on the man command can be used to limit a search to certain sections.
When you view a man page, the top left gives the name of the section, e.g.:
User Commands printf(1)
Standard C Library Functions printf(3C)
So if you are trying to look up C functions and don't want to accidentally see a page for a user command that shares the same name, you would do 'man -s 3C ...'
Wikipedia details about Manual Sections:
General commands
System calls
Library functions, covering in particular the C standard library
Special files (usually devices, those found in /dev) and drivers
File formats and conventions
Games and screensavers
Miscellanea
System administration commands and daemons
Compiling on the shared CentOS server is not allowed. Therefore, I compile my program in my Debian computer, linking it with Debian's system libraries such as libc, etc. Then I upload my program and the Debian system libraries and my program works. The only problem is that setlocale() does not work at CentOS. CentOS has "en_US.utf8" installed and works on all programs except mine. I suspect that I have to also upload Debian's locale files ? How could I link my program to the Debian locale files ? I tried to use LOCPATH but I am unsure of how it works exactly. Which files do I have to link to and how ?
C program:
setenv("LOCPATH", "/", 1);
if (setlocale(LC_ALL, "en_US.utf8") == NULL) {
puts("not set");
}
I used a hex editor to modify the path to /usr/lib/locale/locale-archive which apparently is the only file that setlocale() uses according to strace. This method is dirty but it worked.
According to man LOCPATH, this environment variable is non-standard, so its use is not recommended. No examples are given anywhere of how to use it nor what is meant exactly by a path to "locale's object files".
I guess the only real way of modifying the path is a glibc modification and recompilation.
Quote: LOCPATH is an environment variable that tells the setlocale() function the name of the directory from which to load locale object files. If LOCPATH is not defined, the default directory /usr/lib/nls/locale is searched. LOCPATH is similar to the PATH environment variable; it contains a list of z/OS UNIX directories separated by colons.
So just specifying / and hoping that it does a recursive search will not work.
You could also produce a static binary and upload that to the host.