which.py revision ec758ead391daa2ce0e5697aa0e67ec3ba0f37af
1#! /usr/local/python
2
3# Variant of "which".
4# On stderr, near and total misses are reported.
5
6import sys, posix, string, path
7from stat import *
8
9def msg(str):
10	sys.stderr.write(str + '\n')
11
12pathlist = string.splitfields(posix.environ['PATH'], ':')
13
14sts = 0
15
16for prog in sys.argv[1:]:
17	ident = ()
18	for dir in pathlist:
19		file = path.cat(dir, prog)
20		try:
21			st = posix.stat(file)
22			if S_ISREG(st[ST_MODE]):
23				mode = S_IMODE(st[ST_MODE])
24				if mode % 2 or mode/8 % 2 or mode/64 % 2:
25					if ident:
26						if st[:3] = ident:
27							s = ': same as '
28						else:
29							s = ': also '
30						msg(prog + s + file)
31					else:
32						print file
33						ident = st[:3]
34				else:
35					msg(file + ': not executable')
36			else:
37				msg(file + ': not a disk file')
38		except posix.error:
39			pass
40	if not ident:
41		msg(prog + ': not found')
42		sts = 1
43
44sys.exit(sts)
45