47 #if defined(NEED_STRDUP)
51 char * strdup(
char *p)
55 if ((
new = NEW(
char, strlen(p) + 1)) != NULL)
64 STATIC
int compare(CONST
void *p1,CONST
void *p2)
69 v1 = (CONST
char **)p1;
70 v2 = (CONST
char **)p2;
71 return strcmp(*v1, *v2);
78 STATIC
int FindMatches(
char *dir,
char *file,
char ***avp)
80 #if !defined(SYSTEM_IS_WIN32)
89 if ((dp = opendir(dir)) == NULL)
95 while ((ep = readdir(dp)) != NULL) {
97 if (p[0] ==
'.' && (p[1] ==
'\0' || (p[1] ==
'.' && p[2] ==
'\0')))
99 if (len && strncmp(p, file, len) != 0)
102 if ((ac % MEM_INC) == 0) {
103 if ((neww = NEW(
char*, ac + MEM_INC)) == NULL)
106 COPYFROMTO(neww, av, ac *
sizeof (
char **));
112 if ((av[ac] = STRDUP(p)) == NULL) {
123 qsort(av, ac,
sizeof (
char **), compare);
134 STATIC
int SplitPath(
char *path,
char **dirpart,
char **filepart)
136 static char DOT[] =
".";
140 if ((fpart = strrchr(path,
'/')) == NULL) {
141 if ((dpart = STRDUP(DOT)) == NULL)
143 if ((fpart = STRDUP(path)) == NULL) {
149 if ((dpart = STRDUP(path)) == NULL)
151 dpart[fpart - path] =
'\0';
152 if ((fpart = STRDUP(++fpart)) == NULL) {
156 if (dpart[0] ==
'\0')
171 char *rl_complete(
char *pathname,
int *unique)
184 if (SplitPath(pathname, &dir, &file) < 0)
186 if ((ac = FindMatches(dir, file, &av)) == 0) {
197 j = strlen(av[0]) - len + 2;
198 if ((p = NEW(
char, j + 1)) != NULL) {
199 COPYFROMTO(p, av[0] + len, j);
200 if ((neww = NEW(
char, strlen(dir) + strlen(av[0]) + 2)) != NULL) {
201 (void)strcpy(neww, dir);
202 (void)strcat(neww,
"/");
203 (void)strcat(neww, av[0]);
204 rl_add_slash(neww, p);
213 for (i = len, end = strlen(av[0]); i < end; i++)
214 for (j = 1; j < ac; j++)
215 if (av[0][i] != av[j][i])
220 if ((p = NEW(
char, j)) != NULL) {
221 COPYFROMTO(p, av[0] + len, j);
231 for (i = 0; i < ac; i++)
240 int rl_list_possib(
char *pathname,
char ***avp)
243 char *file, *path, *tt;
246 if (SplitPath(pathname, &dir, &file) < 0)
248 ac = FindMatches(dir, file, avp);
250 for (i = 0; i < ac; i++)
252 path = walloc(
char,strlen(dir)+strlen((*avp)[i])+3);
253 sprintf(path,
"%s/%s",dir,(*avp)[i]);
254 if (el_is_directory(path))
256 tt = walloc(
char,strlen((*avp)[i])+2);
257 sprintf(tt,
"%s/",(*avp)[i]);