Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. #!/usr/bin/env sh
  2.  
  3. # TROUBLESHOOTING:
  4. # If /bin/echo doesn't exist _and_ echo -n doesn't work, then this
  5. # script fails. A more graceful fallback to not dropping the trailing
  6. # newline in pipes would also work, but this hasn't been necessary yet.
  7.  
  8. usage () { cat <<'EOF' 1>&2
  9. lsowners [OPTIONS] [--] [DIRS...]
  10.  
  11. lsowners searches for OWNERS files in the given DIRS. If no DIRS are
  12. named, it searches the current working directory. Unless --no-recursive
  13. is passed, it will also search directories above each dir for OWNERS
  14. files.
  15.  
  16. If you pass both --recursive and --no-recursive (or both --printlf and
  17. --print0), the last flag in the arguments list is used.
  18.  
  19. Options:
  20. -r, --recursive walk parent directories for OWNERS files (default)
  21. -R, --no-recursive disable --recursive
  22. -n, --printlf print output names separated by \n bytes (default)
  23. -0, --print0 print output names separated by NUL bytes. this is
  24. useful if passing the output to xargs (or similar).
  25. EOF
  26. }
  27.  
  28. # globals
  29. owners=''
  30. recursive=1
  31. zerosep=0
  32. parsed=0
  33.  
  34. while test $# -gt 0
  35. do
  36. case "$1" in
  37. -h|--help) usage; exit 2;;
  38. -R|--no-recursive) recursive=0 ;; # disable walk up the tree
  39. -r|--recursive) recursive=1 ;; # default
  40. -0|--print0) zerosep=1 ;; # print output with zero separators
  41. -n|--printlf) zerosep=0 ;; # print output with \n separators
  42. --) shift; parsed=1 ;; # remaining arguments are directories
  43. *) break;;
  44. esac
  45.  
  46. if test $parsed -eq 1
  47. then
  48. break
  49. fi
  50. shift
  51. done
  52.  
  53. # make sure echo builtin actually supports the -n flag -- if it doesn't,
  54. # fall back to /bin/echo -- if /bin/echo doesn't exist, then give up.
  55. if test "$(echo -n test)" = "-n test"
  56. then
  57. if test -e /bin/echo && test -x /bin/echo
  58. then
  59. puts () { /bin/echo "$@"; }
  60. else
  61. echo '/bin/echo does not exist' 2>&1
  62. exit 1
  63. fi
  64. else
  65. puts () { echo "$@"; }
  66. fi
  67.  
  68. strip_empty () {
  69. sed -e '/^[[:space:]]*#/d;/^[[:space:]]*$/d;s/^[[:space:]]*//;s/[[:space:]]*$//' |
  70. tr -s $'\n'
  71. }
  72.  
  73. owners_in () {
  74. local dir="$1"
  75.  
  76. while :
  77. do
  78. if test -f "$dir/OWNERS"
  79. then
  80. owners="$({ echo "$owners"; cat "$dir/OWNERS"; } | strip_empty | sort | uniq)"
  81. fi
  82.  
  83. if test $recursive -eq 0
  84. then
  85. break
  86. fi
  87.  
  88. if test "$dir" = /
  89. then
  90. break
  91. fi
  92. dir="$(cd -P "$(dirname "$dir")" && pwd)"
  93. done
  94. }
  95.  
  96. if test $# -eq 0
  97. then
  98. set -- "$PWD"
  99. fi
  100.  
  101. for argdir in "$@"
  102. do
  103. owners_in "$argdir"
  104. done
  105.  
  106. if test $zerosep -eq 1
  107. then
  108. puts -n "$owners" | tr -s '\n' '\0'
  109. exit
  110. fi
  111.  
  112. if ! test -t 1
  113. then
  114. puts -n "$owners"
  115. exit
  116. fi
  117.  
  118. puts "$owners"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement