Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.8 KiB

  1. package User::grent;
  2. use strict;
  3. BEGIN {
  4. use Exporter ();
  5. use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  6. @EXPORT = qw(getgrent getgrgid getgrnam getgr);
  7. @EXPORT_OK = qw($gr_name $gr_gid $gr_passwd $gr_mem @gr_members);
  8. %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  9. }
  10. use vars @EXPORT_OK;
  11. # Class::Struct forbids use of @ISA
  12. sub import { goto &Exporter::import }
  13. use Class::Struct qw(struct);
  14. struct 'User::grent' => [
  15. name => '$',
  16. passwd => '$',
  17. gid => '$',
  18. members => '@',
  19. ];
  20. sub populate (@) {
  21. return unless @_;
  22. my $gob = new();
  23. ($gr_name, $gr_passwd, $gr_gid) = @$gob[0,1,2] = @_[0,1,2];
  24. @gr_members = @{$gob->[3]} = split ' ', $_[3];
  25. return $gob;
  26. }
  27. sub getgrent ( ) { populate(CORE::getgrent()) }
  28. sub getgrnam ($) { populate(CORE::getgrnam(shift)) }
  29. sub getgrgid ($) { populate(CORE::getgrgid(shift)) }
  30. sub getgr ($) { ($_[0] =~ /^\d+/) ? &getgrgid : &getgrnam }
  31. 1;
  32. __END__
  33. =head1 NAME
  34. User::grent - by-name interface to Perl's built-in getgr*() functions
  35. =head1 SYNOPSIS
  36. use User::grent;
  37. $gr = getgrgid(0) or die "No group zero";
  38. if ( $gr->name eq 'wheel' && @{$gr->members} > 1 ) {
  39. print "gid zero name wheel, with other members";
  40. }
  41. use User::grent qw(:FIELDS;
  42. getgrgid(0) or die "No group zero";
  43. if ( $gr_name eq 'wheel' && @gr_members > 1 ) {
  44. print "gid zero name wheel, with other members";
  45. }
  46. $gr = getgr($whoever);
  47. =head1 DESCRIPTION
  48. This module's default exports override the core getgrent(), getgruid(),
  49. and getgrnam() functions, replacing them with versions that return
  50. "User::grent" objects. This object has methods that return the similarly
  51. named structure field name from the C's passwd structure from F<grp.h>;
  52. namely name, passwd, gid, and members (not mem). The first three
  53. return scalars, the last an array reference.
  54. You may also import all the structure fields directly into your namespace
  55. as regular variables using the :FIELDS import tag. (Note that this still
  56. overrides your core functions.) Access these fields as variables named
  57. with a preceding C<gr_>. Thus, C<$group_obj-E<gt>gid()> corresponds
  58. to $gr_gid if you import the fields. Array references are available as
  59. regular array variables, so C<@{ $group_obj-E<gt>members() }> would be
  60. simply @gr_members.
  61. The getpw() function is a simple front-end that forwards
  62. a numeric argument to getpwuid() and the rest to getpwnam().
  63. To access this functionality without the core overrides,
  64. pass the C<use> an empty import list, and then access
  65. function functions with their full qualified names.
  66. On the other hand, the built-ins are still available
  67. via the C<CORE::> pseudo-package.
  68. =head1 NOTE
  69. While this class is currently implemented using the Class::Struct
  70. module to build a struct-like class, you shouldn't rely upon this.
  71. =head1 AUTHOR
  72. Tom Christiansen