Leaked source code of windows server 2003
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.

94 lines
2.9 KiB

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