path.c
This file
The walk stopped after 200 commits: lines older than 6a4c416 point at it.
changelog: add missing refDaniel Eklöfbefore adoption6a4c416
lines 1–102why, one step back1#include "path.h"2#include "application.h"3#include "tllist.h"45#include <errno.h>6#include <string.h>7#include <fcntl.h>8#include <sys/types.h>9#include <sys/stat.h>10#include <dirent.h>11#include <stdlib.h>12#include <stdio.h>1314#define LOG_MODULE "path"15#define LOG_ENABLE_DBG 016#include "log.h"17#include "xdg.h"18#include "char32.h"19#include "xmalloc.h"2021void22path_find_programs(struct application_list *applications)23{24 const char *_path = getenv("PATH");2526 if (_path == NULL)27 return ;2829 char *copy = xstrdup(_path);30 char *ctx = NULL;31 tll(struct application *) entries = tll_init();3233 for (const char *tok = strtok_r(copy, ":", &ctx);34 tok != NULL;35 tok = strtok_r(NULL, ":", &ctx))36 {37 DIR *d = opendir(tok);38 if (d == NULL) {39 LOG_WARN("failed to open %s from PATH: %s", tok, strerror(errno));40 continue;41 }42 int fd = dirfd(d);43 if (fd == -1) {44 LOG_WARN("failed to open %s's fd from PATH: %s", tok, strerror(errno));45 closedir(d);46 continue;47 }48 for (const struct dirent *e = readdir(d); e != NULL; e = readdir(d)) {49 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)50 continue;5152 struct stat st;53 if (fstatat(fd, e->d_name, &st, 0) == -1) {54 LOG_WARN("%s: failed to stat: %s", e->d_name, strerror(errno));55 continue;56 }57 if (S_ISREG(st.st_mode) && st.st_mode & S_IXUSR) {58 char32_t *wtitle = ambstoc32(e->d_name);59 if (wtitle == NULL)60 continue;61 bool already_exist = false;62 tll_foreach(entries, it) {63 if (c32cmp(wtitle, it->item->title) == 0) {64 already_exist = true;65 break;66 }67 };68 if (already_exist) {69 free(wtitle);70 continue;71 }7273 char32_t *lowercase = xc32dup(wtitle);74 for (size_t i = 0; i < c32len(lowercase); i++)75 lowercase[i] = toc32lower(lowercase[i]);7677 struct application *app = xmalloc(sizeof(*app));78 *app = (struct application){79 .index = 0, /* Not used in application mode */80 .title = wtitle,81 .title_lowercase = lowercase,82 .title_len = c32len(lowercase),83 .exec = xstrjoin3(tok, "/", e->d_name),84 .visible = true,85 };8687 tll_push_back(entries, app);88 }89 }90 closedir(d);91 }92 free(copy);9394 applications->v = xreallocarray(95 applications->v, applications->count + tll_length(entries), sizeof(applications->v[0]));9697 tll_foreach(entries, it) {98 applications->v[applications->count++] = it->item;99 applications->visible_count++;100 tll_remove(entries, it);101 }102}