Initial git import
[pkgusr] / usr / bin / grep_all_regular_files_for
1 #!/bin/bash
2 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
3 # You may do everything with this code except misrepresent its origin.
4 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
5
6 #The following list should contain the mount points of all filesystems
7 #that are to be scanned as a space-separated list within parentheses. 
8 #/ will usually be in this list and if you have /usr
9 #on a separate partition, it will also be in this list. 
10 #Mount points whose filesystems are special, such as procfs or sysfs must
11 #not be in this list. While a simple find on those special filesystems should 
12 #be harmless, operations such as "-exec grep something" are NOT SAFE and may 
13 #have HARMFUL SIDE-EFFECTS, especially when performed as root. 
14
15 ## Bastard settings
16 # fs_to_scan=(/ /opt /usr /usr/local /var)
17
18 fs_to_scan=(/)
19
20 #Files with a path prefix found in the following list are ignored. As the
21 #main function of this script is to help you find files that contain
22 #hardwired paths to /tools or other unwanted references to
23 #your build system, you will usually prune any directories that don't contain
24 #files of interest, such as /tools (whose files naturally refer to /tools)
25 #and your package users' home directories (which may also test positive if
26 #you have unpacked and configured sources lying around).
27 #NOTE: The LFS-6.0 book uses a ramfs mounted on /dev and with that setup
28 #/dev does not need to be in the prune list. But since there is no requirement
29 #that /dev have its on filesystem it's better to prune it explicitly.
30 prune_prefixes=(/home /usr/src /dev /tools) #NO TRAILING SLASHES!!!
31
32 if [ $# -lt 1 -o "$1" = "--help" ]; then
33     echo 1>&2 
34     echo 1>&2 'USAGE: '"${0##*/}"' <grep-commands>'
35     echo 1>&2 
36     echo 1>&2 '  grep -l <grep-commands> -- <file>'
37     echo 1>&2 '  will be executed for each *regular file* <file>'
38     echo 1>&2 '  ATTENTION! If you override the -l switch with a switch that makes grep'
39     echo 1>&2 '  output all individual matches rather than just the matching files,'
40     echo 1>&2 '  then DO NOT redirect output to a file that is in a directory that will be'
41     echo 1>&2 '  scanned, or you risk creating an endless loop that will cause your'
42     echo 1>&2 '  output file to grow till your disk is full.'
43     echo 1>&2 
44     exit 1
45 fi
46
47 #suppress ugly debug output from shell
48 trap ':' SIGPIPE
49
50 #construct find commands that match the prune_prefixes. Each prefix will be
51 #matched as -path <prefix> -or -path <prefix>/*
52 #so that the directory itself and all subdirectories are matched.
53 y=(\( -false)
54 for ((i=0; $i<${#prune_prefixes[@]}; i=$i+1)) 
55 do
56     y[${#y[@]}]='-or'
57     y[${#y[@]}]=-path
58     y[${#y[@]}]="${prune_prefixes[$i]}"
59     y[${#y[@]}]='-or'
60     y[${#y[@]}]=-path
61     y[${#y[@]}]="${prune_prefixes[$i]}/*"
62 done
63 y[${#y[@]}]=')'
64
65 cmd_pre=(-type f -exec grep -l)
66 cmd_post=(-- {} \;)
67
68 #In the following find command, the part
69 # -not ( ( "${y[@]}" -prune ) -or "${y[@]}" )
70 #is responsible for preventing the files that match prune_prefixes from
71 #being processed. The 2nd "${y[@]}" may seem redundant, but it isn't, because
72 #-prune has no effect and is always false when -depth is used (which someone
73 #might do in the future).
74 #The -true before "$@" ensures that -depth can be passed as 1st parameter
75 #of $cmd_pre (should someone change it in the future).
76 find "${fs_to_scan[@]}" -xdev -noleaf \
77     -not \( \( "${y[@]}" -prune \) -or "${y[@]}" \) \
78     -and \( -true "${cmd_pre[@]}" "$@" "${cmd_post[@]}" \)