Introduce git_etc_gitconfig() that encapsulates access of ETC_GITCONFIG.
[git/mingw.git] / config.c
blob79c32a0338286ebd90dd2c312ad14b90e61dbb0d
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
7 */
8 #include "cache.h"
9 #include "exec_cmd.h"
11 #define MAXNAME (256)
13 static FILE *config_file;
14 static const char *config_file_name;
15 static int config_linenr;
16 static int get_next_char(void)
18 int c;
19 FILE *f;
21 c = '\n';
22 if ((f = config_file) != NULL) {
23 c = fgetc(f);
24 if (c == '\r') {
25 /* DOS like systems */
26 c = fgetc(f);
27 if (c != '\n') {
28 ungetc(c, f);
29 c = '\r';
32 if (c == '\n')
33 config_linenr++;
34 if (c == EOF) {
35 config_file = NULL;
36 c = '\n';
39 return c;
42 static char *parse_value(void)
44 static char value[1024];
45 int quote = 0, comment = 0, len = 0, space = 0;
47 for (;;) {
48 int c = get_next_char();
49 if (len >= sizeof(value))
50 return NULL;
51 if (c == '\n') {
52 if (quote)
53 return NULL;
54 value[len] = 0;
55 return value;
57 if (comment)
58 continue;
59 if (isspace(c) && !quote) {
60 space = 1;
61 continue;
63 if (!quote) {
64 if (c == ';' || c == '#') {
65 comment = 1;
66 continue;
69 if (space) {
70 if (len)
71 value[len++] = ' ';
72 space = 0;
74 if (c == '\\') {
75 c = get_next_char();
76 switch (c) {
77 case '\n':
78 continue;
79 case 't':
80 c = '\t';
81 break;
82 case 'b':
83 c = '\b';
84 break;
85 case 'n':
86 c = '\n';
87 break;
88 /* Some characters escape as themselves */
89 case '\\': case '"':
90 break;
91 /* Reject unknown escape sequences */
92 default:
93 return NULL;
95 value[len++] = c;
96 continue;
98 if (c == '"') {
99 quote = 1-quote;
100 continue;
102 value[len++] = c;
106 static inline int iskeychar(int c)
108 return isalnum(c) || c == '-';
111 static int get_value(config_fn_t fn, char *name, unsigned int len)
113 int c;
114 char *value;
116 /* Get the full name */
117 for (;;) {
118 c = get_next_char();
119 if (c == EOF)
120 break;
121 if (!iskeychar(c))
122 break;
123 name[len++] = tolower(c);
124 if (len >= MAXNAME)
125 return -1;
127 name[len] = 0;
128 while (c == ' ' || c == '\t')
129 c = get_next_char();
131 value = NULL;
132 if (c != '\n') {
133 if (c != '=')
134 return -1;
135 value = parse_value();
136 if (!value)
137 return -1;
139 return fn(name, value);
142 static int get_extended_base_var(char *name, int baselen, int c)
144 do {
145 if (c == '\n')
146 return -1;
147 c = get_next_char();
148 } while (isspace(c));
150 /* We require the format to be '[base "extension"]' */
151 if (c != '"')
152 return -1;
153 name[baselen++] = '.';
155 for (;;) {
156 int c = get_next_char();
157 if (c == '\n')
158 return -1;
159 if (c == '"')
160 break;
161 if (c == '\\') {
162 c = get_next_char();
163 if (c == '\n')
164 return -1;
166 name[baselen++] = c;
167 if (baselen > MAXNAME / 2)
168 return -1;
171 /* Final ']' */
172 if (get_next_char() != ']')
173 return -1;
174 return baselen;
177 static int get_base_var(char *name)
179 int baselen = 0;
181 for (;;) {
182 int c = get_next_char();
183 if (c == EOF)
184 return -1;
185 if (c == ']')
186 return baselen;
187 if (isspace(c))
188 return get_extended_base_var(name, baselen, c);
189 if (!iskeychar(c) && c != '.')
190 return -1;
191 if (baselen > MAXNAME / 2)
192 return -1;
193 name[baselen++] = tolower(c);
197 static int git_parse_file(config_fn_t fn)
199 int comment = 0;
200 int baselen = 0;
201 static char var[MAXNAME];
203 for (;;) {
204 int c = get_next_char();
205 if (c == '\n') {
206 /* EOF? */
207 if (!config_file)
208 return 0;
209 comment = 0;
210 continue;
212 if (comment || isspace(c))
213 continue;
214 if (c == '#' || c == ';') {
215 comment = 1;
216 continue;
218 if (c == '[') {
219 baselen = get_base_var(var);
220 if (baselen <= 0)
221 break;
222 var[baselen++] = '.';
223 var[baselen] = 0;
224 continue;
226 if (!isalpha(c))
227 break;
228 var[baselen] = tolower(c);
229 if (get_value(fn, var, baselen+1) < 0)
230 break;
232 die("bad config file line %d in %s", config_linenr, config_file_name);
235 int git_config_int(const char *name, const char *value)
237 if (value && *value) {
238 char *end;
239 int val = strtol(value, &end, 0);
240 if (!*end)
241 return val;
242 if (!strcasecmp(end, "k"))
243 return val * 1024;
244 if (!strcasecmp(end, "m"))
245 return val * 1024 * 1024;
246 if (!strcasecmp(end, "g"))
247 return val * 1024 * 1024 * 1024;
249 die("bad config value for '%s' in %s", name, config_file_name);
252 int git_config_bool(const char *name, const char *value)
254 if (!value)
255 return 1;
256 if (!*value)
257 return 0;
258 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
259 return 1;
260 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
261 return 0;
262 return git_config_int(name, value) != 0;
265 int git_default_config(const char *var, const char *value)
267 /* This needs a better name */
268 if (!strcmp(var, "core.filemode")) {
269 trust_executable_bit = git_config_bool(var, value);
270 return 0;
273 if (!strcmp(var, "core.symlinks")) {
274 has_symlinks = git_config_bool(var, value);
275 return 0;
278 if (!strcmp(var, "core.bare")) {
279 is_bare_repository_cfg = git_config_bool(var, value);
280 return 0;
283 if (!strcmp(var, "core.ignorestat")) {
284 assume_unchanged = git_config_bool(var, value);
285 return 0;
288 if (!strcmp(var, "core.prefersymlinkrefs")) {
289 prefer_symlink_refs = git_config_bool(var, value);
290 return 0;
293 if (!strcmp(var, "core.logallrefupdates")) {
294 log_all_ref_updates = git_config_bool(var, value);
295 return 0;
298 if (!strcmp(var, "core.warnambiguousrefs")) {
299 warn_ambiguous_refs = git_config_bool(var, value);
300 return 0;
303 if (!strcmp(var, "core.legacyheaders")) {
304 use_legacy_headers = git_config_bool(var, value);
305 return 0;
308 if (!strcmp(var, "core.compression")) {
309 int level = git_config_int(var, value);
310 if (level == -1)
311 level = Z_DEFAULT_COMPRESSION;
312 else if (level < 0 || level > Z_BEST_COMPRESSION)
313 die("bad zlib compression level %d", level);
314 zlib_compression_level = level;
315 return 0;
318 if (!strcmp(var, "core.packedgitwindowsize")) {
319 int pgsz_x2 = getpagesize() * 2;
320 packed_git_window_size = git_config_int(var, value);
322 /* This value must be multiple of (pagesize * 2) */
323 packed_git_window_size /= pgsz_x2;
324 if (packed_git_window_size < 1)
325 packed_git_window_size = 1;
326 packed_git_window_size *= pgsz_x2;
327 return 0;
330 if (!strcmp(var, "core.packedgitlimit")) {
331 packed_git_limit = git_config_int(var, value);
332 return 0;
335 if (!strcmp(var, "core.deltabasecachelimit")) {
336 delta_base_cache_limit = git_config_int(var, value);
337 return 0;
340 if (!strcmp(var, "core.autocrlf")) {
341 if (value && !strcasecmp(value, "input")) {
342 auto_crlf = -1;
343 return 0;
345 auto_crlf = git_config_bool(var, value);
346 return 0;
349 if (!strcmp(var, "user.name")) {
350 strlcpy(git_default_name, value, sizeof(git_default_name));
351 return 0;
354 if (!strcmp(var, "user.email")) {
355 strlcpy(git_default_email, value, sizeof(git_default_email));
356 return 0;
359 if (!strcmp(var, "i18n.commitencoding")) {
360 git_commit_encoding = xstrdup(value);
361 return 0;
364 if (!strcmp(var, "i18n.logoutputencoding")) {
365 git_log_output_encoding = xstrdup(value);
366 return 0;
370 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
371 pager_use_color = git_config_bool(var,value);
372 return 0;
375 /* Add other config variables here and to Documentation/config.txt. */
376 return 0;
379 int git_config_from_file(config_fn_t fn, const char *filename)
381 int ret;
382 FILE *f = fopen(filename, "r");
384 ret = -1;
385 if (f) {
386 config_file = f;
387 config_file_name = filename;
388 config_linenr = 1;
389 ret = git_parse_file(fn);
390 fclose(f);
391 config_file_name = NULL;
393 return ret;
396 const char *git_etc_gitconfig(void)
398 return ETC_GITCONFIG;
401 int git_config(config_fn_t fn)
403 int ret = 0;
404 char *repo_config = NULL;
405 const char *home = NULL, *filename;
407 /* $GIT_CONFIG makes git read _only_ the given config file,
408 * $GIT_CONFIG_LOCAL will make it process it in addition to the
409 * global config file, the same way it would the per-repository
410 * config file otherwise. */
411 filename = getenv(CONFIG_ENVIRONMENT);
412 if (!filename) {
413 if (!access(git_etc_gitconfig(), R_OK))
414 ret += git_config_from_file(fn, git_etc_gitconfig());
415 home = getenv("HOME");
416 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
417 if (!filename)
418 filename = repo_config = xstrdup(git_path("config"));
421 if (home) {
422 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
423 if (!access(user_config, R_OK))
424 ret = git_config_from_file(fn, user_config);
425 free(user_config);
428 ret += git_config_from_file(fn, filename);
429 free(repo_config);
430 return ret;
434 * Find all the stuff for git_config_set() below.
437 #define MAX_MATCHES 512
439 static struct {
440 int baselen;
441 char* key;
442 int do_not_match;
443 regex_t* value_regex;
444 int multi_replace;
445 size_t offset[MAX_MATCHES];
446 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
447 int seen;
448 } store;
450 static int matches(const char* key, const char* value)
452 return !strcmp(key, store.key) &&
453 (store.value_regex == NULL ||
454 (store.do_not_match ^
455 !regexec(store.value_regex, value, 0, NULL, 0)));
458 static int store_aux(const char* key, const char* value)
460 const char *ep;
461 size_t section_len;
463 switch (store.state) {
464 case KEY_SEEN:
465 if (matches(key, value)) {
466 if (store.seen == 1 && store.multi_replace == 0) {
467 fprintf(stderr,
468 "Warning: %s has multiple values\n",
469 key);
470 } else if (store.seen >= MAX_MATCHES) {
471 fprintf(stderr, "Too many matches\n");
472 return 1;
475 store.offset[store.seen] = ftell(config_file);
476 store.seen++;
478 break;
479 case SECTION_SEEN:
481 * What we are looking for is in store.key (both
482 * section and var), and its section part is baselen
483 * long. We found key (again, both section and var).
484 * We would want to know if this key is in the same
485 * section as what we are looking for. We already
486 * know we are in the same section as what should
487 * hold store.key.
489 ep = strrchr(key, '.');
490 section_len = ep - key;
492 if ((section_len != store.baselen) ||
493 memcmp(key, store.key, section_len+1)) {
494 store.state = SECTION_END_SEEN;
495 break;
499 * Do not increment matches: this is no match, but we
500 * just made sure we are in the desired section.
502 store.offset[store.seen] = ftell(config_file);
503 /* fallthru */
504 case SECTION_END_SEEN:
505 case START:
506 if (matches(key, value)) {
507 store.offset[store.seen] = ftell(config_file);
508 store.state = KEY_SEEN;
509 store.seen++;
510 } else {
511 if (strrchr(key, '.') - key == store.baselen &&
512 !strncmp(key, store.key, store.baselen)) {
513 store.state = SECTION_SEEN;
514 store.offset[store.seen] = ftell(config_file);
518 return 0;
521 static int write_error()
523 fprintf(stderr, "Failed to write new configuration file\n");
525 /* Same error code as "failed to rename". */
526 return 4;
529 static int store_write_section(int fd, const char* key)
531 const char *dot = strchr(key, '.');
532 int len1 = store.baselen, len2 = -1;
534 dot = strchr(key, '.');
535 if (dot) {
536 int dotlen = dot - key;
537 if (dotlen < len1) {
538 len2 = len1 - dotlen - 1;
539 len1 = dotlen;
543 if (write_in_full(fd, "[", 1) != 1 ||
544 write_in_full(fd, key, len1) != len1)
545 return 0;
546 if (len2 >= 0) {
547 if (write_in_full(fd, " \"", 2) != 2)
548 return 0;
549 while (--len2 >= 0) {
550 unsigned char c = *++dot;
551 if (c == '"')
552 if (write_in_full(fd, "\\", 1) != 1)
553 return 0;
554 if (write_in_full(fd, &c, 1) != 1)
555 return 0;
557 if (write_in_full(fd, "\"", 1) != 1)
558 return 0;
560 if (write_in_full(fd, "]\n", 2) != 2)
561 return 0;
563 return 1;
566 static int store_write_pair(int fd, const char* key, const char* value)
568 int i;
569 int length = strlen(key+store.baselen+1);
570 int quote = 0;
572 /* Check to see if the value needs to be quoted. */
573 if (value[0] == ' ')
574 quote = 1;
575 for (i = 0; value[i]; i++)
576 if (value[i] == ';' || value[i] == '#')
577 quote = 1;
578 if (value[i-1] == ' ')
579 quote = 1;
581 if (write_in_full(fd, "\t", 1) != 1 ||
582 write_in_full(fd, key+store.baselen+1, length) != length ||
583 write_in_full(fd, " = ", 3) != 3)
584 return 0;
585 if (quote && write_in_full(fd, "\"", 1) != 1)
586 return 0;
587 for (i = 0; value[i]; i++)
588 switch (value[i]) {
589 case '\n':
590 if (write_in_full(fd, "\\n", 2) != 2)
591 return 0;
592 break;
593 case '\t':
594 if (write_in_full(fd, "\\t", 2) != 2)
595 return 0;
596 break;
597 case '"':
598 case '\\':
599 if (write_in_full(fd, "\\", 1) != 1)
600 return 0;
601 default:
602 if (write_in_full(fd, value+i, 1) != 1)
603 return 0;
604 break;
606 if (quote && write_in_full(fd, "\"", 1) != 1)
607 return 0;
608 if (write_in_full(fd, "\n", 1) != 1)
609 return 0;
610 return 1;
613 static ssize_t find_beginning_of_line(const char* contents, size_t size,
614 size_t offset_, int* found_bracket)
616 size_t equal_offset = size, bracket_offset = size;
617 ssize_t offset;
619 for (offset = offset_-2; offset > 0
620 && contents[offset] != '\n'; offset--)
621 switch (contents[offset]) {
622 case '=': equal_offset = offset; break;
623 case ']': bracket_offset = offset; break;
625 if (bracket_offset < equal_offset) {
626 *found_bracket = 1;
627 offset = bracket_offset+1;
628 } else
629 offset++;
631 return offset;
634 int git_config_set(const char* key, const char* value)
636 return git_config_set_multivar(key, value, NULL, 0);
640 * If value==NULL, unset in (remove from) config,
641 * if value_regex!=NULL, disregard key/value pairs where value does not match.
642 * if multi_replace==0, nothing, or only one matching key/value is replaced,
643 * else all matching key/values (regardless how many) are removed,
644 * before the new pair is written.
646 * Returns 0 on success.
648 * This function does this:
650 * - it locks the config file by creating ".git/config.lock"
652 * - it then parses the config using store_aux() as validator to find
653 * the position on the key/value pair to replace. If it is to be unset,
654 * it must be found exactly once.
656 * - the config file is mmap()ed and the part before the match (if any) is
657 * written to the lock file, then the changed part and the rest.
659 * - the config file is removed and the lock file rename()d to it.
662 int git_config_set_multivar(const char* key, const char* value,
663 const char* value_regex, int multi_replace)
665 int i, dot;
666 int fd = -1, in_fd;
667 int ret;
668 char* config_filename;
669 char* lock_file;
670 const char* last_dot = strrchr(key, '.');
672 config_filename = getenv(CONFIG_ENVIRONMENT);
673 if (!config_filename) {
674 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
675 if (!config_filename)
676 config_filename = git_path("config");
678 config_filename = xstrdup(config_filename);
679 lock_file = xstrdup(mkpath("%s.lock", config_filename));
682 * Since "key" actually contains the section name and the real
683 * key name separated by a dot, we have to know where the dot is.
686 if (last_dot == NULL) {
687 fprintf(stderr, "key does not contain a section: %s\n", key);
688 ret = 2;
689 goto out_free;
691 store.baselen = last_dot - key;
693 store.multi_replace = multi_replace;
696 * Validate the key and while at it, lower case it for matching.
698 store.key = xmalloc(strlen(key) + 1);
699 dot = 0;
700 for (i = 0; key[i]; i++) {
701 unsigned char c = key[i];
702 if (c == '.')
703 dot = 1;
704 /* Leave the extended basename untouched.. */
705 if (!dot || i > store.baselen) {
706 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
707 fprintf(stderr, "invalid key: %s\n", key);
708 free(store.key);
709 ret = 1;
710 goto out_free;
712 c = tolower(c);
713 } else if (c == '\n') {
714 fprintf(stderr, "invalid key (newline): %s\n", key);
715 free(store.key);
716 ret = 1;
717 goto out_free;
719 store.key[i] = c;
721 store.key[i] = 0;
724 * The lock_file serves a purpose in addition to locking: the new
725 * contents of .git/config will be written into it.
727 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
728 if (fd < 0 || adjust_shared_perm(lock_file)) {
729 fprintf(stderr, "could not lock config file\n");
730 free(store.key);
731 ret = -1;
732 goto out_free;
736 * If .git/config does not exist yet, write a minimal version.
738 in_fd = open(config_filename, O_RDONLY);
739 if ( in_fd < 0 ) {
740 free(store.key);
742 if ( ENOENT != errno ) {
743 error("opening %s: %s", config_filename,
744 strerror(errno));
745 ret = 3; /* same as "invalid config file" */
746 goto out_free;
748 /* if nothing to unset, error out */
749 if (value == NULL) {
750 ret = 5;
751 goto out_free;
754 store.key = (char*)key;
755 if (!store_write_section(fd, key) ||
756 !store_write_pair(fd, key, value))
757 goto write_err_out;
758 } else {
759 struct stat st;
760 char* contents;
761 size_t contents_sz, copy_begin, copy_end;
762 int i, new_line = 0;
764 if (value_regex == NULL)
765 store.value_regex = NULL;
766 else {
767 if (value_regex[0] == '!') {
768 store.do_not_match = 1;
769 value_regex++;
770 } else
771 store.do_not_match = 0;
773 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
774 if (regcomp(store.value_regex, value_regex,
775 REG_EXTENDED)) {
776 fprintf(stderr, "Invalid pattern: %s\n",
777 value_regex);
778 free(store.value_regex);
779 ret = 6;
780 goto out_free;
784 store.offset[0] = 0;
785 store.state = START;
786 store.seen = 0;
789 * After this, store.offset will contain the *end* offset
790 * of the last match, or remain at 0 if no match was found.
791 * As a side effect, we make sure to transform only a valid
792 * existing config file.
794 if (git_config_from_file(store_aux, config_filename)) {
795 fprintf(stderr, "invalid config file\n");
796 free(store.key);
797 if (store.value_regex != NULL) {
798 regfree(store.value_regex);
799 free(store.value_regex);
801 ret = 3;
802 goto out_free;
805 free(store.key);
806 if (store.value_regex != NULL) {
807 regfree(store.value_regex);
808 free(store.value_regex);
811 /* if nothing to unset, or too many matches, error out */
812 if ((store.seen == 0 && value == NULL) ||
813 (store.seen > 1 && multi_replace == 0)) {
814 ret = 5;
815 goto out_free;
818 fstat(in_fd, &st);
819 contents_sz = xsize_t(st.st_size);
820 contents = xmmap(NULL, contents_sz, PROT_READ,
821 MAP_PRIVATE, in_fd, 0);
822 close(in_fd);
824 if (store.seen == 0)
825 store.seen = 1;
827 for (i = 0, copy_begin = 0; i < store.seen; i++) {
828 if (store.offset[i] == 0) {
829 store.offset[i] = copy_end = contents_sz;
830 } else if (store.state != KEY_SEEN) {
831 copy_end = store.offset[i];
832 } else
833 copy_end = find_beginning_of_line(
834 contents, contents_sz,
835 store.offset[i]-2, &new_line);
837 /* write the first part of the config */
838 if (copy_end > copy_begin) {
839 if (write_in_full(fd, contents + copy_begin,
840 copy_end - copy_begin) <
841 copy_end - copy_begin)
842 goto write_err_out;
843 if (new_line &&
844 write_in_full(fd, "\n", 1) != 1)
845 goto write_err_out;
847 copy_begin = store.offset[i];
850 /* write the pair (value == NULL means unset) */
851 if (value != NULL) {
852 if (store.state == START) {
853 if (!store_write_section(fd, key))
854 goto write_err_out;
856 if (!store_write_pair(fd, key, value))
857 goto write_err_out;
860 /* write the rest of the config */
861 if (copy_begin < contents_sz)
862 if (write_in_full(fd, contents + copy_begin,
863 contents_sz - copy_begin) <
864 contents_sz - copy_begin)
865 goto write_err_out;
867 munmap(contents, contents_sz);
868 unlink(config_filename);
871 close(fd);
872 fd = -1;
873 if (rename(lock_file, config_filename) < 0) {
874 fprintf(stderr, "Could not rename the lock file?\n");
875 ret = 4;
876 goto out_free;
879 ret = 0;
881 out_free:
882 if (0 <= fd)
883 close(fd);
884 free(config_filename);
885 if (lock_file) {
886 unlink(lock_file);
887 free(lock_file);
889 return ret;
891 write_err_out:
892 ret = write_error();
893 goto out_free;
897 static int section_name_match (const char *buf, const char *name)
899 int i = 0, j = 0, dot = 0;
900 for (; buf[i] && buf[i] != ']'; i++) {
901 if (!dot && isspace(buf[i])) {
902 dot = 1;
903 if (name[j++] != '.')
904 break;
905 for (i++; isspace(buf[i]); i++)
906 ; /* do nothing */
907 if (buf[i] != '"')
908 break;
909 continue;
911 if (buf[i] == '\\' && dot)
912 i++;
913 else if (buf[i] == '"' && dot) {
914 for (i++; isspace(buf[i]); i++)
915 ; /* do_nothing */
916 break;
918 if (buf[i] != name[j++])
919 break;
921 return (buf[i] == ']' && name[j] == 0);
924 /* if new_name == NULL, the section is removed instead */
925 int git_config_rename_section(const char *old_name, const char *new_name)
927 int ret = 0, remove = 0;
928 char *config_filename;
929 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
930 int out_fd;
931 char buf[1024];
933 config_filename = getenv(CONFIG_ENVIRONMENT);
934 if (!config_filename) {
935 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
936 if (!config_filename)
937 config_filename = git_path("config");
939 config_filename = xstrdup(config_filename);
940 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
941 if (out_fd < 0) {
942 ret = error("Could not lock config file!");
943 goto out;
946 if (!(config_file = fopen(config_filename, "rb"))) {
947 /* no config file means nothing to rename, no error */
948 goto unlock_and_out;
951 while (fgets(buf, sizeof(buf), config_file)) {
952 int i;
953 int length;
954 for (i = 0; buf[i] && isspace(buf[i]); i++)
955 ; /* do nothing */
956 if (buf[i] == '[') {
957 /* it's a section */
958 if (section_name_match (&buf[i+1], old_name)) {
959 ret++;
960 if (new_name == NULL) {
961 remove = 1;
962 continue;
964 store.baselen = strlen(new_name);
965 if (!store_write_section(out_fd, new_name)) {
966 ret = write_error();
967 goto out;
969 continue;
971 remove = 0;
973 if (remove)
974 continue;
975 length = strlen(buf);
976 if (write_in_full(out_fd, buf, length) != length) {
977 ret = write_error();
978 goto out;
981 fclose(config_file);
982 unlock_and_out:
983 if (close(out_fd) || commit_lock_file(lock) < 0)
984 ret = error("Cannot commit config file!");
985 out:
986 free(config_filename);
987 return ret;