File: C:/Ruby27-x64/share/doc/ruby/html/Etc.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>module Etc - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="module">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-confstr">::confstr</a>
<li ><a href="#method-c-endgrent">::endgrent</a>
<li ><a href="#method-c-endpwent">::endpwent</a>
<li ><a href="#method-c-getgrent">::getgrent</a>
<li ><a href="#method-c-getgrgid">::getgrgid</a>
<li ><a href="#method-c-getgrnam">::getgrnam</a>
<li ><a href="#method-c-getlogin">::getlogin</a>
<li ><a href="#method-c-getpwent">::getpwent</a>
<li ><a href="#method-c-getpwnam">::getpwnam</a>
<li ><a href="#method-c-getpwuid">::getpwuid</a>
<li ><a href="#method-c-group">::group</a>
<li ><a href="#method-c-nprocessors">::nprocessors</a>
<li ><a href="#method-c-passwd">::passwd</a>
<li ><a href="#method-c-setgrent">::setgrent</a>
<li ><a href="#method-c-setpwent">::setpwent</a>
<li ><a href="#method-c-sysconf">::sysconf</a>
<li ><a href="#method-c-sysconfdir">::sysconfdir</a>
<li ><a href="#method-c-systmpdir">::systmpdir</a>
<li ><a href="#method-c-uname">::uname</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="module-Etc">
<h1 id="module-Etc" class="module">
module Etc
</h1>
<section class="description">
<p>The <a href="Etc.html"><code>Etc</code></a> module provides access to information typically stored in files in the /etc directory on Unix systems.</p>
<p>The information accessible consists of the information found in the /etc/passwd and /etc/group files, plus information about the system's temporary directory (/tmp) and configuration directory (/etc).</p>
<p>The <a href="Etc.html"><code>Etc</code></a> module provides a more reliable way to access information about the logged in user than environment variables such as +$USER+.</p>
<h2 id="module-Etc-label-Example-3A">Example:<span><a href="#module-Etc-label-Example-3A">¶</a> <a href="#top">↑</a></span></h2>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'etc'</span>
<span class="ruby-identifier">login</span> = <span class="ruby-constant">Etc</span>.<span class="ruby-identifier">getlogin</span>
<span class="ruby-identifier">info</span> = <span class="ruby-constant">Etc</span>.<span class="ruby-identifier">getpwnam</span>(<span class="ruby-identifier">login</span>)
<span class="ruby-identifier">username</span> = <span class="ruby-identifier">info</span>.<span class="ruby-identifier">gecos</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp">/,/</span>).<span class="ruby-identifier">first</span>
<span class="ruby-identifier">puts</span> <span class="ruby-node">"Hello #{username}, I see your login name is #{login}"</span>
</pre>
<p>Note that the methods provided by this module are not always secure. It should be used for informational purposes, and not for security.</p>
<p>All operations defined in this module are class methods, so that you can include the <a href="Etc.html"><code>Etc</code></a> module into your class.</p>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="constants-list">
<header>
<h3>Constants</h3>
</header>
<dl>
<dt id="Group">Group
<dd><p><a href="Etc.html#Group"><code>Group</code></a></p>
<p><a href="Etc.html#Group"><code>Group</code></a> is a <a href="Struct.html"><code>Struct</code></a> that is only available when compiled with <code>HAVE_GETGRENT</code>.</p>
<p>The struct contains the following members:</p>
<dl class="rdoc-list note-list"><dt>name
<dd>
<p>contains the name of the group as a <a href="String.html"><code>String</code></a>.</p>
</dd><dt>passwd
<dd>
<p>contains the encrypted password as a <a href="String.html"><code>String</code></a>. An 'x' is returned if password access to the group is not available; an empty string is returned if no password is needed to obtain membership of the group.</p>
<p>Must be compiled with <code>HAVE_STRUCT_GROUP_GR_PASSWD</code>.</p>
</dd><dt>gid
<dd>
<p>contains the group's numeric ID as an integer.</p>
</dd><dt>mem
<dd>
<p>is an <a href="Array.html"><code>Array</code></a> of Strings containing the short login names of the members of the group.</p>
</dd></dl>
<dt id="Passwd">Passwd
<dd><p><a href="Etc.html#Passwd"><code>Passwd</code></a></p>
<p><a href="Etc.html#Passwd"><code>Passwd</code></a> is a <a href="Struct.html"><code>Struct</code></a> that contains the following members:</p>
<dl class="rdoc-list note-list"><dt>name
<dd>
<p>contains the short login name of the user as a <a href="String.html"><code>String</code></a>.</p>
</dd><dt>passwd
<dd>
<p>contains the encrypted password of the user as a <a href="String.html"><code>String</code></a>. an 'x' is returned if shadow passwords are in use. An '*' is returned if the user cannot log in using a password.</p>
</dd><dt>uid
<dd>
<p>contains the integer user ID (uid) of the user.</p>
</dd><dt>gid
<dd>
<p>contains the integer group ID (gid) of the user's primary group.</p>
</dd><dt>dir
<dd>
<p>contains the path to the home directory of the user as a <a href="String.html"><code>String</code></a>.</p>
</dd><dt>shell
<dd>
<p>contains the path to the login shell of the user as a <a href="String.html"><code>String</code></a>.</p>
</dd></dl>
<h3 id="label-The+following+members+below+are+optional-2C+and+must+be+compiled+with+special+flags-3A">The following members below are optional, and must be compiled with special flags:<span><a href="#label-The+following+members+below+are+optional-2C+and+must+be+compiled+with+special+flags-3A">¶</a> <a href="#top">↑</a></span></h3>
<dl class="rdoc-list note-list"><dt>gecos
<dd>
<p>contains a longer <a href="String.html"><code>String</code></a> description of the user, such as a full name. Some Unix systems provide structured information in the gecos field, but this is system-dependent. must be compiled with <code>HAVE_STRUCT_PASSWD_PW_GECOS</code></p>
</dd><dt>change
<dd>
<p>password change time(integer) must be compiled with <code>HAVE_STRUCT_PASSWD_PW_CHANGE</code></p>
</dd><dt>quota
<dd>
<p>quota value(integer) must be compiled with <code>HAVE_STRUCT_PASSWD_PW_QUOTA</code></p>
</dd><dt>age
<dd>
<p>password age(integer) must be compiled with <code>HAVE_STRUCT_PASSWD_PW_AGE</code></p>
</dd><dt>class
<dd>
<p>user access class(string) must be compiled with <code>HAVE_STRUCT_PASSWD_PW_CLASS</code></p>
</dd><dt>comment
<dd>
<p>comment(string) must be compiled with <code>HAVE_STRUCT_PASSWD_PW_COMMENT</code></p>
</dd><dt>expire
<dd>
<p>account expiration time(integer) must be compiled with <code>HAVE_STRUCT_PASSWD_PW_EXPIRE</code></p>
</dd></dl>
<dt id="VERSION">VERSION
<dd>
</dl>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-confstr" class="method-detail ">
<div class="method-heading">
<span class="method-name">confstr</span><span
class="method-args">(p1)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns system configuration variable using confstr().</p>
<p><em>name</em> should be a constant under <code>Etc</code> which begins with <code>CS_</code>.</p>
<p>The return value is a string or nil. nil means no configuration-defined value. (confstr() returns 0 but errno is not set.)</p>
<pre class="ruby"><span class="ruby-constant">Etc</span>.<span class="ruby-identifier">confstr</span>(<span class="ruby-constant">Etc</span><span class="ruby-operator">::</span><span class="ruby-constant">CS_PATH</span>) <span class="ruby-comment">#=> "/bin:/usr/bin"</span>
<span class="ruby-comment"># GNU/Linux</span>
<span class="ruby-constant">Etc</span>.<span class="ruby-identifier">confstr</span>(<span class="ruby-constant">Etc</span><span class="ruby-operator">::</span><span class="ruby-constant">CS_GNU_LIBC_VERSION</span>) <span class="ruby-comment">#=> "glibc 2.18"</span>
<span class="ruby-constant">Etc</span>.<span class="ruby-identifier">confstr</span>(<span class="ruby-constant">Etc</span><span class="ruby-operator">::</span><span class="ruby-constant">CS_GNU_LIBPTHREAD_VERSION</span>) <span class="ruby-comment">#=> "NPTL 2.18"</span>
</pre>
<div class="method-source-code" id="confstr-source">
<pre>static VALUE
etc_confstr(VALUE obj, VALUE arg)
{
int name;
char localbuf[128], *buf = localbuf;
size_t bufsize = sizeof(localbuf), ret;
VALUE tmp;
name = NUM2INT(arg);
errno = 0;
ret = confstr(name, buf, bufsize);
if (bufsize < ret) {
bufsize = ret;
buf = ALLOCV_N(char, tmp, bufsize);
errno = 0;
ret = confstr(name, buf, bufsize);
}
if (bufsize < ret)
rb_bug("required buffer size for confstr() changed dynamically.");
if (ret == 0) {
if (errno == 0) /* no configuration-defined value */
return Qnil;
rb_sys_fail("confstr");
}
return rb_str_new_cstr(buf);
}</pre>
</div>
</div>
</div>
<div id="method-c-endgrent" class="method-detail ">
<div class="method-heading">
<span class="method-name">endgrent</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Ends the process of scanning through the /etc/group file begun by <a href="Etc.html#method-c-getgrent"><code>::getgrent</code></a>, and closes the file.</p>
<div class="method-source-code" id="endgrent-source">
<pre>static VALUE
etc_endgrent(VALUE obj)
{
#ifdef HAVE_GETGRENT
endgrent();
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-endpwent" class="method-detail ">
<div class="method-heading">
<span class="method-name">endpwent</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Ends the process of scanning through the /etc/passwd file begun with <a href="Etc.html#method-c-getpwent"><code>::getpwent</code></a>, and closes the file.</p>
<div class="method-source-code" id="endpwent-source">
<pre>static VALUE
etc_endpwent(VALUE obj)
{
#ifdef HAVE_GETPWENT
endpwent();
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-getgrent" class="method-detail ">
<div class="method-heading">
<span class="method-name">getgrent</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an entry from the /etc/group file.</p>
<p>The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or <code>nil</code> if the end of the file has been reached.</p>
<p>To close the file when processing is complete, call <a href="Etc.html#method-c-endgrent"><code>::endgrent</code></a>.</p>
<p>Each entry is returned as a <a href="Etc.html#Group"><code>Group</code></a> struct</p>
<div class="method-source-code" id="getgrent-source">
<pre>static VALUE
etc_getgrent(VALUE obj)
{
#ifdef HAVE_GETGRENT
struct group *gr;
if ((gr = getgrent()) != 0) {
return setup_group(gr);
}
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-getgrgid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getgrgid(group_id) → Group
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns information about the group with specified integer <code>group_id</code>, as found in /etc/group.</p>
<p>The information is returned as a <a href="Etc.html#Group"><code>Group</code></a> struct.</p>
<p>See the unix manpage for <code>getgrgid(3)</code> for more detail.</p>
<h3 id="method-c-getgrgid-label-Example-3A">Example:<span><a href="#method-c-getgrgid-label-Example-3A">¶</a> <a href="#top">↑</a></span></h3>
<pre class="ruby"><span class="ruby-constant">Etc</span>.<span class="ruby-identifier">getgrgid</span>(<span class="ruby-value">100</span>)
<span class="ruby-comment">#=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]></span>
</pre>
<div class="method-source-code" id="getgrgid-source">
<pre>static VALUE
etc_getgrgid(int argc, VALUE *argv, VALUE obj)
{
#ifdef HAVE_GETGRENT
VALUE id;
gid_t gid;
struct group *grp;
if (rb_scan_args(argc, argv, "01", &id) == 1) {
gid = NUM2GIDT(id);
}
else {
gid = getgid();
}
grp = getgrgid(gid);
if (grp == 0) rb_raise(rb_eArgError, "can't find group for %d", (int)gid);
return setup_group(grp);
#else
return Qnil;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-getgrnam" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getgrnam(name) → Group
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns information about the group with specified <code>name</code>, as found in /etc/group.</p>
<p>The information is returned as a <a href="Etc.html#Group"><code>Group</code></a> struct.</p>
<p>See the unix manpage for <code>getgrnam(3)</code> for more detail.</p>
<h3 id="method-c-getgrnam-label-Example-3A">Example:<span><a href="#method-c-getgrnam-label-Example-3A">¶</a> <a href="#top">↑</a></span></h3>
<pre class="ruby"><span class="ruby-constant">Etc</span>.<span class="ruby-identifier">getgrnam</span>(<span class="ruby-string">'users'</span>)
<span class="ruby-comment">#=> #<struct Etc::Group name="users", passwd="x", gid=100, mem=["meta", "root"]></span>
</pre>
<div class="method-source-code" id="getgrnam-source">
<pre>static VALUE
etc_getgrnam(VALUE obj, VALUE nam)
{
#ifdef HAVE_GETGRENT
struct group *grp;
const char *p = StringValueCStr(nam);
grp = getgrnam(p);
if (grp == 0) rb_raise(rb_eArgError, "can't find group for %"PRIsVALUE, nam);
return setup_group(grp);
#else
return Qnil;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-getlogin" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getlogin → String
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the short user name of the currently logged in user. Unfortunately, it is often rather easy to fool <a href="Etc.html#method-c-getlogin"><code>::getlogin</code></a>.</p>
<p>Avoid <a href="Etc.html#method-c-getlogin"><code>::getlogin</code></a> for security-related purposes.</p>
<p>If <a href="Etc.html#method-c-getlogin"><code>::getlogin</code></a> fails, try <a href="Etc.html#method-c-getpwuid"><code>::getpwuid</code></a>.</p>
<p>See the unix manpage for <code>getpwuid(3)</code> for more detail.</p>
<p>e.g.</p>
<pre>Etc.getlogin -> 'guest'</pre>
<div class="method-source-code" id="getlogin-source">
<pre>static VALUE
etc_getlogin(VALUE obj)
{
char *login;
#ifdef HAVE_GETLOGIN
login = getlogin();
if (!login) login = getenv("USER");
#else
login = getenv("USER");
#endif
if (login) {
#ifdef _WIN32
rb_encoding *extenc = rb_utf8_encoding();
#else
rb_encoding *extenc = rb_locale_encoding();
#endif
return rb_external_str_new_with_enc(login, strlen(login), extenc);
}
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-getpwent" class="method-detail ">
<div class="method-heading">
<span class="method-name">getpwent</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an entry from the /etc/passwd file.</p>
<p>The first time it is called it opens the file and returns the first entry; each successive call returns the next entry, or <code>nil</code> if the end of the file has been reached.</p>
<p>To close the file when processing is complete, call <a href="Etc.html#method-c-endpwent"><code>::endpwent</code></a>.</p>
<p>Each entry is returned as a <a href="Etc.html#Passwd"><code>Passwd</code></a> struct.</p>
<div class="method-source-code" id="getpwent-source">
<pre>static VALUE
etc_getpwent(VALUE obj)
{
#ifdef HAVE_GETPWENT
struct passwd *pw;
if ((pw = getpwent()) != 0) {
return setup_passwd(pw);
}
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-getpwnam" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getpwnam(name) → Passwd
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the /etc/passwd information for the user with specified login <code>name</code>.</p>
<p>The information is returned as a <a href="Etc.html#Passwd"><code>Passwd</code></a> struct.</p>
<p>See the unix manpage for <code>getpwnam(3)</code> for more detail.</p>
<h3 id="method-c-getpwnam-label-Example-3A">Example:<span><a href="#method-c-getpwnam-label-Example-3A">¶</a> <a href="#top">↑</a></span></h3>
<pre class="ruby"><span class="ruby-constant">Etc</span>.<span class="ruby-identifier">getpwnam</span>(<span class="ruby-string">'root'</span>)
<span class="ruby-comment">#=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash"></span>
</pre>
<div class="method-source-code" id="getpwnam-source">
<pre>static VALUE
etc_getpwnam(VALUE obj, VALUE nam)
{
#ifdef HAVE_GETPWENT
struct passwd *pwd;
const char *p = StringValueCStr(nam);
pwd = getpwnam(p);
if (pwd == 0) rb_raise(rb_eArgError, "can't find user for %"PRIsVALUE, nam);
return setup_passwd(pwd);
#else
return Qnil;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-getpwuid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getpwuid(uid) → Passwd
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the /etc/passwd information for the user with the given integer <code>uid</code>.</p>
<p>The information is returned as a <a href="Etc.html#Passwd"><code>Passwd</code></a> struct.</p>
<p>If <code>uid</code> is omitted, the value from <code>Passwd[:uid]</code> is returned instead.</p>
<p>See the unix manpage for <code>getpwuid(3)</code> for more detail.</p>
<h3 id="method-c-getpwuid-label-Example-3A">Example:<span><a href="#method-c-getpwuid-label-Example-3A">¶</a> <a href="#top">↑</a></span></h3>
<pre class="ruby"><span class="ruby-constant">Etc</span>.<span class="ruby-identifier">getpwuid</span>(<span class="ruby-value">0</span>)
<span class="ruby-comment">#=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash"></span>
</pre>
<div class="method-source-code" id="getpwuid-source">
<pre>static VALUE
etc_getpwuid(int argc, VALUE *argv, VALUE obj)
{
#if defined(HAVE_GETPWENT)
VALUE id;
rb_uid_t uid;
struct passwd *pwd;
if (rb_scan_args(argc, argv, "01", &id) == 1) {
uid = NUM2UIDT(id);
}
else {
uid = getuid();
}
pwd = getpwuid(uid);
if (pwd == 0) rb_raise(rb_eArgError, "can't find user for %d", (int)uid);
return setup_passwd(pwd);
#else
return Qnil;
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-group" class="method-detail ">
<div class="method-heading">
<span class="method-name">group</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Provides a convenient Ruby iterator which executes a block for each entry in the /etc/group file.</p>
<p>The code block is passed an <a href="Etc.html#Group"><code>Group</code></a> struct.</p>
<p>See <a href="Etc.html#method-c-getgrent"><code>::getgrent</code></a> above for details.</p>
<p>Example:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'etc'</span>
<span class="ruby-constant">Etc</span>.<span class="ruby-identifier">group</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">g</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">g</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">+</span> <span class="ruby-string">": "</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">g</span>.<span class="ruby-identifier">mem</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">', '</span>)
}
</pre>
<div class="method-source-code" id="group-source">
<pre>static VALUE
etc_group(VALUE obj)
{
#ifdef HAVE_GETGRENT
struct group *grp;
if (rb_block_given_p()) {
each_group();
}
else if ((grp = getgrent()) != 0) {
return setup_group(grp);
}
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-nprocessors" class="method-detail ">
<div class="method-heading">
<span class="method-name">nprocessors</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the number of online processors.</p>
<p>The result is intended as the number of processes to use all available processors.</p>
<p>This method is implemented using:</p>
<ul><li>
<p>sched_getaffinity(): Linux</p>
</li><li>
<p>sysconf(_SC_NPROCESSORS_ONLN): GNU/Linux, NetBSD, FreeBSD, OpenBSD, DragonFly BSD, OpenIndiana, Mac OS X, AIX</p>
</li></ul>
<p>Example:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'etc'</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">Etc</span>.<span class="ruby-identifier">nprocessors</span> <span class="ruby-comment">#=> 4</span>
</pre>
<p>The result might be smaller number than physical cpus especially when ruby process is bound to specific cpus. This is intended for getting better parallel processing.</p>
<p>Example: (Linux)</p>
<pre>linux$ taskset 0x3 ./ruby -retc -e "p Etc.nprocessors" #=> 2</pre>
<div class="method-source-code" id="nprocessors-source">
<pre>static VALUE
etc_nprocessors(VALUE obj)
{
long ret;
#if !defined(_WIN32)
#if defined(HAVE_SCHED_GETAFFINITY) && defined(CPU_ALLOC)
int ncpus;
ncpus = etc_nprocessors_affin();
if (ncpus != -1) {
return INT2NUM(ncpus);
}
/* fallback to _SC_NPROCESSORS_ONLN */
#endif
errno = 0;
ret = sysconf(_SC_NPROCESSORS_ONLN);
if (ret == -1) {
rb_sys_fail("sysconf(_SC_NPROCESSORS_ONLN)");
}
#else
SYSTEM_INFO si;
GetSystemInfo(&si);
ret = (long)si.dwNumberOfProcessors;
#endif
return LONG2NUM(ret);
}</pre>
</div>
</div>
</div>
<div id="method-c-passwd" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
passwd { |struct| block } → Passwd
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
passwd → Passwd
</span>
</div>
<div class="method-description">
<p>Provides a convenient Ruby iterator which executes a block for each entry in the /etc/passwd file.</p>
<p>The code block is passed an <a href="Etc.html#Passwd"><code>Passwd</code></a> struct.</p>
<p>See <a href="Etc.html#method-c-getpwent"><code>::getpwent</code></a> above for details.</p>
<p>Example:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'etc'</span>
<span class="ruby-constant">Etc</span>.<span class="ruby-identifier">passwd</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">u</span><span class="ruby-operator">|</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">u</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">+</span> <span class="ruby-string">" = "</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">u</span>.<span class="ruby-identifier">gecos</span>
}
</pre>
<div class="method-source-code" id="passwd-source">
<pre>static VALUE
etc_passwd(VALUE obj)
{
#ifdef HAVE_GETPWENT
struct passwd *pw;
if (rb_block_given_p()) {
each_passwd();
}
else if ((pw = getpwent()) != 0) {
return setup_passwd(pw);
}
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-setgrent" class="method-detail ">
<div class="method-heading">
<span class="method-name">setgrent</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Resets the process of reading the /etc/group file, so that the next call to <a href="Etc.html#method-c-getgrent"><code>::getgrent</code></a> will return the first entry again.</p>
<div class="method-source-code" id="setgrent-source">
<pre>static VALUE
etc_setgrent(VALUE obj)
{
#ifdef HAVE_GETGRENT
setgrent();
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-setpwent" class="method-detail ">
<div class="method-heading">
<span class="method-name">setpwent</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Resets the process of reading the /etc/passwd file, so that the next call to <a href="Etc.html#method-c-getpwent"><code>::getpwent</code></a> will return the first entry again.</p>
<div class="method-source-code" id="setpwent-source">
<pre>static VALUE
etc_setpwent(VALUE obj)
{
#ifdef HAVE_GETPWENT
setpwent();
#endif
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-c-sysconf" class="method-detail ">
<div class="method-heading">
<span class="method-name">sysconf</span><span
class="method-args">(p1)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns system configuration variable using sysconf().</p>
<p><em>name</em> should be a constant under <code>Etc</code> which begins with <code>SC_</code>.</p>
<p>The return value is an integer or nil. nil means indefinite limit. (sysconf() returns -1 but errno is not set.)</p>
<pre class="ruby"><span class="ruby-constant">Etc</span>.<span class="ruby-identifier">sysconf</span>(<span class="ruby-constant">Etc</span><span class="ruby-operator">::</span><span class="ruby-constant">SC_ARG_MAX</span>) <span class="ruby-comment">#=> 2097152</span>
<span class="ruby-constant">Etc</span>.<span class="ruby-identifier">sysconf</span>(<span class="ruby-constant">Etc</span><span class="ruby-operator">::</span><span class="ruby-constant">SC_LOGIN_NAME_MAX</span>) <span class="ruby-comment">#=> 256</span>
</pre>
<div class="method-source-code" id="sysconf-source">
<pre>static VALUE
etc_sysconf(VALUE obj, VALUE arg)
{
int name;
long ret;
name = NUM2INT(arg);
errno = 0;
ret = sysconf(name);
if (ret == -1) {
if (errno == 0) /* no limit */
return Qnil;
rb_sys_fail("sysconf");
}
return LONG2NUM(ret);
}</pre>
</div>
</div>
</div>
<div id="method-c-sysconfdir" class="method-detail ">
<div class="method-heading">
<span class="method-name">sysconfdir</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns system configuration directory.</p>
<p>This is typically “/etc”, but is modified by the prefix used when Ruby was compiled. For example, if Ruby is built and installed in /usr/local, returns “/usr/local/etc” on other platforms than Windows. On Windows, this always returns the directory provided by the system.</p>
<div class="method-source-code" id="sysconfdir-source">
<pre>static VALUE
etc_sysconfdir(VALUE obj)
{
#ifdef _WIN32
return rb_w32_special_folder(CSIDL_COMMON_APPDATA);
#else
return rb_filesystem_str_new_cstr(SYSCONFDIR);
#endif
}</pre>
</div>
</div>
</div>
<div id="method-c-systmpdir" class="method-detail ">
<div class="method-heading">
<span class="method-name">systmpdir</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns system temporary directory; typically “/tmp”.</p>
<div class="method-source-code" id="systmpdir-source">
<pre>static VALUE
etc_systmpdir(VALUE _)
{
VALUE tmpdir;
#ifdef _WIN32
WCHAR path[_MAX_PATH];
UINT len = rb_w32_system_tmpdir(path, numberof(path));
if (!len) return Qnil;
tmpdir = rb_w32_conv_from_wchar(path, rb_filesystem_encoding());
#else
const char default_tmp[] = "/tmp";
const char *tmpstr = default_tmp;
size_t tmplen = strlen(default_tmp);
# if defined _CS_DARWIN_USER_TEMP_DIR
#ifndef MAXPATHLEN
#define MAXPATHLEN 1024
#endif
char path[MAXPATHLEN];
size_t len;
len = confstr(_CS_DARWIN_USER_TEMP_DIR, path, sizeof(path));
if (len > 0) {
tmpstr = path;
tmplen = len - 1;
if (len > sizeof(path)) tmpstr = 0;
}
# endif
tmpdir = rb_filesystem_str_new(tmpstr, tmplen);
# if defined _CS_DARWIN_USER_TEMP_DIR
if (!tmpstr) {
confstr(_CS_DARWIN_USER_TEMP_DIR, RSTRING_PTR(tmpdir), len);
}
# endif
#endif
#ifndef RB_PASS_KEYWORDS
/* untaint on Ruby < 2.7 */
FL_UNSET(tmpdir, FL_TAINT);
#endif
return tmpdir;
}</pre>
</div>
</div>
</div>
<div id="method-c-uname" class="method-detail ">
<div class="method-heading">
<span class="method-name">uname</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the system information obtained by uname system call.</p>
<p>The return value is a hash which has 5 keys at least:</p>
<pre>:sysname, :nodename, :release, :version, :machine</pre>
<p>Example:</p>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'etc'</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">'pp'</span>
<span class="ruby-identifier">pp</span> <span class="ruby-constant">Etc</span>.<span class="ruby-identifier">uname</span>
<span class="ruby-comment">#=> {:sysname=>"Linux",</span>
<span class="ruby-comment"># :nodename=>"boron",</span>
<span class="ruby-comment"># :release=>"2.6.18-6-xen-686",</span>
<span class="ruby-comment"># :version=>"#1 SMP Thu Nov 5 19:54:42 UTC 2009",</span>
<span class="ruby-comment"># :machine=>"i686"}</span>
</pre>
<div class="method-source-code" id="uname-source">
<pre>static VALUE
etc_uname(VALUE obj)
{
#ifdef _WIN32
OSVERSIONINFOW v;
SYSTEM_INFO s;
const char *sysname, *mach;
VALUE result, release, version;
VALUE vbuf, nodename = Qnil;
DWORD len = 0;
WCHAR *buf;
v.dwOSVersionInfoSize = sizeof(v);
if (!GetVersionExW(&v))
rb_sys_fail("GetVersionEx");
result = rb_hash_new();
switch (v.dwPlatformId) {
case VER_PLATFORM_WIN32s:
sysname = "Win32s";
break;
case VER_PLATFORM_WIN32_NT:
sysname = "Windows_NT";
break;
case VER_PLATFORM_WIN32_WINDOWS:
default:
sysname = "Windows";
break;
}
rb_hash_aset(result, ID2SYM(rb_intern("sysname")), rb_str_new_cstr(sysname));
release = rb_sprintf("%lu.%lu.%lu", v.dwMajorVersion, v.dwMinorVersion, v.dwBuildNumber);
rb_hash_aset(result, ID2SYM(rb_intern("release")), release);
version = rb_sprintf("%s Version %"PRIsVALUE": %"PRIsVALUE, sysname, release,
rb_w32_conv_from_wchar(v.szCSDVersion, rb_utf8_encoding()));
rb_hash_aset(result, ID2SYM(rb_intern("version")), version);
# if defined _MSC_VER && _MSC_VER < 1300
# define GET_COMPUTER_NAME(ptr, plen) GetComputerNameW(ptr, plen)
# else
# define GET_COMPUTER_NAME(ptr, plen) GetComputerNameExW(ComputerNameDnsFullyQualified, ptr, plen)
# endif
GET_COMPUTER_NAME(NULL, &len);
buf = ALLOCV_N(WCHAR, vbuf, len);
if (GET_COMPUTER_NAME(buf, &len)) {
nodename = rb_w32_conv_from_wchar(buf, rb_utf8_encoding());
}
ALLOCV_END(vbuf);
if (NIL_P(nodename)) nodename = rb_str_new(0, 0);
rb_hash_aset(result, ID2SYM(rb_intern("nodename")), nodename);
# ifndef PROCESSOR_ARCHITECTURE_AMD64
# define PROCESSOR_ARCHITECTURE_AMD64 9
# endif
# ifndef PROCESSOR_ARCHITECTURE_INTEL
# define PROCESSOR_ARCHITECTURE_INTEL 0
# endif
GetSystemInfo(&s);
switch (s.wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_AMD64:
mach = "x64";
break;
case PROCESSOR_ARCHITECTURE_ARM:
mach = "ARM";
break;
case PROCESSOR_ARCHITECTURE_INTEL:
mach = "x86";
break;
default:
mach = "unknown";
break;
}
rb_hash_aset(result, ID2SYM(rb_intern("machine")), rb_str_new_cstr(mach));
#else
struct utsname u;
int ret;
VALUE result;
ret = uname(&u);
if (ret == -1)
rb_sys_fail("uname");
result = rb_hash_new();
rb_hash_aset(result, ID2SYM(rb_intern("sysname")), rb_str_new_cstr(u.sysname));
rb_hash_aset(result, ID2SYM(rb_intern("nodename")), rb_str_new_cstr(u.nodename));
rb_hash_aset(result, ID2SYM(rb_intern("release")), rb_str_new_cstr(u.release));
rb_hash_aset(result, ID2SYM(rb_intern("version")), rb_str_new_cstr(u.version));
rb_hash_aset(result, ID2SYM(rb_intern("machine")), rb_str_new_cstr(u.machine));
#endif
return result;
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>