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 it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if defined (HAVE_UNISTD_H)
25 # include <sys/types.h>
30 #include <bashtypes.h>
32 #include <posixstat.h>
34 #include <sys/param.h>
42 static int mindist (), spdist ();
45 * `spname' and its helpers are inspired by the code in "The UNIX
46 * Programming Environment", Kernighan & Pike, Prentice-Hall 1984,
51 * `spname' -- return a correctly spelled filename
53 * int spname(char * oldname, char * newname)
54 * Returns: -1 if no reasonable match found
55 * 0 if exact match found
57 * Stores corrected name in `newname'.
60 spname(oldname, newname)
65 char guess[PATH_MAX + 1], best[PATH_MAX + 1];
71 while (*op == '/') /* Skip slashes */
75 if (*op == '\0') /* Exact or corrected */
77 /* `.' is rarely the right thing. */
78 if (oldname[1] == '\0' && newname[1] == '\0' &&
79 oldname[0] != '.' && newname[0] == '.')
81 return strcmp(oldname, newname) != 0;
84 /* Copy next component into guess */
85 for (p = guess; *op != '/' && *op != '\0'; op++)
86 if (p < guess + PATH_MAX)
90 if (mindist(newname, guess, best) >= 3)
91 return -1; /* Hopeless */
94 * Add to end of newname
96 for (p = best; *np = *p++; np++)
102 * Search directory for a guess
105 mindist(dir, guess, best)
114 dist = 3; /* Worst distance */
118 if ((fd = opendir(dir)) == NULL)
121 while ((dp = readdir(fd)) != NULL)
124 * Look for a better guess. If the new guess is as
125 * good as the current one, we take it. This way,
126 * any single character match will be a better match
129 x = spdist(dp->d_name, guess);
130 if (x <= dist && x != 3)
132 strcpy(best, dp->d_name);
134 if (dist == 0) /* Exact match */
140 /* Don't return `.' */
141 if (best[0] == '.' && best[1] == '\0')
147 * `spdist' -- return the "distance" between two names.
149 * int spname(char * oldname, char * newname)
150 * Returns: 0 if strings are identical
151 * 1 if two characters are transposed
152 * 2 if one character is wrong, added or deleted
162 return 0; /* Exact match */
171 if (cur[1] && new[1] && cur[0] == new[1] && cur[1] == new[0] && strcmp (cur + 2, new + 2) == 0)
172 return 1; /* Transposition */
174 if (strcmp (cur + 1, new + 1) == 0)
175 return 2; /* One character mismatch */
178 if (strcmp(&cur[1], &new[0]) == 0)
179 return 2; /* Extra character */
182 if (*new && strcmp(cur, new + 1) == 0)
183 return 2; /* Missing character */