Получить строковую переменную, зависящую от конфигурации


#include <unistd.h>
size_t confstr( int name,
char *buf,
size_t len );


The system variable to query.
A pointer to a buffer in which the function can store the value of the system variable.
The length of the buffer, in bytes.




The confstr() function lets applications get or set configuration-defined string values. This is similar to the sysconf() function, but you use it to get string values, rather than numeric values. By default, the function queries and returns values in the system.

The name argument represents the system variable to query. The values are defined in <confname.h>; at least the following name values are valid:

The name of the instruction set architecture for this node's CPU(s).
The domain name.
The name of this node in the network.

Note: A hostname can consist only of letters, numbers, and hyphens, and must not start or end with a hyphen. For more information, see RFC 952.

The name of the hardware manufacturer.
Serial number associated with the hardware.
A value similar to the LD_LIBRARY_PATH environment variable that finds all standard libraries.
The name of the current locale.
This node's hardware type.
A value similar to the PATH environment variable that finds all standard utilities.
The current OS release level.
The contents of the resolv.conf file, excluding the domain name.
The secure RPC domain.
The operating system name.
Time zone string (TZ style)
The current OS version number.

The configuration-defined value is returned in the buffer pointed to by buf, and will be ≤ len bytes long, including the terminating NULL.

To find out the length of a configuration-defined value, call confstr() with buf set to NULL and len set to 0.

To set a configuration value:

  1. OR the name of the value (e.g. _CS_HOSTNAME) with _CS_SET.
  2. Put the new value in a NULL-terminated string.
  3. Set the value of len to 0.

Возвращаемое значение:

if a “get” is done, the value is the length of the configuration-defined value
Возникла ошибка. Код ошибки записан в errno.

You can compare the confstr() return value against len to see if the configuration-defined value was truncated when retrieving a value (you can't do this when setting a value).

Коды ошибок:

The name argument isn't a valid configuration-defined value.


Print information similar to that returned by the uname() function:

#include <unistd.h>
#include <stdio.h>
#include <limits.h>
#define BUFF_SIZE (256 + 1)
int main( void )
char buff[BUFF_SIZE];
if ( confstr( _CS_SYSNAME, buff, BUFF_SIZE ) > 0 )
printf( "System name: %s\n", buff );
if ( confstr( _CS_HOSTNAME, buff, BUFF_SIZE ) > 0 )
printf( "Host name: %s\n", buff );
if ( confstr( _CS_RELEASE, buff, BUFF_SIZE ) > 0 )
printf( "Release: %s\n", buff );
if ( confstr( _CS_VERSION, buff, BUFF_SIZE ) > 0 )
printf( "Version: %s\n", buff );
if ( confstr( _CS_MACHINE, buff, BUFF_SIZE ) > 0 )
printf( "Machine: %s\n", buff );
if ( confstr( _CS_SET | _CS_HOSTNAME, "myhostname", 0 ) != 0 )
printf( "Hostname set to: %s\n", "myhostname" );
return (0);


POSIX 1003.1

Точка остановки потока
Обработчик прерываний
Обработчик сигналов
В потоке


The confstr() function is part of a draft standard; its interface and/or behavior may change in the future.

Тематические ссылки:

pathconf(), sysconf()

getconf, setconf в Справочнике по Утилитам

Предыдущий раздел: Описание API системной библиотеки