2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include <sys/types.h>
35 /* given a partial pathname, this routine fills in as much of the rest of the
38 * return 0 if ok, -1 if no matches
40 int mutt_complete (char *s, size_t slen)
47 char dirpart[_POSIX_PATH_MAX], exp_dirpart[_POSIX_PATH_MAX];
48 char filepart[_POSIX_PATH_MAX];
50 char imap_path[LONG_STRING];
52 dprint (2, (debugfile, "mutt_complete: completing %s\n", s));
54 /* we can use '/' as a delimiter, imap_complete rewrites it */
55 if (*s == '=' || *s == '+' || *s == '!')
58 p = NONULL (Spoolfile);
62 mutt_concat_path (imap_path, p, s+1, sizeof (imap_path));
65 strfcpy (imap_path, s, sizeof(imap_path));
67 if (mx_is_imap (imap_path))
68 return imap_complete (s, slen, imap_path);
71 if (*s == '=' || *s == '+' || *s == '!')
76 strfcpy (exp_dirpart, NONULL (Spoolfile), sizeof (exp_dirpart));
78 strfcpy (exp_dirpart, NONULL (Maildir), sizeof (exp_dirpart));
79 if ((p = strrchr (s, '/')))
81 char buf[_POSIX_PATH_MAX];
82 if (mutt_concatn_path (buf, sizeof(buf), exp_dirpart, strlen(exp_dirpart), s + 1, (size_t)(p - s - 1)) == NULL) {
85 strfcpy (exp_dirpart, buf, sizeof (exp_dirpart));
86 mutt_substrcpy(dirpart, s, p+1, sizeof(dirpart));
87 strfcpy (filepart, p + 1, sizeof (filepart));
90 strfcpy (filepart, s + 1, sizeof (filepart));
91 dirp = opendir (exp_dirpart);
95 if ((p = strrchr (s, '/')))
97 if (p == s) /* absolute path */
100 strfcpy (dirpart, "/", sizeof (dirpart));
102 strfcpy (filepart, p, sizeof (filepart));
103 dirp = opendir (dirpart);
107 mutt_substrcpy(dirpart, s, p, sizeof(dirpart));
108 strfcpy (filepart, p + 1, sizeof (filepart));
109 strfcpy (exp_dirpart, dirpart, sizeof (exp_dirpart));
110 mutt_expand_path (exp_dirpart, sizeof (exp_dirpart));
111 dirp = opendir (exp_dirpart);
116 /* no directory name, so assume current directory. */
118 strfcpy (filepart, s, sizeof (filepart));
119 dirp = opendir (".");
125 dprint (1, (debugfile, "mutt_complete(): %s: %s (errno %d).\n", exp_dirpart, strerror (errno), errno));
130 * special case to handle when there is no filepart yet. find the first
131 * file/directory which is not ``.'' or ``..''
133 if ((len = mutt_strlen (filepart)) == 0)
135 while ((de = readdir (dirp)) != NULL)
137 if (mutt_strcmp (".", de->d_name) != 0 && mutt_strcmp ("..", de->d_name) != 0)
139 strfcpy (filepart, de->d_name, sizeof (filepart));
146 while ((de = readdir (dirp)) != NULL)
148 if (mutt_strncmp (de->d_name, filepart, len) == 0)
152 for (i=0; filepart[i] && de->d_name[i]; i++)
154 if (filepart[i] != de->d_name[i])
164 char buf[_POSIX_PATH_MAX];
167 strfcpy (filepart, de->d_name, sizeof(filepart));
169 /* check to see if it is a directory */
172 strfcpy (buf, exp_dirpart, sizeof (buf));
173 strfcpy (buf + strlen (buf), "/", sizeof (buf) - strlen (buf));
177 strfcpy (buf + strlen (buf), filepart, sizeof (buf) - strlen (buf));
178 if (stat (buf, &st) != -1 && (st.st_mode & S_IFDIR))
179 strfcpy (filepart + strlen (filepart), "/",
180 sizeof (filepart) - strlen (filepart));
189 strfcpy (s, dirpart, slen);
190 if (mutt_strcmp ("/", dirpart) != 0 && dirpart[0] != '=' && dirpart[0] != '+')
191 strfcpy (s + strlen (s), "/", slen - strlen (s));
192 strfcpy (s + strlen (s), filepart, slen - strlen (s));
195 strfcpy (s, filepart, slen);
197 return (init ? 0 : -1);