/* passwd.cc: getpwnam () and friends
- Copyright 1996, 1997, 1998 Cygnus Solutions.
+ Copyright 1996, 1997, 1998, 2001 Cygnus Solutions.
This file is part of Cygwin.
#include "security.h"
#include "fhandler.h"
#include "dtable.h"
+#include "path.h"
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "cygheap.h"
#include <sys/termios.h>
+#include "pwdgrp.h"
/* Read /etc/passwd only once for better performance. This is done
on the first call that needs information from it. */
-static struct passwd *passwd_buf = NULL; /* passwd contents in memory */
-static int curr_lines = 0;
-static int max_lines = 0;
-
-/* Set to loaded when /etc/passwd has been read in by read_etc_passwd ().
- Set to emulated if passwd is emulated. */
-/* Functions in this file need to check the value of passwd_state
- and read in the password file if it isn't set. */
-enum pwd_state {
- uninitialized = 0,
- initializing,
- emulated,
- loaded
-};
-static pwd_state passwd_state = uninitialized;
+static struct passwd *passwd_buf; /* passwd contents in memory */
+static int curr_lines;
+static int max_lines;
+
+static pwdgrp_check passwd_state;
+
/* Position in the passwd cache */
#ifdef _MT_SAFE
if (passwd_state != initializing)
{
passwd_state = initializing;
+ if (max_lines) /* When rereading, free allocated memory first. */
+ {
+ for (int i = 0; i < curr_lines; ++i)
+ free (passwd_buf[i].pw_name);
+ free (passwd_buf);
+ curr_lines = max_lines = 0;
+ }
FILE *f = fopen ("/etc/passwd", "rt");
add_pwd_line (linebuf);
}
+ passwd_state.set_last_modified (f);
fclose (f);
passwd_state = loaded;
}
extern "C" struct passwd *
getpwduid (uid_t)
{
- if (passwd_state <= initializing)
- read_etc_passwd ();
-
return NULL;
}
extern "C" void
setpwent (void)
{
- if (passwd_state <= initializing)
- read_etc_passwd ();
-
pw_pos = 0;
}
extern "C" void
endpwent (void)
{
- if (passwd_state <= initializing)
- read_etc_passwd ();
-
pw_pos = 0;
}
extern "C" int
setpassent ()
{
- if (passwd_state <= initializing)
- read_etc_passwd ();
-
return 0;
}