More getpwent changes

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1465 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
Chris Allegretta 2003-02-13 22:00:19 +00:00
parent 5acbc8bb0d
commit 1debce2865
2 changed files with 35 additions and 25 deletions

View File

@ -33,6 +33,13 @@ CVS code
load_history()
- Fix segfault on loading huge strings from history file
(David Benbennick).
load_history(), save_history()
- Changed to look at $HOME before getpwuid(geteuid()), see
details in comment for rcfile.c:do_rcfile().
real_dir_from_tidle()
- Change check for the running user's home dir to use
getpwuid(geteuid()) rather than a getpwent() loop
(suggested by Jordi).
- nano.c:
breakable()
- Fix incorrect return value on short lines (David Benbennick).

53
files.c
View File

@ -1896,15 +1896,11 @@ char *real_dir_from_tilde(const char *buf)
for (i = 1; buf[i] != '/' && buf[i] != '\0'; i++)
;
if (i == 1) {
/* Determine home directory using getpwent(), don't rely on
/* Determine home directory using getpwent(), don't rely on
$HOME */
uid_t euid = geteuid();
do {
userdata = getpwent();
} while (userdata != NULL && userdata->pw_uid != euid);
} else {
if (i == 1)
userdata = getpwuid(geteuid());
else {
do {
userdata = getpwent();
} while (userdata != NULL &&
@ -2872,22 +2868,26 @@ char *do_browse_from(const char *inpath)
void load_history(void)
{
FILE *hist;
const struct passwd *userage;
uid_t euid = geteuid();
const struct passwd *userage = NULL;
static char *nanohist;
char *buf, *ptr;
char *homenv = getenv("HOME");
historyheadtype *history = &search_history;
do {
userage = getpwent();
} while (userage != NULL && userage->pw_uid != euid);
endpwent();
if (homenv != NULL) {
nanohist = nrealloc(nanohist, strlen(homenv) + 15);
sprintf(nanohist, "%s/.nano_history", homenv);
} else {
userage = getpwuid(geteuid());
endpwent();
nanohist = nrealloc(nanohist, strlen(userage->pw_dir) + 15);
sprintf(nanohist, "%s/.nano_history", userage->pw_dir);
}
/* assume do_rcfile has reported missing home dir */
if (userage != NULL) {
nanohist = nrealloc(nanohist, strlen(userage->pw_dir) + 15);
sprintf(nanohist, "%s/.nano_history", userage->pw_dir);
if (homenv != NULL || userage != NULL) {
hist = fopen(nanohist, "r");
if (!hist) {
if (errno != ENOENT)
@ -2917,9 +2917,9 @@ void load_history(void)
void save_history(void)
{
FILE *hist;
const struct passwd *userage;
uid_t euid = geteuid();
const struct passwd *userage = NULL;
char *nanohist = NULL;
char *homenv = getenv("HOME");
historytype *h;
/* don't save unchanged or empty histories */
@ -2927,14 +2927,17 @@ void save_history(void)
ISSET(HISTORY_CHANGED) && !ISSET(VIEW_MODE)))
return;
do {
userage = getpwent();
} while (userage != NULL && userage->pw_uid != euid);
endpwent();
if (userage != NULL) {
if (homenv != NULL) {
nanohist = nrealloc(nanohist, strlen(homenv) + 15);
sprintf(nanohist, "%s/.nano_history", homenv);
} else {
userage = getpwuid(geteuid());
endpwent();
nanohist = nrealloc(nanohist, strlen(userage->pw_dir) + 15);
sprintf(nanohist, "%s/.nano_history", userage->pw_dir);
}
if (homenv != NULL || userage != NULL) {
hist = fopen(nanohist, "wb");
if (!hist) {
rcfile_msg(_("Unable to write ~/.nano_history file, %s"), strerror(errno));