diff options
author | Corinna Vinschen <corinna@vinschen.de> | 2002-06-05 11:10:15 +0000 |
---|---|---|
committer | Corinna Vinschen <corinna@vinschen.de> | 2002-06-05 11:10:15 +0000 |
commit | 38f39368891d83c63e6f4aa3b00b19b80444b041 (patch) | |
tree | 84c6f229a06e2dc946c671a4d7b2dab26b314900 /winsup/cygwin/grp.cc | |
parent | ce006ffa7f6985c2dbe6128d74ba6beda562743a (diff) | |
download | cygnal-38f39368891d83c63e6f4aa3b00b19b80444b041.tar.gz cygnal-38f39368891d83c63e6f4aa3b00b19b80444b041.tar.bz2 cygnal-38f39368891d83c63e6f4aa3b00b19b80444b041.zip |
* grp.cc (etc_group): Removed.
(parse_grp): Make line parameter nonconst. Don't copy data into new
allocated memory. Check for CR instead of LF to accomodate new
read method.
(add_grp_line): Make line parameter nonconst.
(read_etc_group): Rearrange using new pwdgrp_read class.
* passwd.cc (parse_pwd): Don't copy data into new allocated memory.
Check for CR instead of LF to accomodate new read method.
(read_etc_passwd): Rearrange using new pwdgrp_read class.
* pwdgrp.h (pwdgrp_check::set_last_modified): Use different
parameters.
(class pwdgrp_read): New class for opening and reading passwd and
group files.
Diffstat (limited to 'winsup/cygwin/grp.cc')
-rw-r--r-- | winsup/cygwin/grp.cc | 69 |
1 files changed, 25 insertions, 44 deletions
diff --git a/winsup/cygwin/grp.cc b/winsup/cygwin/grp.cc index 3511bcb92..13f74dd64 100644 --- a/winsup/cygwin/grp.cc +++ b/winsup/cygwin/grp.cc @@ -29,7 +29,6 @@ details. */ /* Read /etc/group only once for better performance. This is done on the first call that needs information from it. */ -static const char *etc_group NO_COPY = "/etc/group"; static struct __group32 *group_buf; /* group contents in memory */ static int curr_lines; static int max_lines; @@ -44,22 +43,19 @@ static int grp_pos = 0; static pwdgrp_check group_state; static int -parse_grp (struct __group32 &grp, const char *line) +parse_grp (struct __group32 &grp, char *line) { int len = strlen(line); - char *newline = (char *) malloc (len + 1); - (void) memcpy (newline, line, len + 1); + if (line[--len] == '\r') + line[len] = '\0'; - if (newline[--len] == '\n') - newline[len] = '\0'; - - char *dp = strchr (newline, ':'); + char *dp = strchr (line, ':'); if (!dp) return 0; *dp++ = '\0'; - grp.gr_name = newline; + grp.gr_name = line; grp.gr_passwd = dp; dp = strchr (grp.gr_passwd, ':'); @@ -104,7 +100,7 @@ parse_grp (struct __group32 &grp, const char *line) /* Read one line from /etc/group into the group cache */ static void -add_grp_line (const char *line) +add_grp_line (char *line) { if (curr_lines == max_lines) { @@ -143,11 +139,7 @@ pthread_mutex_t NO_COPY group_lock::mutex = (pthread_mutex_t) PTHREAD_MUTEX_INIT void read_etc_group () { - char linebuf [200]; - char group_name [UNLEN + 1]; - DWORD group_name_len = UNLEN + 1; - - strncpy (group_name, "Administrators", sizeof (group_name)); + static pwdgrp_read gr; group_lock here (cygwin_finished_initializing); @@ -158,49 +150,38 @@ read_etc_group () if (group_state != initializing) { group_state = initializing; - if (max_lines) /* When rereading, free allocated memory first. */ - { - for (int i = 0; i < curr_lines; ++i) - { - free (group_buf[i].gr_name); - free (group_buf[i].gr_mem); - } - curr_lines = 0; - } - - FILE *f = fopen (etc_group, "rt"); - - if (f) + if (gr.open ("/etc/group")) { - while (fgets (linebuf, sizeof (linebuf), f) != NULL) - { - if (strlen (linebuf)) - add_grp_line (linebuf); - } + char *line; + while ((line = gr.gets ()) != NULL) + if (strlen (line)) + add_grp_line (line); - group_state.set_last_modified (f); - fclose (f); + group_state.set_last_modified (gr.get_fhandle(), gr.get_fname ()); group_state = loaded; + gr.close (); + debug_printf ("Read /etc/group, %d lines", curr_lines); } else /* /etc/group doesn't exist -- create default one in memory */ { + char group_name [UNLEN + 1]; + DWORD group_name_len = UNLEN + 1; char domain_name [INTERNET_MAX_HOST_NAME_LENGTH + 1]; DWORD domain_name_len = INTERNET_MAX_HOST_NAME_LENGTH + 1; SID_NAME_USE acType; + static char linebuf [200]; + debug_printf ("Emulating /etc/group"); - if (! LookupAccountSidA (NULL , - well_known_admins_sid, - group_name, - &group_name_len, - domain_name, - &domain_name_len, - &acType)) + strncpy (group_name, "Administrators", sizeof (group_name)); + if (! LookupAccountSidA (NULL, well_known_admins_sid, group_name, + &group_name_len, domain_name, + &domain_name_len, &acType)) { strcpy (group_name, "unknown"); debug_printf ("Failed to get local admins group name. %E"); } - - snprintf (linebuf, sizeof (linebuf), "%s::%u:\n", group_name, (unsigned) DEFAULT_GID); + snprintf (linebuf, sizeof (linebuf), "%s::%u:\n", group_name, + (unsigned) DEFAULT_GID); add_grp_line (linebuf); group_state = emulated; } |