summaryrefslogtreecommitdiffstats
path: root/pw/pw_conf.c
diff options
context:
space:
mode:
authorBaptiste Daroussin <bapt@FreeBSD.org>2015-05-09 21:53:33 +0000
committerBaptiste Daroussin <bapt@FreeBSD.org>2015-05-09 21:53:33 +0000
commit606bdd6043d9b36dddeeeee8d422b1487fd0ef4e (patch)
tree5e436b4e03115dd7978d78b1879f7570287a6478 /pw/pw_conf.c
parent8f8f1acc5d6c14c716658ea62ce5c631f52fc15b (diff)
downloadpw-darwin-606bdd6043d9b36dddeeeee8d422b1487fd0ef4e.tar.gz
pw-darwin-606bdd6043d9b36dddeeeee8d422b1487fd0ef4e.tar.zst
pw-darwin-606bdd6043d9b36dddeeeee8d422b1487fd0ef4e.zip
Return from the function as early as possible
This reduces the depth of the if statements and improves clarity of the code
Diffstat (limited to 'pw/pw_conf.c')
-rw-r--r--pw/pw_conf.c233
1 files changed, 116 insertions, 117 deletions
diff --git a/pw/pw_conf.c b/pw/pw_conf.c
index 9dce918..4e97fd9 100644
--- a/pw/pw_conf.c
+++ b/pw/pw_conf.c
@@ -368,134 +368,133 @@ int
write_userconfig(char const * file)
{
int fd;
+ int i, j;
struct sbuf *buf;
+ FILE *fp;
if (file == NULL)
file = _PATH_PW_CONF;
- if ((fd = open(file, O_CREAT | O_RDWR | O_TRUNC | O_EXLOCK, 0644)) != -1) {
- FILE *fp;
+ if ((fd = open(file, O_CREAT|O_RDWR|O_TRUNC|O_EXLOCK, 0644)) == -1)
+ return (0);
- if ((fp = fdopen(fd, "w")) == NULL)
- close(fd);
- else {
- int i, j;
+ if ((fp = fdopen(fd, "w")) == NULL) {
+ close(fd);
+ return (0);
+ }
- buf = sbuf_new_auto();
- for (i = _UC_NONE; i < _UC_FIELDS; i++) {
- int quote = 1;
-
- sbuf_clear(buf);
- switch (i) {
- case _UC_DEFAULTPWD:
- sbuf_cat(buf, boolean_str(config.default_password));
- break;
- case _UC_REUSEUID:
- sbuf_cat(buf, boolean_str(config.reuse_uids));
- break;
- case _UC_REUSEGID:
- sbuf_cat(buf, boolean_str(config.reuse_gids));
- break;
- case _UC_NISPASSWD:
- sbuf_cat(buf, config.nispasswd ?
- config.nispasswd : "");
- quote = 0;
- break;
- case _UC_DOTDIR:
- sbuf_cat(buf, config.dotdir ?
- config.dotdir : boolean_str(0));
- break;
- case _UC_NEWMAIL:
- sbuf_cat(buf, config.newmail ?
- config.newmail : boolean_str(0));
- break;
- case _UC_LOGFILE:
- sbuf_cat(buf, config.logfile ?
- config.logfile : boolean_str(0));
- break;
- case _UC_HOMEROOT:
- sbuf_cat(buf, config.home);
- break;
- case _UC_HOMEMODE:
- sbuf_printf(buf, "%04o", config.homemode);
- quote = 0;
- break;
- case _UC_SHELLPATH:
- sbuf_cat(buf, config.shelldir);
- break;
- case _UC_SHELLS:
- for (j = 0; j < _UC_MAXSHELLS &&
- system_shells[j] != NULL; j++) {
- sbuf_printf(buf, "%s\"%s\"", j ?
- "," : "", system_shells[j]);
- }
- quote = 0;
- break;
- case _UC_DEFAULTSHELL:
- sbuf_cat(buf, config.shell_default ?
- config.shell_default : bourne_shell);
- break;
- case _UC_DEFAULTGROUP:
- sbuf_cat(buf, config.default_group ?
- config.default_group : "");
- break;
- case _UC_EXTRAGROUPS:
- extendarray(&config.groups, &config.numgroups, 200);
- for (j = 0; j < config.numgroups &&
- config.groups[j] != NULL; j++)
- sbuf_printf(buf, "%s\"%s\"", j ?
- "," : "", config.groups[j]);
- quote = 0;
- break;
- case _UC_DEFAULTCLASS:
- sbuf_cat(buf, config.default_class ?
- config.default_class : "");
- break;
- case _UC_MINUID:
- sbuf_printf(buf, "%lu", (unsigned long) config.min_uid);
- quote = 0;
- break;
- case _UC_MAXUID:
- sbuf_printf(buf, "%lu", (unsigned long) config.max_uid);
- quote = 0;
- break;
- case _UC_MINGID:
- sbuf_printf(buf, "%lu", (unsigned long) config.min_gid);
- quote = 0;
- break;
- case _UC_MAXGID:
- sbuf_printf(buf, "%lu", (unsigned long) config.max_gid);
- quote = 0;
- break;
- case _UC_EXPIRE:
- sbuf_printf(buf, "%d", config.expire_days);
- quote = 0;
- break;
- case _UC_PASSWORD:
- sbuf_printf(buf, "%d", config.password_days);
- quote = 0;
- break;
- case _UC_NONE:
- break;
- }
- sbuf_finish(buf);
+ buf = sbuf_new_auto();
+ for (i = _UC_NONE; i < _UC_FIELDS; i++) {
+ int quote = 1;
+
+ sbuf_clear(buf);
+ switch (i) {
+ case _UC_DEFAULTPWD:
+ sbuf_cat(buf, boolean_str(config.default_password));
+ break;
+ case _UC_REUSEUID:
+ sbuf_cat(buf, boolean_str(config.reuse_uids));
+ break;
+ case _UC_REUSEGID:
+ sbuf_cat(buf, boolean_str(config.reuse_gids));
+ break;
+ case _UC_NISPASSWD:
+ sbuf_cat(buf, config.nispasswd ? config.nispasswd :
+ "");
+ quote = 0;
+ break;
+ case _UC_DOTDIR:
+ sbuf_cat(buf, config.dotdir ? config.dotdir :
+ boolean_str(0));
+ break;
+ case _UC_NEWMAIL:
+ sbuf_cat(buf, config.newmail ? config.newmail :
+ boolean_str(0));
+ break;
+ case _UC_LOGFILE:
+ sbuf_cat(buf, config.logfile ? config.logfile :
+ boolean_str(0));
+ break;
+ case _UC_HOMEROOT:
+ sbuf_cat(buf, config.home);
+ break;
+ case _UC_HOMEMODE:
+ sbuf_printf(buf, "%04o", config.homemode);
+ quote = 0;
+ break;
+ case _UC_SHELLPATH:
+ sbuf_cat(buf, config.shelldir);
+ break;
+ case _UC_SHELLS:
+ for (j = 0; j < _UC_MAXSHELLS &&
+ system_shells[j] != NULL; j++)
+ sbuf_printf(buf, "%s\"%s\"", j ?
+ "," : "", system_shells[j]);
+ quote = 0;
+ break;
+ case _UC_DEFAULTSHELL:
+ sbuf_cat(buf, config.shell_default ?
+ config.shell_default : bourne_shell);
+ break;
+ case _UC_DEFAULTGROUP:
+ sbuf_cat(buf, config.default_group ?
+ config.default_group : "");
+ break;
+ case _UC_EXTRAGROUPS:
+ extendarray(&config.groups, &config.numgroups, 200);
+ for (j = 0; j < config.numgroups &&
+ config.groups[j] != NULL; j++)
+ sbuf_printf(buf, "%s\"%s\"", j ?
+ "," : "", config.groups[j]);
+ quote = 0;
+ break;
+ case _UC_DEFAULTCLASS:
+ sbuf_cat(buf, config.default_class ?
+ config.default_class : "");
+ break;
+ case _UC_MINUID:
+ sbuf_printf(buf, "%lu", (unsigned long) config.min_uid);
+ quote = 0;
+ break;
+ case _UC_MAXUID:
+ sbuf_printf(buf, "%lu", (unsigned long) config.max_uid);
+ quote = 0;
+ break;
+ case _UC_MINGID:
+ sbuf_printf(buf, "%lu", (unsigned long) config.min_gid);
+ quote = 0;
+ break;
+ case _UC_MAXGID:
+ sbuf_printf(buf, "%lu", (unsigned long) config.max_gid);
+ quote = 0;
+ break;
+ case _UC_EXPIRE:
+ sbuf_printf(buf, "%d", config.expire_days);
+ quote = 0;
+ break;
+ case _UC_PASSWORD:
+ sbuf_printf(buf, "%d", config.password_days);
+ quote = 0;
+ break;
+ case _UC_NONE:
+ break;
+ }
+ sbuf_finish(buf);
- if (comments[i])
- fputs(comments[i], fp);
+ if (comments[i])
+ fputs(comments[i], fp);
- if (*kwds[i]) {
- if (quote)
- fprintf(fp, "%s = \"%s\"\n", kwds[i], sbuf_data(buf));
- else
- fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf));
+ if (*kwds[i]) {
+ if (quote)
+ fprintf(fp, "%s = \"%s\"\n", kwds[i],
+ sbuf_data(buf));
+ else
+ fprintf(fp, "%s = %s\n", kwds[i], sbuf_data(buf));
#if debugging
- printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf));
+ printf("WROTE: %s = %s\n", kwds[i], sbuf_data(buf));
#endif
- }
- }
- sbuf_delete(buf);
- return fclose(fp) != EOF;
}
}
- return 0;
+ sbuf_delete(buf);
+ return (fclose(fp) != EOF);
}