# \[Feature:1.9\] Etc.each\_passwd, each\_group

**URL:** <https://www.ruby-forum.com/t/feature-1-9-etc-each-passwd-each-group/160261>\
**Category:** Development\
**Created:** [February 13, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/feature-1-9-etc-each-passwd-each-group/160261 "2009-02-13T14:38:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [February 13, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/feature-1-9-etc-each-passwd-each-group/160261/1 "2009-02-13T14:38:05Z")

</div>

e$B$J$+$@$G$9!#e(B

Etc.each\_passwde$B$de(BEtc.each\_groupe$B$H$$$&$b$N$r;W$$$D$-$^$7$?!#e(B  
e$B$^$?!"JLL\>$H$7$Fe(BEtc::Passwd.eache$B$de(BEtc.::Group.eache$B$b!#e(B

# e$B%Q%9%o!\<%I$r@\_Dj$7$F$$$J$$%f!\<%6L\>$N%j%9%He(B

Etc::Passwd.find\_all {|u| u.passwd == “”}.map(&:name)

e$B$H$+!#e(B

# Index: ext/etc/etc.c

— ext/etc/etc.c (revision 22104)  
+++ ext/etc/etc.c (working copy)  
@@ -187,4 +187,14 @@ passwd\_iterate(void)  
return Qnil;  
}  
+  
+static void  
+each\_passwd(void)  
+{

- if (passwd\_blocking) {
- rb\_raise(rb\_eRuntimeError, “parallel passwd iteration”);
- }
- passwd\_blocking = Qtrue;
- rb\_ensure(passwd\_iterate, 0, passwd\_ensure, 0);  
+}  
#endif

@@ -212,11 +222,7 @@ etc\_passwd(VALUE obj)  
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);

- each\_passwd();  
}

- if (pw = getpwent()) {

- else if (pw = getpwent()) {  
return setup\_passwd(pw);  
}  
@@ -225,4 +231,32 @@ etc\_passwd(VALUE obj)  
}

+/\* Iterates for each entry in the /etc/passwd file if a block is given.

- 
  - If no block is given, returns the enumerator.

- 
  - 

- 
  - The code block is passed an Struct::Passwd struct; see getpwent  
above for

- 
  - details.

- 
  - 

- 
  - Example:

- 
  - 

- 
  - 

```
require 'etc'

```

- 
  - 

- 
  - 

```
Etc.each_passwd {|u|

```

- 
  - 

```
  puts u.name + " = " + u.gecos

```

- 
  - 

```
}

```

- 
  - 

- 
  - 

```
Etc.each_passwd.collect {|u| u.gecos}

```

- 
  - 

```
Etc::Passwd.collect {|u| u.gecos}

```

- 
  - 

- \*/  
+static VALUE  
+etc\_each\_passwd(VALUE obj)  
+{  
+#ifdef HAVE\_GETPWENT
- RETURN\_ENUMERATOR(obj, 0, 0);
- each\_passwd();  
+#endif
- return obj;  
+}
- 

/\* Resets the process of reading the /etc/passwd file, so that the next  
call

- to getpwent will return the first entry again.  
@@ -391,4 +425,14 @@ group\_iterate(void)  
return Qnil;  
}

- 

+static void  
+each\_group(void)  
+{

- if (group\_blocking) {
- rb\_raise(rb\_eRuntimeError, “parallel group iteration”);
- }
- group\_blocking = Qtrue;
- rb\_ensure(group\_iterate, 0, group\_ensure, 0);  
+}  
#endif

@@ -416,11 +460,7 @@ etc\_group(VALUE obj)  
rb\_secure(4);  
if (rb\_block\_given\_p()) {

- if (group\_blocking) {
- 

```
 rb_raise(rb_eRuntimeError, "parallel group iteration");

```

- }
- group\_blocking = Qtrue;
- rb\_ensure(group\_iterate, 0, group\_ensure, 0);

- each\_group();  
}

- if (grp = getgrent()) {

- else if (grp = getgrent()) {  
return setup\_group(grp);  
}  
@@ -429,4 +469,32 @@ etc\_group(VALUE obj)  
}

+/\* Iterates for each entry in the /etc/group file if a block is given.

- 
  - If no block is given, returns the enumerator.

- 
  - 

- 
  - The code block is passed an Struct::Group struct; see getpwent above  
for

- 
  - details.

- 
  - 

