1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
|
/*
* which v2.x -- print full path of executables
* Copyright (C) 1999, 2003, 2007 Carlo Wood <carlo@gnu.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02110-1301, USA.
*/
#include "sys.h"
#include <stdio.h>
#include "getopt.h"
#include "tilde/tilde.h"
#include "bash.h"
static const char *progname;
static void print_usage(FILE *out)
{
fprintf(out, "Usage: %s [options] [--] programname [...]\n", progname);
fprintf(out, "Options: --version, -[vV] Print version and exit successfully.\n");
fprintf(out, " --help, Print this help and exit successfully.\n");
fprintf(out, " --skip-dot Skip directories in PATH that start with a dot.\n");
fprintf(out, " --skip-tilde Skip directories in PATH that start with a tilde.\n");
fprintf(out, " --show-dot Don't expand a dot to current directory in output.\n");
fprintf(out, " --show-tilde Output a tilde for HOME directory for non-root.\n");
fprintf(out, " --tty-only Stop processing options on the right if not on tty.\n");
fprintf(out, " --all, -a Print all matches in PATH, not just the first\n");
fprintf(out, " --read-alias, -i Read list of aliases from stdin.\n");
fprintf(out, " --skip-alias Ignore option --read-alias; don't read stdin.\n");
fprintf(out, " --read-functions Read shell functions from stdin.\n");
fprintf(out, " --skip-functions Ignore option --read-functions; don't read stdin.\n");
}
static void print_version(void)
{
fprintf(stdout, "GNU which v" VERSION ", Copyright (C) 1999 - 2007 Carlo Wood.\n");
fprintf(stdout, "GNU which comes with ABSOLUTELY NO WARRANTY;\n");
fprintf(stdout, "This program is free software; your freedom to use, change\n");
fprintf(stdout, "and distribute this program is protected by the GPL.\n");
}
static void print_fail(const char *name, const char *path_list)
{
fprintf(stderr, "%s: no %s in (%s)\n", progname, name, path_list);
}
static char home[256];
static size_t homelen = 0;
static int absolute_path_given;
static int found_path_starts_with_dot;
static char *abs_path;
static int skip_dot = 0, skip_tilde = 0, skip_alias = 0, read_alias = 0;
static int show_dot = 0, show_tilde = 0, show_all = 0, tty_only = 0;
static int skip_functions = 0, read_functions = 0;
static char *find_command_in_path(const char *name, const char *path_list, int *path_index)
{
char *found = NULL, *full_path;
int status, name_len;
name_len = strlen(name);
if (!absolute_program(name))
absolute_path_given = 0;
else
{
char *p;
absolute_path_given = 1;
if (abs_path)
free(abs_path);
if (*name != '.' && *name != '/' && *name != '~')
{
abs_path = (char *)xmalloc(3 + name_len);
strcpy(abs_path, "./");
strcat(abs_path, name);
}
else
{
abs_path = (char *)xmalloc(1 + name_len);
strcpy(abs_path, name);
}
path_list = abs_path;
p = strrchr(abs_path, '/');
*p++ = 0;
name = p;
}
while (path_list && path_list[*path_index])
{
char *path;
if (absolute_path_given)
{
path = savestring(path_list);
*path_index = strlen(path);
}
else
path = get_next_path_element(path_list, path_index);
if (!path)
break;
if (*path == '~')
{
char *t = tilde_expand(path);
free(path);
path = t;
if (skip_tilde)
{
free(path);
continue;
}
}
if (skip_dot && *path != '/')
{
free(path);
continue;
}
found_path_starts_with_dot = (*path == '.');
full_path = make_full_pathname(path, name, name_len);
free(path);
status = file_status(full_path);
if ((status & FS_EXISTS) && (status & FS_EXECABLE))
{
found = full_path;
break;
}
free(full_path);
}
return (found);
}
static char cwd[256];
static size_t cwdlen;
static void get_current_working_directory(void)
{
if (cwdlen)
return;
if (!getcwd(cwd, sizeof(cwd)))
{
const char *pwd = getenv("PWD");
if (pwd && strlen(pwd) < sizeof(cwd))
strcpy(cwd, pwd);
}
if (*cwd != '/')
{
fprintf(stderr, "Can't get current working directory\n");
exit(-1);
}
cwdlen = strlen(cwd);
if (cwd[cwdlen - 1] != '/')
{
cwd[cwdlen++] = '/';
cwd[cwdlen] = 0;
}
}
static char *path_clean_up(const char *path)
{
static char result[256];
const char *p1 = path;
char *p2 = result;
int saw_slash = 0, saw_slash_dot = 0, saw_slash_dot_dot = 0;
if (*p1 != '/')
{
get_current_working_directory();
strcpy(result, cwd);
saw_slash = 1;
p2 = &result[cwdlen];
}
do
{
if (!saw_slash || *p1 != '/')
*p2++ = *p1;
if (saw_slash_dot && (*p1 == '/'))
p2 -= 2;
if (saw_slash_dot_dot && (*p1 == '/'))
{
int cnt = 0;
do
{
if (--p2 < result)
{
strcpy(result, path);
return result;
}
if (*p2 == '/')
++cnt;
}
while (cnt != 3);
++p2;
}
saw_slash_dot_dot = saw_slash_dot && (*p1 == '.');
saw_slash_dot = saw_slash && (*p1 == '.');
saw_slash = (*p1 == '/');
}
while (*p1++);
return result;
}
struct function_st {
char *name;
size_t len;
char **lines;
int line_count;
};
static struct function_st *functions;
static int func_count;
static int max_func_count;
static char **aliases;
static int alias_count;
static int max_alias_count;
int func_search(int indent, const char *cmd, struct function_st *func_list, int function_start_type)
{
int i;
for (i = 0; i < func_count; ++i)
{
if (!strcmp(functions[i].name, cmd))
{
int j;
if (indent)
fputc('\t', stdout);
if (function_start_type == 1)
fprintf(stdout, "%s () {\n", cmd);
else
fprintf(stdout, "%s ()\n", cmd);
for (j = 0; j < functions[i].line_count; ++j)
{
if (indent)
fputc('\t', stdout);
fputs(functions[i].lines[j], stdout);
}
return 1;
}
}
return 0;
}
int path_search(int indent, const char *cmd, const char *path_list)
{
char *result = NULL;
int found_something = 0;
if (path_list && *path_list != '\0')
{
int next;
int path_index = 0;
do
{
next = show_all;
result = find_command_in_path(cmd, path_list, &path_index);
if (result)
{
const char *full_path = path_clean_up(result);
int in_home = (show_tilde || skip_tilde) && !strncmp(full_path, home, homelen);
if (indent)
fprintf(stdout, "\t");
if (!(skip_tilde && in_home) && show_dot && found_path_starts_with_dot && !strncmp(full_path, cwd, cwdlen))
{
full_path += cwdlen;
fprintf(stdout, "./");
}
else if (in_home)
{
if (skip_tilde)
{
next = 1;
free(result);
continue;
}
if (show_tilde)
{
full_path += homelen;
fprintf(stdout, "~/");
}
}
fprintf(stdout, "%s\n", full_path);
free(result);
found_something = 1;
}
else
break;
}
while (next);
}
return found_something;
}
void process_alias(const char *str, int argc, char *argv[], const char *path_list, int function_start_type)
{
const char *p = str;
int len = 0;
while(*p == ' ' || *p == '\t')
++p;
if (!strncmp("alias", p, 5))
p += 5;
while(*p == ' ' || *p == '\t')
++p;
while(*p && *p != ' ' && *p != '\t' && *p != '=')
++p, ++len;
for (; argc > 0; --argc, ++argv)
{
char q = 0;
char *cmd;
if (!*argv || len != strlen(*argv) || strncmp(*argv, &p[-len], len))
continue;
fputs(str, stdout);
if (!show_all)
*argv = NULL;
while(*p == ' ' || *p == '\t')
++p;
if (*p == '=')
++p;
while(*p == ' ' || *p == '\t')
++p;
if (*p == '"' || *p == '\'')
q = *p, ++p;
for(;;)
{
int found = 0;
while(*p == ' ' || *p == '\t')
++p;
len = 0;
while(*p && *p != ' ' && *p != '\t' && *p != q && *p != '|' && *p != '&')
++p, ++len;
cmd = (char *)xmalloc(len + 1);
strncpy(cmd, &p[-len], len);
cmd[len] = 0;
if (*argv && !strcmp(cmd, *argv))
*argv = NULL;
if (read_functions && !strchr(cmd, '/'))
found = func_search(1, cmd, functions, function_start_type);
if (show_all || !found)
path_search(1, cmd, path_list);
free(cmd);
while(*p && (*p != '|' || p[1] == '|') && (*p != '&' || p[1] == '&'))
++p;
if (!*p)
break;
++p;
}
break;
}
}
enum opts {
opt_version,
opt_skip_dot,
opt_skip_tilde,
opt_skip_alias,
opt_read_functions,
opt_skip_functions,
opt_show_dot,
opt_show_tilde,
opt_tty_only,
opt_help
};
#ifdef __TANDEM
/* According to Tom Bates, <tom.bates@hp.com> */
static uid_t const superuser = 65535;
#else
static uid_t const superuser = 0;
#endif
int main(int argc, char *argv[])
{
const char *path_list = getenv("PATH");
int short_option, fail_count = 0;
static int long_option;
struct option longopts[] = {
{"help", 0, &long_option, opt_help},
{"version", 0, &long_option, opt_version},
{"skip-dot", 0, &long_option, opt_skip_dot},
{"skip-tilde", 0, &long_option, opt_skip_tilde},
{"show-dot", 0, &long_option, opt_show_dot},
{"show-tilde", 0, &long_option, opt_show_tilde},
{"tty-only", 0, &long_option, opt_tty_only},
{"all", 0, NULL, 'a'},
{"read-alias", 0, NULL, 'i'},
{"skip-alias", 0, &long_option, opt_skip_alias},
{"read-functions", 0, &long_option, opt_read_functions},
{"skip-functions", 0, &long_option, opt_skip_functions},
{NULL, 0, NULL, 0}
};
progname = argv[0];
while ((short_option = getopt_long(argc, argv, "aivV", longopts, NULL)) != -1)
{
switch (short_option)
{
case 0:
switch (long_option)
{
case opt_help:
print_usage(stdout);
return 0;
case opt_version:
print_version();
return 0;
case opt_skip_dot:
skip_dot = !tty_only;
break;
case opt_skip_tilde:
skip_tilde = !tty_only;
break;
case opt_skip_alias:
skip_alias = 1;
break;
case opt_show_dot:
show_dot = !tty_only;
break;
case opt_show_tilde:
show_tilde = (!tty_only && geteuid() != superuser);
break;
case opt_tty_only:
tty_only = !isatty(1);
break;
case opt_read_functions:
read_functions = 1;
break;
case opt_skip_functions:
skip_functions = 1;
break;
}
break;
case 'a':
show_all = 1;
break;
case 'i':
read_alias = 1;
break;
case 'v':
case 'V':
print_version();
return 0;
}
}
uidget();
if (show_dot)
get_current_working_directory();
if (show_tilde || skip_tilde)
{
const char *h;
if (!(h = getenv("HOME")))
{
fprintf(stderr, "%s: ", progname);
if (show_tilde)
fprintf(stderr, "--show-tilde");
else
fprintf(stderr, "--skip-tilde");
fprintf(stderr, ": Environment variable HOME not set\n");
show_tilde = skip_tilde = 0;
}
else
{
strncpy(home, h, sizeof(home));
home[sizeof(home) - 1] = 0;
homelen = strlen(home);
if (home[homelen - 1] != '/' && homelen < sizeof(home) - 1)
{
strcat(home, "/");
++homelen;
}
}
}
if (skip_alias)
read_alias = 0;
if (skip_functions)
read_functions = 0;
argv += optind;
argc -= optind;
if (argc == 0)
{
print_usage(stderr);
return -1;
}
int function_start_type = 0;
if (read_alias || read_functions)
{
char buf[1024];
int processing_aliases = read_alias;
if (isatty(0))
{
fprintf(stderr, "%s: %s: Warning: stdin is a tty.\n", progname,
(read_functions ? read_alias ? "--read-functions, --read-alias, -i" : "--read-functions" : "--read-alias, -i"));
}
while (fgets(buf, sizeof(buf), stdin))
{
int looks_like_function_start = 0;
int function_start_has_declare;
if (read_functions)
{
// bash version 2.0.5a and older output a pattern for `str' like
// declare -fx FUNCTION_NAME ()
// {
// body
// }
//
// bash version 2.0.5b and later output a pattern for `str' like
// FUNCTION_NAME ()
// {
// body
// }
char *p = buf + strlen(buf) - 1;
while (isspace(*p) && p > buf + 2)
--p;
if (*p == ')' && p[-1] == '(' && p[-2] == ' ')
{
looks_like_function_start = 1;
function_start_has_declare = (strncmp("declare -", buf, 9) == 0);
}
// Add some zsh support here.
// zsh does output a pattern for `str' like
// FUNCTION () {
// body
// }
if (p > buf + 4 && *p == '{' && p[-1] == ' ' &&
p[-2] == ')' && p[-3] == '(' && p[-4] == ' ')
{
looks_like_function_start = 1;
function_start_type = 1;
function_start_has_declare = 0;
}
}
if (processing_aliases && !looks_like_function_start)
{
// bash version 2.0.5b can throw in lines like "declare -fx FUNCTION_NAME", eat them.
if (!strncmp("declare -", buf, 9))
continue;
if (alias_count == max_alias_count)
{
max_alias_count += 32;
aliases = (char **)xrealloc(aliases, max_alias_count * sizeof(char *));
}
aliases[alias_count++] = strcpy((char *)xmalloc(strlen(buf) + 1), buf);
}
else if (read_functions && looks_like_function_start)
{
struct function_st *function;
int max_line_count;
const char *p = buf;
int len = 0;
processing_aliases = 0;
// Eat "declare -fx " at start of bash version 2.0.5a and older, if present.
if (function_start_has_declare)
{
p += 9;
while(*p && *p++ != ' ');
}
while(*p && *p != ' ')
++p, ++len;
if (func_count == max_func_count)
{
max_func_count += 16;
functions = (struct function_st *)xrealloc(functions, max_func_count * sizeof(struct function_st));
}
function = &functions[func_count++];
function->name = (char *)xmalloc(len + 1);
strncpy(function->name, &p[-len], len);
function->name[len] = 0;
function->len = len;
max_line_count = 32;
function->lines = (char **)xmalloc(max_line_count * sizeof(char *));
function->line_count = 0;
while (fgets(buf, sizeof(buf), stdin))
{
size_t blen = strlen(buf);
function->lines[function->line_count++] = strcpy((char *)xmalloc(blen + 1), buf);
if (!strcmp(buf, "}\n"))
break;
if (function->line_count == max_line_count)
{
max_line_count += 32;
function->lines = (char **)xrealloc(function->lines, max_line_count * sizeof(char *));
}
}
}
}
if (read_alias)
{
int i;
for (i = 0; i < alias_count; ++i)
process_alias(aliases[i], argc, argv, path_list, function_start_type);
}
}
for (; argc > 0; --argc, ++argv)
{
int found_something = 0;
if (!*argv)
continue;
if (read_functions && !strchr(*argv, '/'))
found_something = func_search(0, *argv, functions, function_start_type);
if ((show_all || !found_something) && !path_search(0, *argv, path_list) && !found_something)
{
print_fail(absolute_path_given ? strrchr(*argv, '/') + 1 : *argv, absolute_path_given ? abs_path : path_list);
++fail_count;
}
}
return fail_count;
}
#ifdef NEED_XMALLOC
void *xmalloc(size_t size)
{
void *ptr = malloc(size);
if (ptr == NULL)
{
fprintf(stderr, "%s: Out of memory", progname);
exit(-1);
}
return ptr;
}
void *xrealloc(void *ptr, size_t size)
{
if (!ptr)
return xmalloc(size);
ptr = realloc(ptr, size);
if (size > 0 && ptr == NULL)
{
fprintf(stderr, "%s: Out of memory\n", progname);
exit(-1);
}
return ptr;
}
#endif /* NEED_XMALLOC */
|