2015-05-05 12:23:49 +02:00
|
|
|
Based on f813180f2fc1d682dd097e4a05ef4d15000204ad Mon Sep 17 00:00:00 2001
|
2015-01-13 18:16:42 +01:00
|
|
|
From: Roman Kollar <rkollar@redhat.com>
|
|
|
|
Date: Mon, 29 Oct 2012 17:52:52 +0100
|
|
|
|
Subject: [PATCH] Add .history file locking - shared readers, exclusive writer
|
|
|
|
|
|
|
|
Originally reported at Red Hat Bugzilla:
|
|
|
|
https://bugzilla.redhat.com/show_bug.cgi?id=648592
|
|
|
|
|
|
|
|
Patch by Vojtech Vitek (V-Teq) <vvitek@redhat.com>
|
|
|
|
|
|
|
|
Additional changes reflecting:
|
|
|
|
https://bugzilla.redhat.com/show_bug.cgi?id=879371
|
|
|
|
|
|
|
|
Changes by Fridolin Pokorny <fpokorny@redhat.com>
|
|
|
|
|
|
|
|
---
|
2015-05-05 12:23:49 +02:00
|
|
|
sh.c | 102 +++++++++++++++++++++++++++++++++++++--------------
|
|
|
|
sh.decls.h | 4 +-
|
|
|
|
sh.dol.c | 2 -
|
|
|
|
sh.err.c | 16 ++++++++
|
|
|
|
sh.h | 18 +++++++++
|
|
|
|
sh.hist.c | 122 ++++++++++++++++++++++++++++++-------------------------------
|
|
|
|
sh.lex.c | 8 ++--
|
|
|
|
sh.sem.c | 2 -
|
|
|
|
8 files changed, 178 insertions(+), 96 deletions(-)
|
2015-01-13 18:16:42 +01:00
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.c
|
|
|
|
+++ sh.c 2015-05-04 13:53:54.000000000 +0000
|
|
|
|
@@ -141,6 +141,7 @@ struct saved_state {
|
2015-01-13 18:16:42 +01:00
|
|
|
int cantell;
|
|
|
|
struct Bin B;
|
|
|
|
int justpr;
|
|
|
|
+ int close_unit;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int srccat (Char *, Char *);
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1378,7 +1379,7 @@ main(int argc, char **argv)
|
2015-01-13 18:16:42 +01:00
|
|
|
/*
|
|
|
|
* Source history before .login so that it is available in .login
|
|
|
|
*/
|
|
|
|
- loadhist(NULL, 0);
|
|
|
|
+ loadhist(NULL, HIST_FILE_RDLCK);
|
|
|
|
#ifndef LOGINFIRST
|
|
|
|
if (loginsh)
|
|
|
|
(void) srccat(varval(STRhome), STRsldotlogin);
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1501,7 +1502,7 @@ static int
|
2015-01-13 18:16:42 +01:00
|
|
|
srccat(Char *cp, Char *dp)
|
|
|
|
{
|
|
|
|
if (cp[0] == '/' && cp[1] == '\0')
|
|
|
|
- return srcfile(short2str(dp), (mflag ? 0 : 1), 0, NULL);
|
|
|
|
+ return srcfile(short2str(dp), (mflag ? 0 : HIST_ONLY), 0, NULL);
|
|
|
|
else {
|
|
|
|
Char *ep;
|
|
|
|
char *ptr;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1517,7 +1518,7 @@ srccat(Char *cp, Char *dp)
|
2015-01-13 18:16:42 +01:00
|
|
|
cleanup_push(ep, xfree);
|
|
|
|
ptr = short2str(ep);
|
|
|
|
|
|
|
|
- rv = srcfile(ptr, (mflag ? 0 : 1), 0, NULL);
|
|
|
|
+ rv = srcfile(ptr, (mflag ? 0 : HIST_ONLY), 0, NULL);
|
|
|
|
cleanup_until(ep);
|
|
|
|
return rv;
|
|
|
|
}
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1531,20 +1532,49 @@ static int
|
2015-01-13 18:16:42 +01:00
|
|
|
#else
|
|
|
|
int
|
|
|
|
#endif /*WINNT_NATIVE*/
|
|
|
|
-srcfile(const char *f, int onlyown, int flag, Char **av)
|
|
|
|
+srcfile(const char *f, int onlyown, int flg, Char **av)
|
|
|
|
{
|
|
|
|
- int unit;
|
2015-05-05 12:23:49 +02:00
|
|
|
+ int *unit;
|
|
|
|
|
2015-01-13 18:16:42 +01:00
|
|
|
- if ((unit = xopen(f, O_RDONLY|O_LARGEFILE)) == -1)
|
|
|
|
- return 0;
|
|
|
|
- cleanup_push(&unit, open_cleanup);
|
|
|
|
- unit = dmove(unit, -1);
|
|
|
|
- cleanup_ignore(&unit);
|
|
|
|
- cleanup_until(&unit);
|
|
|
|
-
|
|
|
|
- (void) close_on_exec(unit, 1);
|
|
|
|
- srcunit(unit, onlyown, flag, av);
|
|
|
|
- return 1;
|
|
|
|
+ unit = xmalloc(sizeof(*unit));
|
|
|
|
+ cleanup_push(unit, xfree);
|
|
|
|
+ *unit = xopen(f, O_LARGEFILE |
|
|
|
|
+ ((flg & HIST_FILE_WRLCK) ? (O_CREAT|O_RDWR) : O_RDONLY), 0600);
|
|
|
|
+ if (*unit == -1)
|
|
|
|
+ return 0; /* Error. */
|
|
|
|
+
|
|
|
|
+ cleanup_push(unit, open_cleanup);
|
|
|
|
+ *unit = dmove(*unit, -1);
|
|
|
|
+ (void) close_on_exec(*unit, 1);
|
|
|
|
+
|
|
|
|
+ if (flg & (HIST_FILE_WRLCK | HIST_FILE_RDLCK)) {
|
|
|
|
+ struct flock fl;
|
|
|
|
+
|
|
|
|
+ fl.l_type = (flg & HIST_FILE_WRLCK) ? F_WRLCK : F_RDLCK;
|
|
|
|
+ fl.l_whence = SEEK_SET;
|
|
|
|
+ fl.l_start = 0;
|
|
|
|
+ fl.l_len = 0;
|
|
|
|
+
|
|
|
|
+ cleanup_push(unit, fcntl_cleanup);
|
|
|
|
+ if (fcntl(*unit, F_SETLKW, &fl) == -1)
|
|
|
|
+ cleanup_ignore(unit);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ srcunit(*unit, onlyown, flg, av);
|
|
|
|
+
|
|
|
|
+ /* Unlock the unit, if we don't want to leave it locked (or open). */
|
|
|
|
+ if ((flg & (HIST_FILE_WRLCK | HIST_FILE_RDLCK)) &&
|
|
|
|
+ (!(flg & HIST_FILE_LOCK) || !(flg & HIST_FILE_OPEN)))
|
|
|
|
+ cleanup_until(unit); /* fcntl_cleanup */
|
|
|
|
+
|
|
|
|
+ /* Close the unit, if we don't want to leave it open. */
|
|
|
|
+ if (!(flg & HIST_FILE_OPEN)) {
|
|
|
|
+ cleanup_until(unit); /* open_cleanup */
|
|
|
|
+ cleanup_until(unit); /* xfree */
|
|
|
|
+ return -1; /* Not error but invalid file descriptor. */
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return *unit; /* File descriptor (fd > FSAFE). */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1553,7 +1583,7 @@ srcfile(const char *f, int onlyown, int
|
2015-01-13 18:16:42 +01:00
|
|
|
* fd.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
-st_save(struct saved_state *st, int unit, int hflg, Char **al, Char **av)
|
|
|
|
+st_save(struct saved_state *st, int unit, int flg, Char **al, Char **av)
|
|
|
|
{
|
|
|
|
st->insource = insource;
|
|
|
|
st->SHIN = SHIN;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1602,10 +1632,14 @@ st_save(struct saved_state *st, int unit
|
2015-01-13 18:16:42 +01:00
|
|
|
st->onelflg = onelflg;
|
|
|
|
st->enterhist = enterhist;
|
|
|
|
st->justpr = justpr;
|
|
|
|
- if (hflg)
|
|
|
|
+ if (flg & (HIST_ONLY | HIST_MERGE))
|
|
|
|
st->HIST = HIST;
|
|
|
|
else
|
|
|
|
st->HIST = '\0';
|
|
|
|
+ if (flg & HIST_FILE_OPEN)
|
|
|
|
+ st->close_unit = 0;
|
|
|
|
+ else
|
|
|
|
+ st->close_unit = 1;
|
|
|
|
st->cantell = cantell;
|
|
|
|
cpybin(st->B, B);
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1644,7 +1678,7 @@ st_save(struct saved_state *st, int unit
|
2015-01-13 18:16:42 +01:00
|
|
|
evalp = 0;
|
|
|
|
alvec = al;
|
|
|
|
alvecp = 0;
|
|
|
|
- enterhist = hflg;
|
|
|
|
+ enterhist = flg & (HIST_ONLY | HIST_MERGE);
|
|
|
|
if (enterhist)
|
|
|
|
HIST = '\0';
|
|
|
|
insource = 1;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1677,7 +1711,8 @@ st_restore(void *xst)
|
2015-01-13 18:16:42 +01:00
|
|
|
}
|
|
|
|
cpybin(B, st->B);
|
|
|
|
|
|
|
|
- xclose(SHIN);
|
|
|
|
+ if (st->close_unit)
|
|
|
|
+ xclose(SHIN);
|
|
|
|
|
|
|
|
insource = st->insource;
|
|
|
|
SHIN = st->SHIN;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1713,7 +1748,7 @@ st_restore(void *xst)
|
2015-01-13 18:16:42 +01:00
|
|
|
* we don't chance it. This occurs on ".cshrc"s and the like.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
-srcunit(int unit, int onlyown, int hflg, Char **av)
|
|
|
|
+srcunit(int unit, int onlyown, int flg, Char **av)
|
|
|
|
{
|
|
|
|
struct saved_state st;
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1739,7 +1774,7 @@ srcunit(int unit, int onlyown, int hflg,
|
2015-01-13 18:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the current state and move us to a new state */
|
|
|
|
- st_save(&st, unit, hflg, NULL, av);
|
|
|
|
+ st_save(&st, unit, flg, NULL, av);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now if we are allowing commands to be interrupted, we let ourselves be
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -2078,7 +2113,7 @@ process(int catch)
|
2015-01-13 18:16:42 +01:00
|
|
|
* elsewhere...
|
|
|
|
*/
|
|
|
|
if (enterhist || (catch && intty && !whyles && !tellwhat && !arun))
|
|
|
|
- savehist(¶ml, enterhist > 1);
|
|
|
|
+ savehist(¶ml, enterhist > 1 ? HIST_MERGE : 0);
|
|
|
|
|
|
|
|
if (Expand && seterr)
|
|
|
|
Expand = 0;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -2165,21 +2200,28 @@ process(int catch)
|
2015-01-13 18:16:42 +01:00
|
|
|
void
|
|
|
|
dosource(Char **t, struct command *c)
|
|
|
|
{
|
|
|
|
+ (void) dosource_flg(t, c, 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int
|
|
|
|
+dosource_flg(Char **t, struct command *c, int flg)
|
|
|
|
+{
|
|
|
|
Char *f;
|
|
|
|
- int hflg = 0;
|
|
|
|
char *file;
|
|
|
|
+ int fd;
|
|
|
|
+ int newflg = 0;
|
|
|
|
|
|
|
|
USE(c);
|
|
|
|
t++;
|
|
|
|
if (*t && eq(*t, STRmh)) {
|
|
|
|
if (*++t == NULL)
|
|
|
|
stderror(ERR_NAME | ERR_HFLAG);
|
|
|
|
- hflg++;
|
|
|
|
+ newflg |= HIST_ONLY;
|
|
|
|
}
|
|
|
|
else if (*t && eq(*t, STRmm)) {
|
|
|
|
if (*++t == NULL)
|
|
|
|
stderror(ERR_NAME | ERR_MFLAG);
|
|
|
|
- hflg = 2;
|
|
|
|
+ newflg |= HIST_MERGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = globone(*t++, G_ERROR);
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -2188,9 +2230,15 @@ dosource(Char **t, struct command *c)
|
2015-01-13 18:16:42 +01:00
|
|
|
xfree(f);
|
|
|
|
t = glob_all_or_error(t);
|
2015-05-05 12:23:49 +02:00
|
|
|
cleanup_push(t, blk_cleanup);
|
2015-01-13 18:16:42 +01:00
|
|
|
- if ((!srcfile(file, 0, hflg, t)) && (!hflg) && (!bequiet))
|
|
|
|
+ fd = srcfile(file, 0, (flg | newflg), t);
|
|
|
|
+ if ((!fd) && (!newflg) && (!bequiet))
|
|
|
|
stderror(ERR_SYSTEM, file, strerror(errno));
|
|
|
|
- cleanup_until(file);
|
|
|
|
+ /* We need to preserve fd and it's cleaning routines on the top of the
|
|
|
|
+ * cleaning stack. Don't call cleanup_until() but clean it manually. */
|
|
|
|
+ cleanup_ignore(file);
|
|
|
|
+ xfree(file);
|
|
|
|
+
|
|
|
|
+ return fd; /* Valid/invalid file descriptor (>FSAVE, -1). Zero on error. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.decls.h
|
|
|
|
+++ sh.decls.h 2015-05-04 13:56:16.000000000 +0000
|
2015-01-13 18:16:42 +01:00
|
|
|
@@ -38,6 +38,7 @@
|
|
|
|
*/
|
|
|
|
extern Char *gethdir (const Char *);
|
|
|
|
extern void dosource (Char **, struct command *);
|
|
|
|
+extern int dosource_flg (Char **, struct command *, int);
|
|
|
|
extern void exitstat (void);
|
|
|
|
extern void goodbye (Char **, struct command *);
|
|
|
|
extern void importpath (Char *);
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -99,6 +100,7 @@ extern void cleanup_until_mark(void);
|
2015-01-13 18:16:42 +01:00
|
|
|
extern size_t cleanup_push_mark(void);
|
|
|
|
extern void cleanup_pop_mark(size_t);
|
|
|
|
extern void open_cleanup(void *);
|
|
|
|
+extern void fcntl_cleanup(void *);
|
|
|
|
extern void opendir_cleanup(void *);
|
|
|
|
extern void sigint_cleanup(void *);
|
|
|
|
extern void sigprocmask_cleanup(void *);
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -220,7 +222,7 @@ extern struct Hist *enthist (int, str
|
2015-01-13 18:16:42 +01:00
|
|
|
extern void savehist (struct wordent *, int);
|
|
|
|
extern char *fmthist (int, ptr_t);
|
|
|
|
extern void rechist (Char *, int);
|
|
|
|
-extern void loadhist (Char *, int);
|
|
|
|
+extern int loadhist (Char *, int);
|
|
|
|
extern void displayHistStats(const char *);
|
2015-05-05 12:23:49 +02:00
|
|
|
extern void sethistory (int);
|
2015-01-13 18:16:42 +01:00
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.dol.c
|
|
|
|
+++ sh.dol.c 2013-03-28 09:06:43.000000000 +0000
|
|
|
|
@@ -1120,6 +1120,6 @@ again:
|
2015-01-13 18:16:42 +01:00
|
|
|
*obp = 0;
|
|
|
|
tmp = short2str(obuf);
|
|
|
|
(void) xwrite(0, tmp, strlen (tmp));
|
|
|
|
- (void) lseek(0, (off_t) 0, L_SET);
|
|
|
|
+ (void) lseek(0, (off_t) 0, SEEK_SET);
|
|
|
|
cleanup_until(&inheredoc);
|
|
|
|
}
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.err.c
|
|
|
|
+++ sh.err.c 2013-03-28 09:06:43.000000000 +0000
|
2015-01-13 18:16:42 +01:00
|
|
|
@@ -514,6 +514,22 @@ open_cleanup(void *xptr)
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
+fcntl_cleanup(void *xptr)
|
|
|
|
+{
|
|
|
|
+ int *ptr;
|
|
|
|
+ struct flock fl;
|
|
|
|
+
|
|
|
|
+ ptr = xptr;
|
|
|
|
+
|
|
|
|
+ fl.l_type = F_UNLCK;
|
|
|
|
+ fl.l_whence = SEEK_SET;
|
|
|
|
+ fl.l_start = 0;
|
|
|
|
+ fl.l_len = 0;
|
|
|
|
+
|
|
|
|
+ fcntl(*ptr, F_SETLK, &fl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void
|
|
|
|
opendir_cleanup(void *xdir)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.h
|
|
|
|
+++ sh.h 2013-03-28 09:06:43.000000000 +0000
|
2015-01-13 18:16:42 +01:00
|
|
|
@@ -50,6 +50,24 @@
|
|
|
|
# include <inttypes.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <fcntl.h>
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * History flags.
|
|
|
|
+ */
|
|
|
|
+#define HIST_ONLY 0x001
|
|
|
|
+#define HIST_SAVE 0x002
|
|
|
|
+#define HIST_LOAD 0x004
|
|
|
|
+#define HIST_REV 0x008
|
|
|
|
+#define HIST_CLEAR 0x010
|
|
|
|
+#define HIST_MERGE 0x020
|
|
|
|
+#define HIST_TIME 0x040
|
|
|
|
+#define HIST_FILE_WRLCK 0x080 /* Write lock */
|
|
|
|
+#define HIST_FILE_RDLCK 0x100 /* Read lock */
|
|
|
|
+#define HIST_FILE_OPEN 0x200 /* Leave file open */
|
|
|
|
+#define HIST_FILE_LOCK 0x400 /* Leave file locked */
|
|
|
|
+
|
|
|
|
#if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && !defined(WINNT_NATIVE)
|
|
|
|
typedef unsigned long intptr_t;
|
|
|
|
#endif
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.hist.c
|
|
|
|
+++ sh.hist.c 2015-05-04 14:51:57.995328370 +0000
|
|
|
|
@@ -36,6 +36,8 @@ RCSID("$tcsh: sh.hist.c,v 3.60 2015/02/2
|
|
|
|
|
|
|
|
#include <stdio.h> /* for rename(2), grr. */
|
|
|
|
#include <assert.h>
|
|
|
|
+#include <unistd.h>
|
|
|
|
+#include <sys/types.h>
|
|
|
|
#include "tc.h"
|
|
|
|
#include "dotlock.h"
|
|
|
|
|
|
|
|
@@ -46,14 +48,6 @@ Char HistLit = 0;
|
2015-01-13 18:16:42 +01:00
|
|
|
static int heq (const struct wordent *, const struct wordent *);
|
|
|
|
static void hfree (struct Hist *);
|
|
|
|
|
|
|
|
-#define HIST_ONLY 0x01
|
|
|
|
-#define HIST_SAVE 0x02
|
|
|
|
-#define HIST_LOAD 0x04
|
|
|
|
-#define HIST_REV 0x08
|
|
|
|
-#define HIST_CLEAR 0x10
|
|
|
|
-#define HIST_MERGE 0x20
|
|
|
|
-#define HIST_TIME 0x40
|
|
|
|
-
|
|
|
|
/*
|
|
|
|
* C shell
|
|
|
|
*/
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -145,13 +139,13 @@ discardExcess(int hlen)
|
2015-01-13 18:16:42 +01:00
|
|
|
void
|
|
|
|
savehist(
|
|
|
|
struct wordent *sp,
|
|
|
|
- int mflg) /* true if -m (merge) specified */
|
2015-05-05 12:23:49 +02:00
|
|
|
+ int flg) /* true if -m (merge) specified */
|
2015-01-13 18:16:42 +01:00
|
|
|
{
|
2015-05-05 12:23:49 +02:00
|
|
|
/* throw away null lines */
|
|
|
|
if (sp && sp->next->word[0] == '\n')
|
|
|
|
return;
|
2015-01-13 18:16:42 +01:00
|
|
|
if (sp)
|
|
|
|
- (void) enthist(++eventno, sp, 1, mflg, histlen);
|
|
|
|
+ (void) enthist(++eventno, sp, 1, flg, histlen);
|
|
|
|
discardExcess(histlen);
|
|
|
|
}
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -923,7 +917,7 @@ enthist(
|
2015-01-13 18:16:42 +01:00
|
|
|
int event, /* newly incremented global eventno */
|
|
|
|
struct wordent *lp,
|
|
|
|
int docopy,
|
|
|
|
- int mflg, /* true if merge requested */
|
2015-05-05 12:23:49 +02:00
|
|
|
+ int flg, /* true if merge requested */
|
|
|
|
int hlen) /* -1 if unknown */
|
2015-01-13 18:16:42 +01:00
|
|
|
{
|
|
|
|
struct Hist *p = NULL, *pp = &Histlist, *pTime = NULL;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -943,7 +937,7 @@ enthist(
|
2015-01-13 18:16:42 +01:00
|
|
|
Htime = p->Htime;
|
|
|
|
/* If we are merging, and the old entry is at the place we want
|
|
|
|
* to insert the new entry, then remember the place. */
|
|
|
|
- if (mflg && Htime != 0 && p->Hprev->Htime >= Htime)
|
|
|
|
+ if ((flg & HIST_MERGE) && Htime != 0 && p->Hprev->Htime >= Htime)
|
|
|
|
pTime = p->Hprev;
|
|
|
|
if (!fastMergeErase)
|
|
|
|
renumberHist(p); /* Reset Href of subsequent entries */
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1002,7 +996,7 @@ enthist(
|
2015-01-13 18:16:42 +01:00
|
|
|
/* The head of history list is the default insertion point.
|
|
|
|
If merging, advance insertion point, in pp, according to Htime. */
|
|
|
|
/* XXX -- In histdup=all, Htime values can be non-monotonic. */
|
|
|
|
- if (mflg) { /* merge according to np->Htime */
|
|
|
|
+ if (flg & HIST_MERGE) { /* merge according to np->Htime */
|
|
|
|
pp = mergeInsertionPoint(np, pTime);
|
|
|
|
for (p = pp->Hnext; p && p->Htime == np->Htime; pp = p, p = p->Hnext) {
|
|
|
|
if (heq(&p->Hlex, &np->Hlex)) {
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1041,11 +1035,11 @@ hfree(struct Hist *hp)
|
2015-01-13 18:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_STATIC void
|
|
|
|
-phist(struct Hist *hp, int hflg)
|
|
|
|
+phist(struct Hist *hp, int flg)
|
|
|
|
{
|
2015-05-05 12:23:49 +02:00
|
|
|
if (hp->Href < 0)
|
|
|
|
return;
|
2015-01-13 18:16:42 +01:00
|
|
|
- if (hflg & HIST_ONLY) {
|
|
|
|
+ if (flg & HIST_ONLY) {
|
|
|
|
int old_output_raw;
|
|
|
|
|
|
|
|
/*
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1057,7 +1051,7 @@ phist(struct Hist *hp, int hflg)
|
2015-01-13 18:16:42 +01:00
|
|
|
old_output_raw = output_raw;
|
|
|
|
output_raw = 1;
|
|
|
|
cleanup_push(&old_output_raw, output_raw_restore);
|
|
|
|
- if (hflg & HIST_TIME)
|
|
|
|
+ if (flg & HIST_TIME)
|
|
|
|
/*
|
|
|
|
* Make file entry with history time in format:
|
|
|
|
* "+NNNNNNNNNN" (10 digits, left padded with ascii '0')
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1088,7 +1082,7 @@ phist(struct Hist *hp, int hflg)
|
2015-01-13 18:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PG_STATIC void
|
|
|
|
-dophist(int n, int hflg)
|
|
|
|
+dophist(int n, int flg)
|
|
|
|
{
|
|
|
|
struct Hist *hp;
|
|
|
|
if (setintr) {
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1097,7 +1091,7 @@ dophist(int n, int hflg)
|
2015-01-13 18:16:42 +01:00
|
|
|
pintr_push_enable(&old_pintr_disabled);
|
|
|
|
cleanup_until(&old_pintr_disabled);
|
|
|
|
}
|
|
|
|
- if ((hflg & HIST_REV) == 0) {
|
|
|
|
+ if (!(flg & HIST_REV)) {
|
|
|
|
/* Since the history list is stored most recent first, non-reversing
|
|
|
|
* print needs to print (backwards) up the list. */
|
|
|
|
if ((unsigned)n >= histCount)
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1111,10 +1105,10 @@ dophist(int n, int hflg)
|
2015-01-13 18:16:42 +01:00
|
|
|
if (hp == NULL)
|
|
|
|
return; /* nothing to print */
|
|
|
|
for (; hp != &Histlist; hp = hp->Hprev)
|
|
|
|
- phist(hp, hflg);
|
|
|
|
+ phist(hp, flg);
|
|
|
|
} else {
|
|
|
|
for (hp = Histlist.Hnext; n-- > 0 && hp != NULL; hp = hp->Hnext)
|
|
|
|
- phist(hp, hflg);
|
|
|
|
+ phist(hp, flg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1122,7 +1116,7 @@ dophist(int n, int hflg)
|
2015-01-13 18:16:42 +01:00
|
|
|
void
|
|
|
|
dohist(Char **vp, struct command *c)
|
|
|
|
{
|
|
|
|
- int n, hflg = 0;
|
|
|
|
+ int n, flg = 0;
|
|
|
|
|
|
|
|
USE(c);
|
|
|
|
if (getn(varval(STRhistory)) == 0)
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1133,40 +1127,40 @@ dohist(Char **vp, struct command *c)
|
2015-01-13 18:16:42 +01:00
|
|
|
while (*++vp2)
|
|
|
|
switch (*vp2) {
|
|
|
|
case 'c':
|
|
|
|
- hflg |= HIST_CLEAR;
|
|
|
|
+ flg |= HIST_CLEAR;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
- hflg |= HIST_ONLY;
|
|
|
|
+ flg |= HIST_ONLY;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
- hflg |= HIST_REV;
|
|
|
|
+ flg |= HIST_REV;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
- hflg |= HIST_SAVE;
|
|
|
|
+ flg |= HIST_SAVE;
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
- hflg |= HIST_LOAD;
|
|
|
|
+ flg |= HIST_LOAD;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
- hflg |= HIST_MERGE;
|
|
|
|
+ flg |= HIST_MERGE;
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
- hflg |= HIST_TIME;
|
|
|
|
+ flg |= HIST_TIME;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
stderror(ERR_HISTUS, "chrSLMT");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- if (hflg & HIST_CLEAR) {
|
|
|
|
+ if (flg & HIST_CLEAR) {
|
|
|
|
struct Hist *np, *hp;
|
|
|
|
for (hp = &Histlist; (np = hp->Hnext) != NULL;)
|
|
|
|
hremove(np), hfree(np);
|
|
|
|
}
|
|
|
|
|
|
|
|
- if (hflg & (HIST_LOAD | HIST_MERGE))
|
|
|
|
- loadhist(*vp, (hflg & HIST_MERGE) ? 1 : 0);
|
|
|
|
- else if (hflg & HIST_SAVE)
|
|
|
|
+ if (flg & (HIST_LOAD | HIST_MERGE))
|
|
|
|
+ loadhist(*vp, (flg | HIST_FILE_RDLCK));
|
|
|
|
+ else if (flg & HIST_SAVE)
|
|
|
|
rechist(*vp, 1);
|
|
|
|
else {
|
|
|
|
if (*vp)
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1174,7 +1168,7 @@ dohist(Char **vp, struct command *c)
|
2015-01-13 18:16:42 +01:00
|
|
|
else {
|
|
|
|
n = getn(varval(STRhistory));
|
|
|
|
}
|
|
|
|
- dophist(n, hflg);
|
|
|
|
+ dophist(n, flg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1222,10 +1216,9 @@ dotlock_cleanup(void* lockpath)
|
2015-01-13 18:16:42 +01:00
|
|
|
void
|
|
|
|
rechist(Char *fname, int ref)
|
|
|
|
{
|
2015-05-05 12:23:49 +02:00
|
|
|
- Char *snum, *rs;
|
2015-01-13 18:16:42 +01:00
|
|
|
- int fp, ftmp, oldidfds;
|
2015-05-05 12:23:49 +02:00
|
|
|
+ Char *snum;
|
|
|
|
+ int fd = -1, ftmp, oldidfds;
|
2015-01-13 18:16:42 +01:00
|
|
|
struct varent *shist;
|
2015-05-05 12:23:49 +02:00
|
|
|
- char path[MAXPATHLEN];
|
|
|
|
struct stat st;
|
2015-01-13 18:16:42 +01:00
|
|
|
static Char *dumphist[] = {STRhistory, STRmhT, 0, 0};
|
|
|
|
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1294,46 +1287,49 @@ rechist(Char *fname, int ref)
|
|
|
|
cleanup_push(lockpath, dotlock_cleanup);
|
|
|
|
#endif
|
|
|
|
}
|
2015-01-13 18:16:42 +01:00
|
|
|
- loadhist(fname, 1);
|
2015-05-05 12:23:49 +02:00
|
|
|
+ /* Read .history file, leave it's fd open for writing. */
|
|
|
|
+ fd = loadhist(fname, HIST_MERGE|HIST_FILE_WRLCK|HIST_FILE_OPEN|HIST_FILE_LOCK);
|
|
|
|
+ if (fd > 0) {
|
|
|
|
+ /* Truncate the .history file. */
|
|
|
|
+ (void) ftruncate(fd, 0);
|
|
|
|
+ (void) lseek(fd, (off_t) 0, SEEK_SET);
|
|
|
|
+ }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- rs = randsuf();
|
|
|
|
- xsnprintf(path, sizeof(path), "%S.%S", fname, rs);
|
|
|
|
- xfree(rs);
|
2015-01-13 18:16:42 +01:00
|
|
|
-
|
2015-05-05 12:23:49 +02:00
|
|
|
- fp = xcreat(path, 0600);
|
2015-01-13 18:16:42 +01:00
|
|
|
- if (fp == -1) {
|
|
|
|
- didfds = oldidfds;
|
2015-05-05 12:23:49 +02:00
|
|
|
- cleanup_until(fname);
|
2015-01-13 18:16:42 +01:00
|
|
|
- return;
|
|
|
|
+ if (fd <= 0) {
|
2015-05-05 12:23:49 +02:00
|
|
|
+ /* Open .history file for writing (if not open yet). */
|
2015-01-13 18:16:42 +01:00
|
|
|
+ fd = xopen(short2str(fname), O_LARGEFILE|O_CREAT|O_WRONLY|O_TRUNC, 0600);
|
|
|
|
+ if (fd != -1)
|
|
|
|
+ cleanup_push(&fd, open_cleanup);
|
|
|
|
}
|
2015-05-05 12:23:49 +02:00
|
|
|
/* Try to preserve ownership and permissions of the original history file */
|
|
|
|
#ifndef WINNT_NATIVE
|
|
|
|
if (stat(short2str(fname), &st) != -1) {
|
|
|
|
- TCSH_IGNORE(fchown(fp, st.st_uid, st.st_gid));
|
|
|
|
- TCSH_IGNORE(fchmod(fp, st.st_mode));
|
|
|
|
+ TCSH_IGNORE(fchown(fd, st.st_uid, st.st_gid));
|
|
|
|
+ TCSH_IGNORE(fchmod(fd, st.st_mode));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
UNREFERENCED_PARAMETER(st);
|
|
|
|
#endif
|
2015-01-13 18:16:42 +01:00
|
|
|
- ftmp = SHOUT;
|
|
|
|
- SHOUT = fp;
|
|
|
|
- dumphist[2] = snum;
|
|
|
|
- dohist(dumphist, NULL);
|
|
|
|
- xclose(fp);
|
|
|
|
- SHOUT = ftmp;
|
2015-05-05 12:23:49 +02:00
|
|
|
+ if (fd != -1) {
|
|
|
|
+ ftmp = SHOUT;
|
|
|
|
+ SHOUT = fd;
|
|
|
|
+ dumphist[2] = snum;
|
|
|
|
+ dohist(dumphist, NULL);
|
|
|
|
+ SHOUT = ftmp;
|
|
|
|
+ }
|
2015-01-13 18:16:42 +01:00
|
|
|
didfds = oldidfds;
|
2015-05-05 12:23:49 +02:00
|
|
|
- (void)rename(path, short2str(fname));
|
|
|
|
cleanup_until(fname);
|
2015-01-13 18:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is the entry point for loading history data from a file. */
|
|
|
|
-void
|
|
|
|
-loadhist(Char *fname, int mflg)
|
|
|
|
+int
|
|
|
|
+loadhist(Char *fname, int flg)
|
|
|
|
{
|
|
|
|
static Char *loadhist_cmd[] = {STRsource, NULL, NULL, NULL};
|
|
|
|
- loadhist_cmd[1] = mflg ? STRmm : STRmh;
|
|
|
|
+ int fd;
|
|
|
|
+ loadhist_cmd[1] = (flg & HIST_MERGE) ? STRmm : STRmh;
|
|
|
|
|
|
|
|
if (fname != NULL)
|
|
|
|
loadhist_cmd[2] = fname;
|
2015-05-05 12:23:49 +02:00
|
|
|
@@ -1342,17 +1338,19 @@ loadhist(Char *fname, int mflg)
|
2015-01-13 18:16:42 +01:00
|
|
|
else
|
|
|
|
loadhist_cmd[2] = STRtildothist;
|
|
|
|
|
|
|
|
- dosource(loadhist_cmd, NULL);
|
|
|
|
+ fd = dosource_flg(loadhist_cmd, NULL, flg);
|
|
|
|
|
|
|
|
- /* During history merging (enthist sees mflg set), we disable management of
|
|
|
|
- * Hnum and Href (because fastMergeErase is true). So now reset all the
|
|
|
|
+ /* During history merging (enthist sees merge flag), we disable management
|
|
|
|
+ * of Hnum and Href (because fastMergeErase is true). So now reset all the
|
|
|
|
* values based on the final ordering of the history list. */
|
|
|
|
- if (mflg) {
|
|
|
|
+ if (flg & HIST_MERGE) {
|
|
|
|
int n = eventno;
|
|
|
|
struct Hist *hp = &Histlist;
|
|
|
|
while ((hp = hp->Hnext))
|
|
|
|
hp->Hnum = hp->Href = n--;
|
|
|
|
}
|
|
|
|
+
|
|
|
|
+ return fd; /* Valid/invalid file descriptor (>FSAVE, -1). Zero on error. */
|
|
|
|
}
|
2015-05-05 12:23:49 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
--- sh.lex.c
|
|
|
|
+++ sh.lex.c 2013-03-28 09:06:43.000000000 +0000
|
2015-01-13 18:16:42 +01:00
|
|
|
@@ -1595,7 +1595,7 @@ wide_read(int fildes, Char *buf, size_t
|
|
|
|
/* Throwing away possible partial multibyte characters on error if the
|
|
|
|
stream is not seekable */
|
|
|
|
err = errno;
|
|
|
|
- lseek(fildes, -(off_t)partial, L_INCR);
|
|
|
|
+ lseek(fildes, -(off_t)partial, SEEK_CUR);
|
|
|
|
errno = err;
|
|
|
|
return res != 0 ? res : r;
|
|
|
|
}
|
|
|
|
@@ -1610,7 +1610,7 @@ bgetc(void)
|
|
|
|
if (cantell) {
|
|
|
|
if (fseekp < fbobp || fseekp > feobp) {
|
|
|
|
fbobp = feobp = fseekp;
|
|
|
|
- (void) lseek(SHIN, fseekp, L_SET);
|
|
|
|
+ (void) lseek(SHIN, fseekp, SEEK_SET);
|
|
|
|
}
|
|
|
|
if (fseekp == feobp) {
|
|
|
|
#ifdef WIDE_STRINGS
|
|
|
|
@@ -1814,7 +1814,7 @@ btell(struct Ain *l)
|
|
|
|
void
|
|
|
|
btoeof(void)
|
|
|
|
{
|
|
|
|
- (void) lseek(SHIN, (off_t) 0, L_XTND);
|
|
|
|
+ (void) lseek(SHIN, (off_t) 0, SEEK_END);
|
|
|
|
aret = TCSH_F_SEEK;
|
|
|
|
fseekp = feobp;
|
|
|
|
alvec = NULL;
|
|
|
|
@@ -1832,7 +1832,7 @@ settell(void)
|
|
|
|
cantell = 0;
|
|
|
|
if (arginp || onelflg || intty)
|
|
|
|
return;
|
|
|
|
- if ((x = lseek(SHIN, (off_t) 0, L_INCR)) == -1)
|
|
|
|
+ if ((x = lseek(SHIN, (off_t) 0, SEEK_CUR)) == -1)
|
|
|
|
return;
|
|
|
|
fbuf = xcalloc(2, sizeof(Char **));
|
|
|
|
fblocks = 1;
|
2015-05-05 12:23:49 +02:00
|
|
|
--- sh.sem.c
|
|
|
|
+++ sh.sem.c 2013-03-28 09:06:43.000000000 +0000
|
|
|
|
@@ -903,7 +903,7 @@ doio(struct command *t, int *pipein, int
|
2015-01-13 18:16:42 +01:00
|
|
|
fd = xopen(tmp, O_WRONLY|O_APPEND|O_LARGEFILE);
|
|
|
|
#else /* !O_APPEND */
|
|
|
|
fd = xopen(tmp, O_WRONLY|O_LARGEFILE);
|
|
|
|
- (void) lseek(fd, (off_t) 0, L_XTND);
|
|
|
|
+ (void) lseek(fd, (off_t) 0, SEEK_END);
|
|
|
|
#endif /* O_APPEND */
|
|
|
|
}
|
|
|
|
else
|