#ifndef _GNU_SOURCE # define _GNU_SOURCE #endif #include #include #include #include #include static const char* color_ok_programs[] = { "/usr/bin/lv", "/usr/local/stow/w3m/bin/w3m", }; static int (*libc_isatty)(int fd) = NULL; __attribute__((constructor)) static void init() { libc_isatty = (int (*)(int))dlsym(RTLD_NEXT, "isatty"); } int isatty(int fd) { usleep(1); if (!libc_isatty) return 0; if (libc_isatty(fd)) return 1; char buf[256]; sprintf(buf, "/proc/self/fd/%d", fd); char fdp[256]; int sz = readlink(buf, fdp, 255); if (sz <= 0 || sz == 255) return 0; fdp[sz] = '\0'; if (strncmp(fdp, "pipe:", 5)) return 0; pid_t mypid = getpid(); pid_t mygrp = getpgrp(); DIR* dir = opendir("/proc"); struct dirent* ent; while ((ent = readdir(dir)) != NULL) { int pid = atoi(ent->d_name); if (!pid || pid == mypid) continue; pid_t grp = getpgid(pid); if (grp == mygrp) { char exe[256]; sprintf(buf, "/proc/%d/exe", pid); int sz2 = readlink(buf, exe, 255); if (sz2 <= 0 || sz2 == 255) continue; exe[sz2] = '\0'; //printf("EXE %s=>%s\n", buf, exe); int i; for (i = 0; i < sizeof(color_ok_programs) / sizeof(color_ok_programs[0]); i++) { if (!strcmp(exe, color_ok_programs[i])) { closedir(dir); return 1; } } } } closedir(dir); return 0; }