From: Chris Allegretta Date: Thu, 13 Feb 2003 22:00:19 +0000 (+0000) Subject: More getpwent changes X-Git-Tag: v1.1.99pre3~3 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=1debce286590c6fe079dad03bacbdf680b4b63c5;p=nano.git More getpwent changes git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1465 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index d8265f92..7051fc6f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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). diff --git a/files.c b/files.c index 6436c7af..bce66810 100644 --- a/files.c +++ b/files.c @@ -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(); - - /* assume do_rcfile has reported missing home dir */ - 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); + } + + /* assume do_rcfile has reported missing home 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));