• release flock after the lseek

• -1 not 0 is the safe “none” value for fds
This commit is contained in:
tg
2011-12-30 20:35:29 +00:00
parent 31bf077591
commit 38dcf950c9

View File

@ -26,7 +26,7 @@
#include <sys/file.h> #include <sys/file.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.113 2011/12/10 14:12:16 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.114 2011/12/30 20:35:29 tg Exp $");
Trap sigtraps[NSIG + 1]; Trap sigtraps[NSIG + 1];
static struct sigaction Sigact_ign; static struct sigaction Sigact_ign;
@ -71,7 +71,7 @@ static Source *hist_source;
/* current history file: name, fd, size */ /* current history file: name, fd, size */
static char *hname; static char *hname;
static int histfd; static int histfd = -1;
static size_t hsize; static size_t hsize;
#endif #endif
@ -576,10 +576,10 @@ sethistfile(const char *name)
/* /*
* it's a new name - possibly * it's a new name - possibly
*/ */
if (histfd) { if (histfd != -1) {
/* yes the file is open */ /* yes the file is open */
(void)close(histfd); (void)close(histfd);
histfd = 0; histfd = -1;
hsize = 0; hsize = 0;
afree(hname, APERM); afree(hname, APERM);
hname = NULL; hname = NULL;
@ -617,7 +617,7 @@ histsync(void)
{ {
bool changed = false; bool changed = false;
if (histfd) { if (histfd != -1) {
int lno = hist_source->line; int lno = hist_source->line;
hist_source->line++; hist_source->line++;
@ -656,7 +656,7 @@ histsave(int *lnp, const char *cmd, bool dowrite MKSH_A_UNUSED, bool ignoredups)
++*lnp; ++*lnp;
#if HAVE_PERSISTENT_HISTORY #if HAVE_PERSISTENT_HISTORY
if (histfd && dowrite) if (dowrite && histfd != -1)
writehistfile(*lnp, c); writehistfile(*lnp, c);
#endif #endif
@ -778,11 +778,11 @@ hist_init(Source *s)
histload(hist_source, base+2, hsize-2); histload(hist_source, base+2, hsize-2);
munmap(caddr_cast(base), hsize); munmap(caddr_cast(base), hsize);
} }
(void)flock(histfd, LOCK_UN);
hfsize = lseek(histfd, (off_t)0, SEEK_END); hfsize = lseek(histfd, (off_t)0, SEEK_END);
hsize = 1024 * 1048576; hsize = 1024 * 1048576;
if (hfsize < (off_t)hsize) if (hfsize < (off_t)hsize)
hsize = hfsize; hsize = hfsize;
(void)flock(histfd, LOCK_UN);
#endif #endif
} }
@ -1045,7 +1045,7 @@ hist_finish(void)
{ {
(void)flock(histfd, LOCK_UN); (void)flock(histfd, LOCK_UN);
(void)close(histfd); (void)close(histfd);
histfd = 0; histfd = -1;
} }
/* /*