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).
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 &&
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)
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 */
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));