blob: 94c7556195cc7330a8cd7c51b001b491cc93d682 [file] [log] [blame]
Guido van Rossumec758ea1991-06-04 20:36:54 +00001#! /usr/local/python
2
3# Variant of "which".
4# On stderr, near and total misses are reported.
Guido van Rossumfcd1e6e1992-03-02 16:17:31 +00005# '-l<flags>' argument adds ls -l<flags> of each file found.
Guido van Rossumec758ea1991-06-04 20:36:54 +00006
7import sys, posix, string, path
8from stat import *
9
10def msg(str):
11 sys.stderr.write(str + '\n')
12
13pathlist = string.splitfields(posix.environ['PATH'], ':')
14
15sts = 0
Guido van Rossumfcd1e6e1992-03-02 16:17:31 +000016longlist = ''
17
18if sys.argv[1:] and sys.argv[1][:2] == '-l':
19 longlist = sys.argv[1]
20 del sys.argv[1]
Guido van Rossumec758ea1991-06-04 20:36:54 +000021
22for prog in sys.argv[1:]:
23 ident = ()
24 for dir in pathlist:
Guido van Rossum9c5c8081991-07-01 18:22:34 +000025 file = path.join(dir, prog)
Guido van Rossumec758ea1991-06-04 20:36:54 +000026 try:
27 st = posix.stat(file)
Guido van Rossumec758ea1991-06-04 20:36:54 +000028 except posix.error:
Guido van Rossumfcd1e6e1992-03-02 16:17:31 +000029 continue
30 if not S_ISREG(st[ST_MODE]):
31 msg(file + ': not a disk file')
32 else:
33 mode = S_IMODE(st[ST_MODE])
34 if mode & 0111:
35 if not ident:
36 print file
37 ident = st[:3]
38 else:
39 if st[:3] == ident:
40 s = 'same as: '
41 else:
42 s = 'also: '
43 msg(s + file)
44 else:
45 msg(file + ': not executable')
46 if longlist:
47 sts = posix.system('ls ' + longlist + ' ' + file)
48 if sts: msg('"ls -l" exit status: ' + `sts`)
Guido van Rossumec758ea1991-06-04 20:36:54 +000049 if not ident:
50 msg(prog + ': not found')
51 sts = 1
52
53sys.exit(sts)