2 ms·
#include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <dirent.h> #include <string.h> void list(char* dir) { DIR* d; struc
by t-3 3y ago
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
void list(char* dir) {
DIR* d;
struct dirent* dp;
if (!(d = opendir(dir))) return;
while ((dp = readdir(d)) != NULL) {
int namelen = strlen(dp->d_name);
if (dp->d_type == DT_DIR) {
if ((strcmp(dp->d_name, ".") == 0) || (strcmp(dp->d_name, "..") == 0)) continue;
char p[1024];
snprintf(p, sizeof(p), "%s/%s", dir, dp->d_name);
list(p);
}
else
if (((dp->d_name[namelen-1] == '1') ||
(dp->d_name[namelen-1] == '2') ||
(dp->d_name[namelen-1] == '3') ||
(dp->d_name[namelen-1] == '4') ||
(dp->d_name[namelen-1] == '5') ||
(dp->d_name[namelen-1] == '6') ||
(dp->d_name[namelen-1] == '7') ||
(dp->d_name[namelen-1] == '8') ||
(dp->d_name[namelen-1] == '9')) &&
(dp->d_name[namelen-2] == '.'))
{ write(1, dp->d_name, namelen-2); write(1, "\n", 1); }
}
closedir(d);
}
int main() {
char* path = getenv("MANPATH");
char* dir;
for (dir = strsep(&path, ":"); dir != NULL; dir = strsep(&path, ":")) { list(dir); }
return 0;
}
To use it, just compile and place in your path (I have it as lsm):
set -A complete_man -- $(lsm | sort | uniq)
I wrote it by just copying and slightly modifying the example in the opendir/readdir man page, it can probably be improved.
- tedunangst 3y agoConsider using isdigit.