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.

103 lines
2.9 KiB

  1. package User::pwent;
  2. use strict;
  3. BEGIN {
  4. use Exporter ();
  5. use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
  6. @EXPORT = qw(getpwent getpwuid getpwnam getpw);
  7. @EXPORT_OK = qw(
  8. $pw_name $pw_passwd $pw_uid
  9. $pw_gid $pw_quota $pw_comment
  10. $pw_gecos $pw_dir $pw_shell
  11. );
  12. %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
  13. }
  14. use vars @EXPORT_OK;
  15. # Class::Struct forbids use of @ISA
  16. sub import { goto &Exporter::import }
  17. use Class::Struct qw(struct);
  18. struct 'User::pwent' => [
  19. name => '$',
  20. passwd => '$',
  21. uid => '$',
  22. gid => '$',
  23. quota => '$',
  24. comment => '$',
  25. gecos => '$',
  26. dir => '$',
  27. shell => '$',
  28. ];
  29. sub populate (@) {
  30. return unless @_;
  31. my $pwob = new();
  32. ( $pw_name, $pw_passwd, $pw_uid,
  33. $pw_gid, $pw_quota, $pw_comment,
  34. $pw_gecos, $pw_dir, $pw_shell, ) = @$pwob = @_;
  35. return $pwob;
  36. }
  37. sub getpwent ( ) { populate(CORE::getpwent()) }
  38. sub getpwnam ($) { populate(CORE::getpwnam(shift)) }
  39. sub getpwuid ($) { populate(CORE::getpwuid(shift)) }
  40. sub getpw ($) { ($_[0] =~ /^\d+/) ? &getpwuid : &getpwnam }
  41. 1;
  42. __END__
  43. =head1 NAME
  44. User::pwent - by-name interface to Perl's built-in getpw*() functions
  45. =head1 SYNOPSIS
  46. use User::pwent;
  47. $pw = getpwnam('daemon') or die "No daemon user";
  48. if ( $pw->uid == 1 && $pw->dir =~ m#^/(bin|tmp)?$# ) {
  49. print "gid 1 on root dir";
  50. }
  51. use User::pwent qw(:FIELDS);
  52. getpwnam('daemon') or die "No daemon user";
  53. if ( $pw_uid == 1 && $pw_dir =~ m#^/(bin|tmp)?$# ) {
  54. print "gid 1 on root dir";
  55. }
  56. $pw = getpw($whoever);
  57. =head1 DESCRIPTION
  58. This module's default exports override the core getpwent(), getpwuid(),
  59. and getpwnam() functions, replacing them with versions that return
  60. "User::pwent" objects. This object has methods that return the similarly
  61. named structure field name from the C's passwd structure from F<pwd.h>;
  62. namely name, passwd, uid, gid, quota, comment, gecos, dir, and shell.
  63. You may also import all the structure fields directly into your namespace
  64. as regular variables using the :FIELDS import tag. (Note that this still
  65. overrides your core functions.) Access these fields as
  66. variables named with a preceding C<pw_> in front their method names.
  67. Thus, C<$passwd_obj-E<gt>shell()> corresponds to $pw_shell if you import
  68. the fields.
  69. The getpw() function is a simple front-end that forwards
  70. a numeric argument to getpwuid() and the rest to getpwnam().
  71. To access this functionality without the core overrides,
  72. pass the C<use> an empty import list, and then access
  73. function functions with their full qualified names.
  74. On the other hand, the built-ins are still available
  75. via the C<CORE::> pseudo-package.
  76. =head1 NOTE
  77. While this class is currently implemented using the Class::Struct
  78. module to build a struct-like class, you shouldn't rely upon this.
  79. =head1 AUTHOR
  80. Tom Christiansen