- 
  - Example:

- 
  - 

- 
  - 

```
require 'etc'

```

- 
  - 

- 
  - 

```
Etc.each_group {|g|

```

- 
  - 

```
  puts g.name + ": " + g.mem.join(', ')

```

- 
  - 

```
}

```

- 
  - 

- 
  - 

```
Etc.each_group.collect {|g| g.name}

```

- 
  - 

```
Etc::Group.select {|g| !g.mem.empty?}

```

- 
  - 

- \*/  
+static VALUE  
+etc\_each\_group(VALUE obj)  
+{  
+#ifdef HAVE\_GETPWENT
- RETURN\_ENUMERATOR(obj, 0, 0);
- each\_group();  
+#endif
- return obj;  
+}
- 

/\* Resets the process of reading the /etc/group file, so that the next  
call

- to getgrent will return the first entry again.  
@@ -505,8 +573,10 @@ Init\_etc(void)  
rb\_define\_module\_function(mEtc, “getpwent”, etc\_getpwent, 0);  
rb\_define\_module\_function(mEtc, “passwd”, etc\_passwd, 0);

- rb\_define\_module\_function(mEtc, “each\_passwd”, etc\_each\_passwd, 0);

- rb\_define\_module\_function(mEtc, “each\_group”, etc\_each\_group, 0);  
rb\_define\_module\_function(mEtc, “setgrent”, etc\_setgrent, 0);  
rb\_define\_module\_function(mEtc, “endgrent”, etc\_endgrent, 0);  
@@ -539,4 +609,6 @@ Init\_etc(void)  
NULL);  
rb\_define\_const(mEtc, “Passwd”, sPasswd);

- rb\_extend\_object(sPasswd, rb\_mEnumerable);

- rb\_define\_singleton\_method(sPasswd, “each”, etc\_each\_passwd, 0);

#ifdef HAVE\_GETGRENT  
@@ -548,4 +620,6 @@ Init\_etc(void)

```
 rb_define_const(mEtc, "Group", sGroup);

```

- rb\_extend\_object(sGroup, rb\_mEnumerable);
- rb\_define\_singleton\_method(sGroup, “each”, etc\_each\_group, 0);  
#endif  
}

---

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [February 17, 2009, 2:47pm UTC](https://www.ruby-forum.com/t/feature-1-9-etc-each-passwd-each-group/160261/2 "2009-02-17T14:47:09Z")

</div>

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:37999] [Feature:1.9] Etc.each\_passwd,  
each\_group”  
on Fri, 13 Feb 2009 22:30:01 +0900, Nobuyoshi N.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|Etc.each\_passwde$B$de(BEtc.each\_groupe$B$H$$$&$b$N$r;W$$$D$-$^$7$?!#e(B

e$B8\_49@-$r5$$K$7$J$1$l$P!"e(BEtc.passwde$B$,e(Beach\_passwde$B$H$7$FF0:n$9e(B  
e$B$Y$-$G$O$J$$$+$H46$8$^$7$?!#e(B

|e$B$^$?!"JLL\>$H$7$Fe(BEtc::Passwd.eache$B$de(BEtc.::Group.eache$B$b!#e(B  
|  
| # e$B%Q%9%o!\<%I$r@\_Dj$7$F$$$J$$%f!\<%6L\>$N%j%9%He(B  
| Etc::Passwd.find\_all {|u| u.passwd == “”}.map(&:name)  
|  
|e$B$H$+!#e(B

e$B$3$C$A$O;?@.$7$^$9!#e(B

---

<div class="post-metadata">

**Author:** ![nobu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e9c0ed/32.png) [@nobu](https://www.ruby-forum.com/u/nobu)\
**Post date:** [February 17, 2009, 4:58pm UTC](https://www.ruby-forum.com/t/feature-1-9-etc-each-passwd-each-group/160261/3 "2009-02-17T16:58:02Z")

</div>

e$B%A%1%C%He(B #1155 e$B$,99?7$5$l$^$7$?!#e(B (by Nobuyoshi N.)

e$B%9%F!\<%?%9e(B Opene$B$+$ie(BClosede$B$KJQ99e(B  
e$B?JD=e(B % 0e$B$+$ie(B100e$B$KJQ99e(B

## Applied in changeset r22377.

[http://redmine.ruby-lang.org/issues/show/1155](http://redmine.ruby-lang.org/issues/show/1155)
