1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | # Variant of "which". |
---|
4 | n/a | # On stderr, near and total misses are reported. |
---|
5 | n/a | # '-l<flags>' argument adds ls -l<flags> of each file found. |
---|
6 | n/a | |
---|
7 | n/a | import sys |
---|
8 | n/a | if sys.path[0] in (".", ""): del sys.path[0] |
---|
9 | n/a | |
---|
10 | n/a | import sys, os |
---|
11 | n/a | from stat import * |
---|
12 | n/a | |
---|
13 | n/a | def msg(str): |
---|
14 | n/a | sys.stderr.write(str + '\n') |
---|
15 | n/a | |
---|
16 | n/a | def main(): |
---|
17 | n/a | pathlist = os.environ['PATH'].split(os.pathsep) |
---|
18 | n/a | |
---|
19 | n/a | sts = 0 |
---|
20 | n/a | longlist = '' |
---|
21 | n/a | |
---|
22 | n/a | if sys.argv[1:] and sys.argv[1][:2] == '-l': |
---|
23 | n/a | longlist = sys.argv[1] |
---|
24 | n/a | del sys.argv[1] |
---|
25 | n/a | |
---|
26 | n/a | for prog in sys.argv[1:]: |
---|
27 | n/a | ident = () |
---|
28 | n/a | for dir in pathlist: |
---|
29 | n/a | filename = os.path.join(dir, prog) |
---|
30 | n/a | try: |
---|
31 | n/a | st = os.stat(filename) |
---|
32 | n/a | except OSError: |
---|
33 | n/a | continue |
---|
34 | n/a | if not S_ISREG(st[ST_MODE]): |
---|
35 | n/a | msg(filename + ': not a disk file') |
---|
36 | n/a | else: |
---|
37 | n/a | mode = S_IMODE(st[ST_MODE]) |
---|
38 | n/a | if mode & 0o111: |
---|
39 | n/a | if not ident: |
---|
40 | n/a | print(filename) |
---|
41 | n/a | ident = st[:3] |
---|
42 | n/a | else: |
---|
43 | n/a | if st[:3] == ident: |
---|
44 | n/a | s = 'same as: ' |
---|
45 | n/a | else: |
---|
46 | n/a | s = 'also: ' |
---|
47 | n/a | msg(s + filename) |
---|
48 | n/a | else: |
---|
49 | n/a | msg(filename + ': not executable') |
---|
50 | n/a | if longlist: |
---|
51 | n/a | sts = os.system('ls ' + longlist + ' ' + filename) |
---|
52 | n/a | if sts: msg('"ls -l" exit status: ' + repr(sts)) |
---|
53 | n/a | if not ident: |
---|
54 | n/a | msg(prog + ': not found') |
---|
55 | n/a | sts = 1 |
---|
56 | n/a | |
---|
57 | n/a | sys.exit(sts) |
---|
58 | n/a | |
---|
59 | n/a | if __name__ == '__main__': |
---|
60 | n/a | main() |
---|