⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_26.html

📁 Linux程序员的工作手册
💻 HTML
字号:
<!-- This HTML file has been created by texi2html 1.27     from library.texinfo on 3 March 1994 --><TITLE>The GNU C Library - System Information</TITLE><P>Go to the <A HREF="library_25.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html">previous</A>, <A HREF="library_27.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html">next</A> section.<P><H1><A NAME="SEC451" HREF="library_toc.html#SEC451" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC451">System Information</A></H1><P>This chapter describes functions that return information about theparticular machine that is in use--the type of hardware, the type ofsoftware, and the individual machine's name.<P><H2><A NAME="SEC452" HREF="library_toc.html#SEC452" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC452">Host Identification</A></H2><P>This section explains how to identify the particular machine that yourprogram is running on.  The identification of a machine consists of itsInternet host name and Internet address; see section <A HREF="library_15.html#SEC227" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_15.html#SEC227">The Internet Namespace</A>.  <A NAME="IDX1846"></A><A NAME="IDX1847"></A><A NAME="IDX1848"></A><P>Prototypes for these functions appear in <TT>`unistd.h'</TT>.  The shellcommands <CODE>hostname</CODE> and <CODE>hostid</CODE> work by calling them.<P><A NAME="IDX1849"></A><U>Function:</U> int <B>gethostname</B> <I>(char *<VAR>name</VAR>, size_t <VAR>size</VAR>)</I><P>This function returns the name of the host machine in the array<VAR>name</VAR>.  The <VAR>size</VAR> argument specifies the size of this array,in bytes.<P>The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure.  Inthe GNU C library, <CODE>gethostname</CODE> fails if <VAR>size</VAR> is not largeenough; then you can try again with a larger array.  The following<CODE>errno</CODE> error condition is defined for this function:<P><DL COMPACT><DT><CODE>ENAMETOOLONG</CODE><DD>The <VAR>size</VAR> argument is less than the size of the host name plus one.</DL><A NAME="IDX1850"></A><P>On some systems, there is a symbol for the maximum possible host namelength: <CODE>MAXHOSTNAMELEN</CODE>.  It is defined in <TT>`sys/param.h'</TT>.But you can't count on this to exist, so it is cleaner to handlefailure and try again.<P><CODE>gethostname</CODE> stores the beginning of the host name in <VAR>name</VAR>even if the host name won't entirely fit.  For some purposes, atruncated host name is good enough.  If it is, you can ignore theerror code.<P><A NAME="IDX1851"></A><U>Function:</U> int <B>sethostname</B> <I>(const char *<VAR>name</VAR>, size_t <VAR>length</VAR>)</I><P>The <CODE>sethostname</CODE> function sets the name of the host machine to<VAR>name</VAR>, a string with length <VAR>length</VAR>.  Only privilegedprocesses are allowed to do this.  Usually it happens just once, atsystem boot time.<P>The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure.The following <CODE>errno</CODE> error condition is defined for this function:<P><DL COMPACT><DT><CODE>EPERM</CODE><DD>This process cannot set the host name because it is not privileged.</DL><P><A NAME="IDX1852"></A><U>Function:</U> long int <B>gethostid</B> <I>(void)</I><P>This function returns the Internet address of the machine the program isrunning on.<P><A NAME="IDX1853"></A><U>Function:</U> int <B>sethostid</B> <I>(long int <VAR>id</VAR>)</I><P>The <CODE>sethostid</CODE> function sets the address of the host machine to<VAR>id</VAR>.  Only privileged processes are allowed to do this.  Usually ithappens just once, at system boot time.<P>The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure.The following <CODE>errno</CODE> error condition is defined for this function:<P><DL COMPACT><DT><CODE>EPERM</CODE><DD>This process cannot set the host name because it is not privileged.</DL><P><H2><A NAME="SEC453" HREF="library_toc.html#SEC453" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC453">Hardware/Software Type Identification</A></H2><P>You can use the <CODE>uname</CODE> function to find out some information aboutthe type of computer your program is running on.  This function and theassociated data type are declared in the header file<TT>`sys/utsname.h'</TT>.<A NAME="IDX1854"></A><P><A NAME="IDX1855"></A><U>Data Type:</U> <B>struct utsname</B><P>The <CODE>utsname</CODE> structure is used to hold information returnedby the <CODE>uname</CODE> function.  It has the following members:<P><DL COMPACT><DT><CODE>char sysname[]</CODE><DD>This is the name of the operating system in use.<P><DT><CODE>char nodename[]</CODE><DD>This is the network name of this particular computer.  In the GNUlibrary, the value is the same as that returned by <CODE>gethostname</CODE>;see section <A HREF="library_26.html#SEC452" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_26.html#SEC452">Host Identification</A>.<P><DT><CODE>char release[]</CODE><DD>This is the current release level of the operating system implementation.<P><DT><CODE>char version[]</CODE><DD>This is the current version level within the release of the operatingsystem.<P><DT><CODE>char machine[]</CODE><DD>This is a description of the type of hardware that is in use.<P>The GNU C Library fills in this field based on the configuration namethat was specified when building and installing the library.  GNU uses athree-part name to describe a system configuration; the three parts are<VAR>cpu</VAR>, <VAR>manufacturer</VAR> and <VAR>system-type</VAR>, and they areseparated with dashes.  Any possible combination of three names ispotentially meaningful, but most such combinations are meaningless inpractice and even the meaningful ones are not necessarily supported byany particular GNU program.<P>Since the value in <CODE>machine</CODE> is supposed to describe just thehardware, it consists of the first two parts of the configuration name:<SAMP>`<VAR>cpu</VAR>-<VAR>manufacturer</VAR>'</SAMP>.<P>Here is a list of all the possible alternatives:<P><BLOCKQUOTE><CODE>"i386-<VAR>anything</VAR>"</CODE>, <CODE>"m68k-hp"</CODE>, <CODE>"sparc-sun"</CODE>,<CODE>"m68k-sun"</CODE>, <CODE>"m68k-sony"</CODE>, <CODE>"mips-dec"</CODE></BLOCKQUOTE></DL><P><A NAME="IDX1856"></A><U>Function:</U> int <B>uname</B> <I>(struct utsname *<VAR>info</VAR>)</I><P>The <CODE>uname</CODE> function fills in the structure pointed to by<VAR>info</VAR> with information about the operating system and host machine.A non-negative value indicates that the data was successfully stored.<P><CODE>-1</CODE> as the value indicates an error.  The only error possible is<CODE>EFAULT</CODE>, which we normally don't mention as it is always apossibility.<P><P>Go to the <A HREF="library_25.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html">previous</A>, <A HREF="library_27.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_27.html">next</A> section.<P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -