-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhich.c
143 lines (124 loc) · 4.29 KB
/
which.c
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
/*
* Copyright (C) 2023 Nicolai Brand (https://lytix.dev)
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "builtin/builtin.h"
#include "interpreter/interpreter.h"
#include "interpreter/value/slash_str.h"
#include "lib/str_view.h"
Builtin builtins[] = { { .name = "cd", .func = builtin_cd },
{ .name = "vars", .func = builtin_vars },
{ .name = "which", .func = builtin_which },
{ .name = "exit", .func = builtin_exit },
{ .name = "read", .func = builtin_read } };
static void which_internal(WhichResult *mutable_result, char *PATH, char *command)
{
/* PATH environment variable is often prefixed with 'PATH=' */
size_t PATH_len = strlen(PATH) + 1;
char *path_cpy = malloc(PATH_len);
memcpy(path_cpy, PATH, PATH_len);
char *single_path = strtok(path_cpy, ":");
while (single_path != NULL) {
DIR *path_dir = opendir(single_path);
struct dirent *candidate_program;
while (path_dir != NULL && (candidate_program = readdir(path_dir)) != NULL) {
struct stat sb;
/* check for name equality */
if (strcmp(command, candidate_program->d_name) == 0)
continue;
snprintf(mutable_result->path, PROGRAM_PATH_MAX_LEN, "%s/%s", single_path, command);
/* check if executable bit is on */
if (stat(mutable_result->path, &sb) == 0 && sb.st_mode & S_IXUSR &&
!S_ISDIR(sb.st_mode)) {
mutable_result->type = WHICH_EXTERN;
free(path_cpy);
closedir(path_dir);
return;
}
/* can not be in this path_dir as file with matching name is not an executable */
break;
}
if (path_dir != NULL)
closedir(path_dir);
single_path = strtok(NULL, ":");
}
free(path_cpy);
mutable_result->type = WHICH_NOT_FOUND;
}
int builtin_which(Interpreter *interpreter, size_t argc, SlashValue *argv)
{
if (argc == 0) {
fprintf(stderr, "which: no argument received");
return 1;
}
SlashValue param = argv[0];
TraitToStr to_str = param.T->to_str;
if (to_str == NULL) {
fprintf(stderr, "which: could not take to_str of type '%s'", param.T->name);
return 1;
}
SlashStr *param_str = AS_STR(to_str(interpreter, param));
ScopeAndValue path = var_get(interpreter->scope, &(StrView){ .view = "PATH", .size = 4 });
if (!IS_STR(*path.value)) {
fprintf(stderr, "which: PATH variable should be type '%s' not '%s'", str_type_info.name,
path.value->T->name);
return 1;
}
WhichResult which_result = which((StrView){ .view = param_str->str, .size = param_str->len },
AS_STR(*path.value)->str);
int return_code = 0;
switch (which_result.type) {
case WHICH_BUILTIN:
printf("%s: slash builtin\n", param_str->str);
break;
case WHICH_EXTERN:
printf("%s\n", which_result.path);
break;
case WHICH_NOT_FOUND:
printf("%s not found\n", param_str->str);
return_code = 1;
break;
}
return return_code;
}
WhichResult which(StrView cmd, char *PATH)
{
char command[cmd.size + 1];
str_view_to_cstr(cmd, command);
WhichResult result = { 0 };
/* edge case: command is a path */
if (command[0] == '/') {
result.type = WHICH_EXTERN;
memcpy(result.path, command,
cmd.size + 1 > PROGRAM_PATH_MAX_LEN ? PROGRAM_PATH_MAX_LEN : cmd.size + 1);
return result;
}
size_t builtins_count = sizeof(builtins) / sizeof(builtins[0]);
for (size_t i = 0; i < builtins_count; i++) {
Builtin builtin = builtins[i];
if (strcmp(builtin.name, command) == 0) {
result.type = WHICH_BUILTIN;
result.builtin = builtin.func;
return result;
}
}
/* execution enters here means cmd is not a builtin */
which_internal(&result, PATH, command);
return result;
}