Split process_flags(), open_files(), and close_files() out of main(). New

global variables is_shadow, sort_mode, use_system_grp_file, and
use_system_sgr_file.
This commit is contained in:
nekral-guest 2008-01-01 13:13:47 +00:00
parent 83b9a376a2
commit f6f6eeda8e
2 changed files with 109 additions and 69 deletions

View File

@ -1,3 +1,9 @@
2008-01-01 Nicolas François <nicolas.francois@centraliens.net>
* src/grpck.c: Split process_flags(), open_files(), and
close_files() out of main(). New global variables is_shadow,
sort_mode, use_system_grp_file, and use_system_sgr_file.
2007-12-31 Nicolas François <nicolas.francois@centraliens.net> 2007-12-31 Nicolas François <nicolas.francois@centraliens.net>
* man/po/Makefile.in.in: If remove-potcdate.sin does not exist, * man/po/Makefile.in.in: If remove-potcdate.sin does not exist,

View File

@ -66,11 +66,16 @@ extern struct commonio_entry *__sgr_get_head (void);
*/ */
static char *Prog; static char *Prog;
static const char *grp_file = GROUP_FILE; static const char *grp_file = GROUP_FILE;
static int use_system_grp_file = 1;
#ifdef SHADOWGRP #ifdef SHADOWGRP
static const char *sgr_file = SGROUP_FILE; static const char *sgr_file = SGROUP_FILE;
static int use_system_sgr_file = 1;
static int is_shadow = 0;
#endif #endif
/* Options */
static int read_only = 0; static int read_only = 0;
static int sort_mode = 0;
/* local function prototypes */ /* local function prototypes */
static void usage (void); static void usage (void);
@ -106,34 +111,11 @@ static void delete_member (char **list, const char *member)
} }
/* /*
* grpck - verify group file integrity * process_flags -
*/ */
int main (int argc, char **argv) static void process_flags (int argc, char **argv)
{ {
int arg; int arg;
int errors = 0;
int changed = 0;
int i;
struct commonio_entry *gre, *tgre;
struct group *grp;
int sort_mode = 0;
#ifdef SHADOWGRP
struct commonio_entry *sge, *tsge;
struct sgrp *sgr;
int is_shadow = 0;
#endif
/*
* Get my name so that I can use it to report errors.
*/
Prog = Basename (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
OPENLOG ("grpck");
/* /*
* Parse the command line arguments * Parse the command line arguments
@ -163,11 +145,13 @@ int main (int argc, char **argv)
* Make certain we have the right number of arguments * Make certain we have the right number of arguments
*/ */
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (optind != argc && optind + 1 != argc && optind + 2 != argc) if ((argc < optind) || (argc > (optind + 2)))
#else #else
if (optind != argc && optind + 1 != argc) if ((argc < optind) || (argc > (optind + 1)))
#endif #endif
{
usage (); usage ();
}
/* /*
* If there are two left over filenames, use those as the group and * If there are two left over filenames, use those as the group and
@ -176,34 +160,42 @@ int main (int argc, char **argv)
if (optind != argc) { if (optind != argc) {
grp_file = argv[optind]; grp_file = argv[optind];
gr_name (grp_file); gr_name (grp_file);
use_system_grp_file = 0;
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (optind + 2 == argc) { if ((optind + 2) == argc) {
sgr_file = argv[optind + 1]; sgr_file = argv[optind + 1];
sgr_name (sgr_file); sgr_name (sgr_file);
is_shadow = 1; is_shadow = 1;
} else if (optind == argc) use_system_sgr_file = 0;
} else if (optind == argc) {
is_shadow = sgr_file_present (); is_shadow = sgr_file_present ();
}
#endif #endif
}
static void open_files ()
{
/* /*
* Lock the files if we aren't in "read-only" mode * Lock the files if we aren't in "read-only" mode
*/ */
if (!read_only) { if (!read_only) {
if (!gr_lock ()) { if (gr_lock () == 0) {
fprintf (stderr, _("%s: cannot lock file %s\n"), fprintf (stderr, _("%s: cannot lock file %s\n"),
Prog, grp_file); Prog, grp_file);
if (optind == argc) if (use_system_grp_file) {
SYSLOG ((LOG_WARN, "cannot lock %s", grp_file)); SYSLOG ((LOG_WARN, "cannot lock %s", grp_file));
}
closelog (); closelog ();
exit (E_CANT_LOCK); exit (E_CANT_LOCK);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow && !sgr_lock ()) { if (is_shadow && (sgr_lock () == 0)) {
fprintf (stderr, _("%s: cannot lock file %s\n"), fprintf (stderr, _("%s: cannot lock file %s\n"),
Prog, sgr_file); Prog, sgr_file);
if (optind == argc) if (use_system_sgr_file) {
SYSLOG ((LOG_WARN, "cannot lock %s", sgr_file)); SYSLOG ((LOG_WARN, "cannot lock %s", sgr_file));
}
closelog (); closelog ();
exit (E_CANT_LOCK); exit (E_CANT_LOCK);
} }
@ -214,24 +206,91 @@ int main (int argc, char **argv)
* Open the files. Use O_RDONLY if we are in read_only mode, * Open the files. Use O_RDONLY if we are in read_only mode,
* O_RDWR otherwise. * O_RDWR otherwise.
*/ */
if (!gr_open (read_only ? O_RDONLY : O_RDWR)) { if (gr_open (read_only ? O_RDONLY : O_RDWR) == 0) {
fprintf (stderr, _("%s: cannot open file %s\n"), Prog, fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
grp_file); grp_file);
if (optind == argc) if (use_system_grp_file) {
SYSLOG ((LOG_WARN, "cannot open %s", grp_file)); SYSLOG ((LOG_WARN, "cannot open %s", grp_file));
}
closelog (); closelog ();
exit (E_CANT_OPEN); exit (E_CANT_OPEN);
} }
#ifdef SHADOWGRP #ifdef SHADOWGRP
if (is_shadow && !sgr_open (read_only ? O_RDONLY : O_RDWR)) { if (is_shadow && (sgr_open (read_only ? O_RDONLY : O_RDWR) == 0)) {
fprintf (stderr, _("%s: cannot open file %s\n"), Prog, fprintf (stderr, _("%s: cannot open file %s\n"), Prog,
sgr_file); sgr_file);
if (optind == argc) if (use_system_sgr_file) {
SYSLOG ((LOG_WARN, "cannot open %s", sgr_file)); SYSLOG ((LOG_WARN, "cannot open %s", sgr_file));
}
closelog (); closelog ();
exit (E_CANT_OPEN); exit (E_CANT_OPEN);
} }
#endif #endif
}
static void close_files (int changed)
{
/*
* All done. If there were no change we can just abandon any
* changes to the files.
*/
if (changed) {
if (gr_close () == 0) {
fprintf (stderr, _("%s: cannot update file %s\n"),
Prog, grp_file);
exit (E_CANT_UPDATE);
}
#ifdef SHADOWGRP
if (is_shadow && (sgr_close () == 0)) {
fprintf (stderr, _("%s: cannot update file %s\n"),
Prog, sgr_file);
exit (E_CANT_UPDATE);
}
#endif
}
/*
* Don't be anti-social - unlock the files when you're done.
*/
#ifdef SHADOWGRP
if (is_shadow) {
sgr_unlock ();
}
#endif
(void) gr_unlock ();
}
/*
* grpck - verify group file integrity
*/
int main (int argc, char **argv)
{
int errors = 0;
int changed = 0;
int i;
struct commonio_entry *gre, *tgre;
struct group *grp;
#ifdef SHADOWGRP
struct commonio_entry *sge, *tsge;
struct sgrp *sgr;
#endif
/*
* Get my name so that I can use it to report errors.
*/
Prog = Basename (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
OPENLOG ("grpck");
/* Parse the command line arguments */
process_flags (argc, argv);
open_files ();
if (sort_mode) { if (sort_mode) {
gr_sort (); gr_sort ();
@ -631,34 +690,9 @@ int main (int argc, char **argv)
shadow_done: shadow_done:
#endif /* SHADOWGRP */ #endif /* SHADOWGRP */
/* write_and_bye:
* All done. If there were no change we can just abandon any /* Commit the change in the database if needed */
* changes to the files. close_files (changed);
*/
if (changed) {
write_and_bye:
if (!gr_close ()) {
fprintf (stderr, _("%s: cannot update file %s\n"),
Prog, grp_file);
exit (E_CANT_UPDATE);
}
#ifdef SHADOWGRP
if (is_shadow && !sgr_close ()) {
fprintf (stderr, _("%s: cannot update file %s\n"),
Prog, sgr_file);
exit (E_CANT_UPDATE);
}
#endif
}
/*
* Don't be anti-social - unlock the files when you're done.
*/
#ifdef SHADOWGRP
if (is_shadow)
sgr_unlock ();
#endif
(void) gr_unlock ();
nscd_flush_cache ("group"); nscd_flush_cache ("group");