1 |
douglas |
1 |
// Douglas Thrift |
2 |
|
|
// |
3 |
|
|
// CCS Computer Science |
4 |
douglas |
34 |
// Shells |
5 |
douglas |
1 |
// |
6 |
|
|
// $Id$ |
7 |
|
|
|
8 |
douglas |
34 |
#include <stdbool.h> |
9 |
douglas |
1 |
|
10 |
|
|
#include "common.h" |
11 |
|
|
|
12 |
|
|
int main(int argc, char *argv[]) |
13 |
|
|
{ |
14 |
|
|
int exception; |
15 |
|
|
jmp_buf environment; |
16 |
|
|
|
17 |
|
|
switch (exception = setjmp(environment)) |
18 |
|
|
{ |
19 |
|
|
case 0: |
20 |
|
|
break; |
21 |
|
|
case 1: |
22 |
|
|
perror(argv[0]); |
23 |
|
|
|
24 |
|
|
return 1; |
25 |
|
|
default: |
26 |
|
|
fprintf(stderr, "%s: %s\n", argv[0], (char *)exception); |
27 |
|
|
|
28 |
|
|
return 1; |
29 |
|
|
} |
30 |
|
|
|
31 |
douglas |
34 |
char *shells_[custom]; |
32 |
douglas |
1 |
|
33 |
douglas |
34 |
for (Shell shell = sh; shell != _3sh; ++shell) |
34 |
|
|
{ |
35 |
|
|
int pipe_[2]; |
36 |
douglas |
1 |
|
37 |
douglas |
34 |
check(pipe(pipe_), environment); |
38 |
douglas |
1 |
|
39 |
douglas |
34 |
pid_t which; |
40 |
douglas |
1 |
|
41 |
douglas |
34 |
if (!(which = check(fork(), environment))) |
42 |
|
|
{ |
43 |
|
|
check(dup2(pipe_[1], 1), environment); |
44 |
|
|
check(close(pipe_[0]), environment); |
45 |
|
|
check(execlp("which", argv[0], shells[shell].name, NULL), environment); |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
check(close(pipe_[1]), environment); |
49 |
|
|
|
50 |
|
|
FILE *which_ = fdopen(pipe_[0], "r"); |
51 |
|
|
size_t size; |
52 |
|
|
char *shell_ = fcheck(fgetln(which_, &size), which_, environment); |
53 |
|
|
int status; |
54 |
|
|
|
55 |
|
|
check(waitpid(which, &status, 0), environment); |
56 |
|
|
|
57 |
|
|
if (WEXITSTATUS(status)) |
58 |
|
|
{ |
59 |
|
|
char *error = malloc(strlen(shells[shell].name) + 18); |
60 |
|
|
|
61 |
|
|
sprintf(error, "Shell not found: %s", shells[shell].name); |
62 |
|
|
longjmp(environment, (int)error); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
shells_[shell] = malloc(size); |
66 |
|
|
|
67 |
|
|
strlcpy(shells_[shell], shell_, size); |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
bool match; |
71 |
|
|
|
72 |
|
|
# define checkshells(os) \ |
73 |
|
|
match = true; \ |
74 |
|
|
\ |
75 |
|
|
for (Shell shell = sh; shell != _3sh; ++shell) \ |
76 |
|
|
if (strcmp(shells[shell].os, shells_[shell])) \ |
77 |
|
|
{ \ |
78 |
|
|
match = false; \ |
79 |
|
|
\ |
80 |
|
|
break; \ |
81 |
|
|
} \ |
82 |
|
|
\ |
83 |
|
|
if (match) \ |
84 |
|
|
{ \ |
85 |
|
|
printf(#os "\n"); \ |
86 |
|
|
\ |
87 |
|
|
goto on; \ |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
checkshells(freebsd); |
91 |
|
|
checkshells(linux); |
92 |
|
|
checkshells(darwin); |
93 |
|
|
checkshells(solaris); |
94 |
|
|
checkshells(netbsd); |
95 |
|
|
|
96 |
|
|
# undef checkshells |
97 |
|
|
|
98 |
|
|
longjmp(environment, (int)"Unknown shell layout"); |
99 |
|
|
|
100 |
|
|
on: for (Shell shell = sh; shell != _3sh; ++shell) |
101 |
|
|
free(shells_[shell]); |
102 |
|
|
|
103 |
douglas |
1 |
return 0; |
104 |
|
|
} |