config.c: Tolerate UTF8 BOM at the beginning of config file
Unfortunately, the abomination of Windows Notepad likes to scatted non-sensical UTF8 BOM marks across text files it edits. This is especially troublesome when editing the Git configuration file, and it does not appear to be particularly harmful to teach Git to deal with this poo in the configfile. Signed-off-by: Petr Baudis <petr.baudis@novartis.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Petr Baudis committed
Oct 1, 2008 at 22:13 UTC
de056402fd90dbb1dc47756ef7cbcc9d230a5eb8
1 file changed
+19
config.c
+19
@@ -205,8 +205,27 @@ static int git_parse_file(config_fn_t fn, void *data)
205
int baselen = 0;
206
static char var[MAXNAME];
207
208
+ /* U+FEFF Byte Order Mark in UTF8 */
209
+ static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
210
+ const unsigned char *bomptr = utf8_bom;
211
+
212
for (;;) {
213
int c = get_next_char();
214
+ if (bomptr && *bomptr) {
215
+ /* We are at the file beginning; skip UTF8-encoded BOM
216
+ * if present. Sane editors won't put this in on their
217
+ * own, but e.g. Windows Notepad will do it happily. */
218
+ if ((unsigned char) c == *bomptr) {
219
+ bomptr++;
220
+ continue;
221
+ } else {
222
+ /* Do not tolerate partial BOM. */
223
+ if (bomptr != utf8_bom)
224
+ break;
225
+ /* No BOM at file beginning. Cool. */
226
+ bomptr = NULL;
227
+ }
228
+ }
229
if (c == '\n') {
230
if (config_file_eof)
231
return 0;