1 /* spell.c -- spelling correction for pathnames. */
3 /* Copyright (C) 2000 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
30 #include <bashtypes.h>
32 #include <posixstat.h>
34 #include <sys/param.h>
43 static int mindist
__P((char *, char *, char *));
44 static int spdist
__P((char *, char *));
47 * `spname' and its helpers are inspired by the code in "The UNIX
48 * Programming Environment", Kernighan & Pike, Prentice-Hall 1984,
53 * `spname' -- return a correctly spelled filename
55 * int spname(char * oldname, char * newname)
56 * Returns: -1 if no reasonable match found
57 * 0 if exact match found
59 * Stores corrected name in `newname'.
62 spname(oldname
, newname
)
67 char guess
[PATH_MAX
+ 1], best
[PATH_MAX
+ 1];
73 while (*op
== '/') /* Skip slashes */
77 if (*op
== '\0') /* Exact or corrected */
79 /* `.' is rarely the right thing. */
80 if (oldname
[1] == '\0' && newname
[1] == '\0' &&
81 oldname
[0] != '.' && newname
[0] == '.')
83 return strcmp(oldname
, newname
) != 0;
86 /* Copy next component into guess */
87 for (p
= guess
; *op
!= '/' && *op
!= '\0'; op
++)
88 if (p
< guess
+ PATH_MAX
)
92 if (mindist(newname
, guess
, best
) >= 3)
93 return -1; /* Hopeless */
96 * Add to end of newname
98 for (p
= best
; *np
= *p
++; np
++)
104 * Search directory for a guess
107 mindist(dir
, guess
, best
)
116 dist
= 3; /* Worst distance */
120 if ((fd
= opendir(dir
)) == NULL
)
123 while ((dp
= readdir(fd
)) != NULL
)
126 * Look for a better guess. If the new guess is as
127 * good as the current one, we take it. This way,
128 * any single character match will be a better match
131 x
= spdist(dp
->d_name
, guess
);
132 if (x
<= dist
&& x
!= 3)
134 strcpy(best
, dp
->d_name
);
136 if (dist
== 0) /* Exact match */
142 /* Don't return `.' */
143 if (best
[0] == '.' && best
[1] == '\0')
149 * `spdist' -- return the "distance" between two names.
151 * int spname(char * oldname, char * newname)
152 * Returns: 0 if strings are identical
153 * 1 if two characters are transposed
154 * 2 if one character is wrong, added or deleted
164 return 0; /* Exact match */
173 if (cur
[1] && new[1] && cur
[0] == new[1] && cur
[1] == new[0] && strcmp (cur
+ 2, new + 2) == 0)
174 return 1; /* Transposition */
176 if (strcmp (cur
+ 1, new + 1) == 0)
177 return 2; /* One character mismatch */
180 if (strcmp(&cur
[1], &new[0]) == 0)
181 return 2; /* Extra character */
184 if (*new && strcmp(cur
, new + 1) == 0)
185 return 2; /* Missing character */
197 n
= (strlen (dirname
) * 3 + 1) / 2 + 1;
198 guess
= (char *)malloc (n
);
202 switch (spname (dirname
, guess
))