fix symlinks

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

32
main.c
View file

@ -28,6 +28,7 @@ int main(int argc, char *argv[]) {
exit(EINVAL);
}
int nargc = 0;
// open file
char *script_path = argv[1];
FILE *script = fopen(script_path, "r");
@ -36,9 +37,8 @@ int main(int argc, char *argv[]) {
}
// init variables
char found_path [PATH_MAX+1];
int nargc = 0;
char *args [128];
char found_path[PATH_MAX + 1];
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,21 +85,26 @@ 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) {
goto next_iter;
}
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);
}
next_iter:
token = strtok(NULL, ":");
}
@ -118,7 +122,7 @@ cleanup:
}
}
for (int n = 0; n < nargc; ++n) {
free_if_used(args[nargc]);
free_if_used(args[n]);
}
return exit_code;