Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcd1e6e

Browse files
committed
Added '-l[flags]' option.
1 parent 8b83bce commit fcd1e6e

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

Tools/scripts/which.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Variant of "which".
44
# On stderr, near and total misses are reported.
5+
# '-l<flags>' argument adds ls -l<flags> of each file found.
56

67
import sys, posix, string, path
78
from stat import *
@@ -12,31 +13,39 @@ def msg(str):
1213
pathlist = string.splitfields(posix.environ['PATH'], ':')
1314

1415
sts = 0
16+
longlist = ''
17+
18+
if sys.argv[1:] and sys.argv[1][:2] == '-l':
19+
longlist = sys.argv[1]
20+
del sys.argv[1]
1521

1622
for prog in sys.argv[1:]:
1723
ident = ()
1824
for dir in pathlist:
1925
file = path.join(dir, prog)
2026
try:
2127
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]
28+
except posix.error:
29+
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]
3438
else:
35-
msg(file + ': not executable')
39+
if st[:3] == ident:
40+
s = 'same as: '
41+
else:
42+
s = 'also: '
43+
msg(s + file)
3644
else:
37-
msg(file + ': not a disk file')
38-
except posix.error:
39-
pass
45+
msg(file + ': not executable')
46+
if longlist:
47+
sts = posix.system('ls ' + longlist + ' ' + file)
48+
if sts: msg('"ls -l" exit status: ' + `sts`)
4049
if not ident:
4150
msg(prog + ': not found')
4251
sts = 1

0 commit comments

Comments
 (0)