Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 2 | |
| 3 | # Variant of "which". |
| 4 | # On stderr, near and total misses are reported. |
Guido van Rossum | fcd1e6e | 1992-03-02 16:17:31 +0000 | [diff] [blame] | 5 | # '-l<flags>' argument adds ls -l<flags> of each file found. |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 7 | import sys, os, string |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 8 | from stat import * |
| 9 | |
| 10 | def msg(str): |
| 11 | sys.stderr.write(str + '\n') |
| 12 | |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 13 | pathlist = string.splitfields(os.environ['PATH'], ':') |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 14 | |
| 15 | sts = 0 |
Guido van Rossum | fcd1e6e | 1992-03-02 16:17:31 +0000 | [diff] [blame] | 16 | longlist = '' |
| 17 | |
| 18 | if sys.argv[1:] and sys.argv[1][:2] == '-l': |
| 19 | longlist = sys.argv[1] |
| 20 | del sys.argv[1] |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 21 | |
| 22 | for prog in sys.argv[1:]: |
| 23 | ident = () |
| 24 | for dir in pathlist: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 25 | file = os.path.join(dir, prog) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 26 | try: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 27 | st = os.stat(file) |
| 28 | except os.error: |
Guido van Rossum | fcd1e6e | 1992-03-02 16:17:31 +0000 | [diff] [blame] | 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] |
| 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: |
Guido van Rossum | e7b88e7 | 1992-03-30 11:13:59 +0000 | [diff] [blame] | 47 | sts = os.system('ls ' + longlist + ' ' + file) |
Guido van Rossum | fcd1e6e | 1992-03-02 16:17:31 +0000 | [diff] [blame] | 48 | if sts: msg('"ls -l" exit status: ' + `sts`) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 49 | if not ident: |
| 50 | msg(prog + ': not found') |
| 51 | sts = 1 |
| 52 | |
| 53 | sys.exit(sts) |