Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add the implementation of Etc::Passwd#each and Etc::Group#each

  • Loading branch information...
commit 7da3aa4c3eed152f4e8234c403a8801f28c89b36 1 parent aff4435
@Watson1978 Watson1978 authored
Showing with 84 additions and 12 deletions.
  1. +84 −12 ext/etc/etc.c
View
96 ext/etc/etc.c
@@ -183,6 +183,16 @@ passwd_iterate(void)
endpwent();
return Qnil;
}
+
+static void
+each_passwd(void)
+{
+ if (passwd_blocking) {
+ rb_raise(rb_eRuntimeError, "parallel passwd iteration");
+ }
+ passwd_blocking = (int)Qtrue;
+ rb_ensure(passwd_iterate, 0, passwd_ensure, 0);
+}
#endif
/* Provides a convenient Ruby iterator which executes a block for each entry
@@ -208,19 +218,43 @@ etc_passwd(VALUE obj, SEL sel)
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()) != NULL) {
+ else if ((pw = getpwent()) != NULL) {
return setup_passwd(pw);
}
#endif
return Qnil;
}
+/* 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::Passwd.each {|u|
+ * puts u.name + " = " + u.gecos
+ * }
+ *
+ * Etc::Passwd.collect {|u| u.gecos}
+ * Etc::Passwd.collect {|u| u.gecos}
+ *
+ */
+static VALUE
+etc_each_passwd(VALUE obj, SEL sel)
+{
+#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.
*/
@@ -387,6 +421,16 @@ group_iterate(void)
endgrent();
return Qnil;
}
+
+static void
+each_group(void)
+{
+ if (group_blocking) {
+ rb_raise(rb_eRuntimeError, "parallel group iteration");
+ }
+ group_blocking = (int)Qtrue;
+ rb_ensure(group_iterate, 0, group_ensure, 0);
+}
#endif
/* Provides a convenient Ruby iterator which executes a block for each entry
@@ -412,19 +456,43 @@ etc_group(VALUE obj, SEL sel)
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()) != NULL) {
+ else if ((grp = getgrent()) != NULL) {
return setup_group(grp);
}
#endif
return Qnil;
}
+#ifdef HAVE_GETGRENT
+/* 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::Group.each {|g|
+ * puts g.name + ": " + g.mem.join(', ')
+ * }
+ *
+ * Etc::Group.collect {|g| g.name}
+ * Etc::Group.select {|g| !g.mem.empty?}
+ *
+ */
+static VALUE
+etc_each_group(VALUE obj, SEL sel)
+{
+ RETURN_ENUMERATOR(obj, 0, 0);
+ each_group();
+ return obj;
+}
+#endif
+
/* Resets the process of reading the /etc/group file, so that the next call
* to getgrent will return the first entry again.
*/
@@ -536,6 +604,8 @@ Init_etc(void)
#endif
NULL);
rb_define_const(mEtc, "Passwd", sPasswd);
+ rb_extend_object(sPasswd, rb_mEnumerable);
+ rb_objc_define_method(*(VALUE *)sPasswd, "each", etc_each_passwd, 0);
#ifdef HAVE_GETGRENT
sGroup = rb_struct_define("Group", "name",
@@ -545,5 +615,7 @@ Init_etc(void)
"gid", "mem", NULL);
rb_define_const(mEtc, "Group", sGroup);
+ rb_extend_object(sGroup, rb_mEnumerable);
+ rb_objc_define_method(*(VALUE *)sGroup, "each", etc_each_group, 0);
#endif
}
Please sign in to comment.
Something went wrong with that request. Please try again.