fix symlinks

This commit is contained in:
atagen 2025-12-03 20:39:18 +11:00
parent 133d7e7c5b
commit 12dba23421

27
main.c
View file

@ -36,9 +36,9 @@ int main(int argc, char *argv[]) {
}
// init variables
char found_path [PATH_MAX+1];
char found_path[PATH_MAX + 1];
int nargc = 0;
char *args [128];
char *args[128];
// read magic bytes
size_t read = 0;
@ -61,7 +61,7 @@ int main(int argc, char *argv[]) {
args[nargc] = malloc(sizeof(char) * strlen(token));
// avoid copying the magic bytes and newline
char* p = mempcpy(args[nargc], token, strlen(token) - 1);
char *p = mempcpy(args[nargc], token, strlen(token) - 1);
*p = '\0';
token = strtok(NULL, " ");
++nargc;
@ -74,7 +74,6 @@ int main(int argc, char *argv[]) {
}
args[nargc] = script_path;
// search for path env var
char *path_env = getenv("PATH");
if (path_env == NULL) {
@ -86,17 +85,21 @@ int main(int argc, char *argv[]) {
// search each member of PATH for executable with name
struct dirent *entity;
DIR *dir;
char rpath [PATH_MAX+1];
char dirpath[PATH_MAX + 1];
char fullpath[PATH_MAX + 1];
while (token != NULL) {
char* __attribute__((unused)) _ = realpath(token, rpath);
dir = opendir(token);
if (dir != NULL) {
if (realpath(token, dirpath) == NULL) {
continue;
}
if ((dir = opendir(token)) != NULL) {
while ((entity = readdir(dir)) != NULL) {
if (entity->d_type == DT_REG && strcmp(args[0], entity->d_name) == 0) {
throw_on(closedir(dir), -1, EIO);
if (strcmp(args[0], entity->d_name) == 0) {
int len = strlen(token) + strlen(args[0]) + 2;
snprintf(found_path, len, "%s/%s", token, args[0]);
goto end_loop;
snprintf(fullpath, len, "%s/%s", token, args[0]);
if (realpath(fullpath, found_path) != NULL) {
throw_on(closedir(dir), -1, EIO);
goto end_loop;
}
}
}
throw_on(closedir(dir), -1, EIO);