2019-07-22 20:32:33 +00:00
|
|
|
#define _XOPEN_SOURCE 500
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdlib.h>
|
2019-09-05 07:24:24 +00:00
|
|
|
extern char *strdup(const char* s);
|
2019-07-22 20:32:33 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "complete.h"
|
|
|
|
|
|
|
|
void completefile(char *file, char *pos, size_t size)
|
|
|
|
{
|
|
|
|
struct dirent *direntp;
|
2019-09-05 07:24:24 +00:00
|
|
|
char *dir, *complete;
|
2019-07-22 20:32:33 +00:00
|
|
|
char next;
|
2019-09-05 07:24:24 +00:00
|
|
|
size_t required;
|
2019-07-22 20:32:33 +00:00
|
|
|
DIR *dirp;
|
|
|
|
|
|
|
|
next = *pos;
|
|
|
|
*pos = 0;
|
|
|
|
|
|
|
|
if (!(complete=strrchr(file,'/'))) {
|
|
|
|
dir = strdup(".");
|
|
|
|
complete = file;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (complete == file) {
|
|
|
|
dir = strdup("/");
|
|
|
|
} else {
|
|
|
|
dir = strdup(file);
|
|
|
|
dir[complete-file] = 0;
|
|
|
|
}
|
|
|
|
complete++;
|
|
|
|
}
|
|
|
|
required = strlen(complete);
|
|
|
|
|
|
|
|
if (!(dirp = opendir(dir))) {
|
|
|
|
free(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((direntp = readdir(dirp))) {
|
2019-09-05 07:24:24 +00:00
|
|
|
if ((direntp->d_name[0] != '.' || complete[0] == '.')
|
|
|
|
&& !strncmp(complete, direntp->d_name, required)) {
|
2019-07-22 20:32:33 +00:00
|
|
|
if (!next && strlen(direntp->d_name) > required) {
|
2019-09-05 07:24:24 +00:00
|
|
|
strncpy(pos, direntp->d_name+required,
|
|
|
|
size - (size_t)(pos-file));
|
2019-07-22 20:32:33 +00:00
|
|
|
file[size-1] = 0;
|
|
|
|
break;
|
|
|
|
} else if (direntp->d_name[required] == next && !strcmp(pos+1, direntp->d_name+required+1)) {
|
|
|
|
next = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
free(dir);
|
|
|
|
}
|