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() load_history()
- Fix segfault on loading huge strings from history file - Fix segfault on loading huge strings from history file
(David Benbennick). (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: - nano.c:
breakable() breakable()
- Fix incorrect return value on short lines (David Benbennick). - Fix incorrect return value on short lines (David Benbennick).

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