passwd.awk 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # passwd.awk --- access password file information
  2. #
  3. # Arnold Robbins, arnold@skeeve.com, Public Domain
  4. # May 1993
  5. # Revised October 2000
  6. # Revised December 2010
  7. BEGIN {
  8. # tailor this to suit your system
  9. _pw_awklib = "/usr/libexec/awk/"
  10. }
  11. function _pw_init( oldfs, oldrs, olddol0, pwcat, using_fw, using_fpat)
  12. {
  13. if (_pw_inited)
  14. return
  15. oldfs = FS
  16. oldrs = RS
  17. olddol0 = $0
  18. using_fw = (PROCINFO["FS"] == "FIELDWIDTHS")
  19. using_fpat = (PROCINFO["FS"] == "FPAT")
  20. FS = ":"
  21. RS = "\n"
  22. pwcat = _pw_awklib "pwcat"
  23. while ((pwcat | getline) > 0) {
  24. _pw_byname[$1] = $0
  25. _pw_byuid[$3] = $0
  26. _pw_bycount[++_pw_total] = $0
  27. }
  28. close(pwcat)
  29. _pw_count = 0
  30. _pw_inited = 1
  31. FS = oldfs
  32. if (using_fw)
  33. FIELDWIDTHS = FIELDWIDTHS
  34. else if (using_fpat)
  35. FPAT = FPAT
  36. RS = oldrs
  37. $0 = olddol0
  38. }
  39. function getpwnam(name)
  40. {
  41. _pw_init()
  42. return _pw_byname[name]
  43. }
  44. function getpwuid(uid)
  45. {
  46. _pw_init()
  47. return _pw_byuid[uid]
  48. }
  49. function getpwent()
  50. {
  51. _pw_init()
  52. if (_pw_count < _pw_total)
  53. return _pw_bycount[++_pw_count]
  54. return ""
  55. }
  56. function endpwent()
  57. {
  58. _pw_count = 0
  59. }