blob: a05d91998a58d97a09729735259dec3a5ee24020 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumec758ea1991-06-04 20:36:54 +00002
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
Guido van Rossum9476a781997-10-20 23:48:01 +00007import sys
8if sys.path[0] in (".", ""): del sys.path[0]
9
Guido van Rossume7b88e71992-03-30 11:13:59 +000010import sys, os, string
Guido van Rossumec758ea1991-06-04 20:36:54 +000011from stat import *
12
13def msg(str):
14 sys.stderr.write(str + '\n')
15
Guido van Rossume7b88e71992-03-30 11:13:59 +000016pathlist = string.splitfields(os.environ['PATH'], ':')
Guido van Rossumec758ea1991-06-04 20:36:54 +000017
18sts = 0
Guido van Rossumfcd1e6e1992-03-02 16:17:31 +000019longlist = ''
20
21if sys.argv[1:] and sys.argv[1][:2] == '-l':
22 longlist = sys.argv[1]
23 del sys.argv[1]
Guido van Rossumec758ea1991-06-04 20:36:54 +000024
25for prog in sys.argv[1:]:
26 ident = ()
27 for dir in pathlist:
Guido van Rossume7b88e71992-03-30 11:13:59 +000028 file = os.path.join(dir, prog)
Guido van Rossumec758ea1991-06-04 20:36:54 +000029 try:
Guido van Rossume7b88e71992-03-30 11:13:59 +000030 st = os.stat(file)
31 except os.error:
Guido van Rossumfcd1e6e1992-03-02 16:17:31 +000032 continue
33 if not S_ISREG(st[ST_MODE]):
34 msg(file + ': not a disk file')
35 else:
36 mode = S_IMODE(st[ST_MODE])
37 if mode & 0111:
38 if not ident:
39 print file
40 ident = st[:3]
41 else:
42 if st[:3] == ident:
43 s = 'same as: '
44 else:
45 s = 'also: '
46 msg(s + file)
47 else:
48 msg(file + ': not executable')
49 if longlist:
Guido van Rossume7b88e71992-03-30 11:13:59 +000050 sts = os.system('ls ' + longlist + ' ' + file)
Guido van Rossumfcd1e6e1992-03-02 16:17:31 +000051 if sts: msg('"ls -l" exit status: ' + `sts`)
Guido van Rossumec758ea1991-06-04 20:36:54 +000052 if not ident:
53 msg(prog + ': not found')
54 sts = 1
55
56sys.exit(sts)