static VALUE passwd_iterate(void) { struct passwd *pw; setpwent(); while (pw = getpwent()) { rb_yield(setup_passwd(pw)); } return Qnil; }
/* Returns an entry from the /etc/passwd file. The first time it is called it * opens the file and returns the first entry; each successive call returns * the next entry, or nil if the end of the file has been reached. * * To close the file when processing is complete, call endpwent. * * Each entry is returned as a Struct::Passwd: * * - Passwd#name contains the short login name of the user as a String. * * - Passwd#passwd contains the encrypted password of the user as a String. * an 'x' is returned if shadow passwords are in use. An '*' is returned * if the user cannot log in using a password. * * - Passwd#uid contains the integer user ID (uid) of the user. * * - Passwd#gid contains the integer group ID (gid) of the user's primary group. * * - Passwd#gecos contains a longer String description of the user, such as * a full name. Some Unix systems provide structured information in the * gecos field, but this is system-dependent. * * - Passwd#dir contains the path to the home directory of the user as a String. * * - Passwd#shell contains the path to the login shell of the user as a String. */ static VALUE etc_getpwent(VALUE obj) { #ifdef HAVE_GETPWENT struct passwd *pw; if (pw = getpwent()) { return setup_passwd(pw); } #endif return Qnil; }
/* Returns the /etc/passwd information for the user with specified login name. * * The information is returned as a Struct::Passwd; see getpwent above for * details. * * e.g. * Etc.getpwnam('root') -> #<struct Struct::Passwd name="root", * passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash"> */ static VALUE etc_getpwnam(VALUE obj, VALUE nam) { #ifdef HAVE_GETPWENT struct passwd *pwd; SafeStringValue(nam); pwd = getpwnam(RSTRING_PTR(nam)); if (pwd == 0) rb_raise(rb_eArgError, "can't find user for %s", RSTRING_PTR(nam)); return setup_passwd(pwd); #else return Qnil; #endif }
/* call-seq: * Etc.passwd { |struct| block } -> Passwd * Etc.passwd -> Passwd * * Provides a convenient Ruby iterator which executes a block for each entry * in the /etc/passwd file. * * The code block is passed an Passwd struct. * * See ::getpwent above for details. * * Example: * * require 'etc' * * Etc.passwd {|u| * puts u.name + " = " + u.gecos * } * */ static VALUE etc_passwd(VALUE obj) { #ifdef HAVE_GETPWENT struct passwd *pw; if (rb_block_given_p()) { each_passwd(); } else if (pw = getpwent()) { return setup_passwd(pw); } #endif return Qnil; }
/* call-seq: * getpwnam(name) -> Passwd * * Returns the /etc/passwd information for the user with specified login * +name+. * * The information is returned as a Passwd struct. * * See the unix manpage for <code>getpwnam(3)</code> for more detail. * * === Example: * * Etc.getpwnam('root') * #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash"> */ static VALUE etc_getpwnam(VALUE obj, VALUE nam) { #ifdef HAVE_GETPWENT struct passwd *pwd; const char *p = StringValueCStr(nam); rb_check_safe_obj(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 }
/* Provides a convenient Ruby iterator which executes a block for each entry * in the /etc/passwd file. * * The code block is passed an Etc::Passwd struct; see getpwent above for * details. * * Example: * * require 'etc' * * Etc.passwd {|u| * puts u.name + " = " + u.gecos * } * */ static VALUE etc_passwd(VALUE obj) { #ifdef HAVE_GETPWENT struct passwd *pw; rb_secure(4); if (rb_block_given_p()) { if (passwd_blocking) { rb_raise(rb_eRuntimeError, "parallel passwd iteration"); } passwd_blocking = Qtrue; rb_ensure(passwd_iterate, 0, passwd_ensure, 0); } if (pw = getpwent()) { return setup_passwd(pw); } #endif return Qnil; }
/* call-seq: * getpwuid(uid) -> Passwd * * Returns the /etc/passwd information for the user with the given integer +uid+. * * The information is returned as a Passwd struct. * * If +uid+ is omitted, the value from <code>Passwd[:uid]</code> is returned * instead. * * See the unix manpage for <code>getpwuid(3)</code> for more detail. * * === Example: * * Etc.getpwuid(0) * #=> #<struct Etc::Passwd name="root", passwd="x", uid=0, gid=0, gecos="root",dir="/root", shell="/bin/bash"> */ 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 }