From b392d5450bd3d98512d558a0bdec0c0087db1489 Mon Sep 17 00:00:00 2001 From: dsl Date: Sat, 31 Oct 2009 21:54:01 +0000 Subject: [PATCH] Avoid generating a corrupt history file if multiple ksh exit together. Fixes PR/28912 --- bin/ksh/history.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/bin/ksh/history.c b/bin/ksh/history.c index 8c2fbf94561b..66498c5e7092 100644 --- a/bin/ksh/history.c +++ b/bin/ksh/history.c @@ -1,4 +1,4 @@ -/* $NetBSD: history.c,v 1.9 2005/06/26 19:09:00 christos Exp $ */ +/* $NetBSD: history.c,v 1.10 2009/10/31 21:54:01 dsl Exp $ */ /* * command history @@ -19,7 +19,7 @@ #include #ifndef lint -__RCSID("$NetBSD: history.c,v 1.9 2005/06/26 19:09:00 christos Exp $"); +__RCSID("$NetBSD: history.c,v 1.10 2009/10/31 21:54:01 dsl Exp $"); #endif @@ -740,20 +740,27 @@ void hist_finish() { static int once; + int fd; FILE *fh; register int i; register char **hp; if (once++) return; + if (hname == NULL || hname[0] == 0) + return; + /* check how many we have */ i = histptr - histlist; if (i >= histsize) hp = &histptr[-histsize]; else hp = histlist; - if (hname && (fh = fopen(hname, "w"))) - { + + fd = open(hname, O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK, 0777); + /* Remove anything written before we got the lock */ + ftruncate(fd, 0); + if (fd >= 0 && (fh = fdopen(fd, "w"))) { for (i = 0; hp + i <= histptr && hp[i]; i++) fprintf(fh, "%s%c", hp[i], '\0'); fclose(fh);