suwako / UnixMenu アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
UnixMenu / src / utils.c
2 KB、98行 生 Blame 履歴
1#include "utils.h"
2
3#include <string.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7
8#if defined(__linux__)
9#include <string.h>
10#include <ctype.h>
11#endif
12
13Dup dupn[MAX_NAME_LEN * 2];
14int dupcnt = 0;
15
16#if defined(__linux__)
17#include <string.h>
18
19char *strcasestr(const char *haystack, const char *needle) {
20 size_t needle_len = strlen(needle);
21 if (needle_len == 0) {
22 return (char *)haystack;
23 }
24
25 while (*haystack) {
26 if (strncasecmp(haystack, needle, needle_len) == 0) {
27 return (char *)haystack;
28 }
29 haystack++;
30 }
31 return NULL;
32}
33#endif
34
35int isdup(const char *name) {
36 for (int i = 0; i < dupcnt; i++) {
37 if (strncmp(dupn[i].name, name, strlen(name)) == 0) return 1;
38 }
39
40 return 0;
41}
42
43int isdis(const char *filepath) {
44 FILE *file = fopen(filepath, "r");
45 if (!file) return 0;
46
47 char line[MAX_LINE_LENGTH];
48 while (fgets(line, sizeof(line), file)) {
49 if (strstr(line, "NoDisplay=true")) {
50 fclose(file);
51 return 0;
52 }
53 }
54
55 fclose(file);
56 return 1;
57}
58
59void add_to_dup(const char *name) {
60 if (dupcnt < (MAX_NAME_LEN * 2)) {
61 strncpy(dupn[dupcnt].name, name, MAX_NAME_LEN - 1);
62 dupn[dupcnt].name[MAX_NAME_LEN - 1] = '\0';
63 dupcnt++;
64 }
65}
66
67int launch_path(const char *exec) {
68 char *path = getenv("PATH");
69 if (!path) return 1;
70
71 char *pdup = strdup(path);
72 char *dir = strtok(pdup, ":");
73 while (dir != NULL) {
74 char fp[1024];
75 snprintf(fp, sizeof(fp), "%s/%s", dir, exec);
76
77 if (access(fp, X_OK) == 0) {
78 free(pdup);
79 return 1;
80 }
81 dir = strtok(NULL, ":");
82 }
83
84 free(pdup);
85 return 0;
86}
87
88void cleanup(Display *display, Window window, GC gc, XftColor *color,
89 XftColor *selcolor, XftDraw *draw, XftFont *font,
90 Colormap colormap, Visual *visual) {
91 if (color) XftColorFree(display, visual, colormap, color);
92 if (selcolor) XftColorFree(display, visual, colormap, selcolor);
93 if (font) XftFontClose(display, font);
94 if (draw) XftDrawDestroy(draw);
95 if (gc) XFreeGC(display, gc);
96 if (window) XDestroyWindow(display, window);
97 if (display) XCloseDisplay(display);
98}