From owner-freebsd-standards Sat Mar 30 3: 0:27 2002 Delivered-To: freebsd-standards@hub.freebsd.org Received: from freefall.freebsd.org (unknown [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id 7AA3B37B405 for ; Sat, 30 Mar 2002 03:00:15 -0800 (PST) Received: (from gnats@localhost) by freefall.freebsd.org (8.11.6/8.11.6) id g2UB0EW11994; Sat, 30 Mar 2002 03:00:14 -0800 (PST) (envelope-from gnats) Date: Sat, 30 Mar 2002 03:00:14 -0800 (PST) Message-Id: <200203301100.g2UB0EW11994@freefall.freebsd.org> To: freebsd-standards@FreeBSD.org Cc: From: "Tim J. Robbins" Subject: Re: standards/36191: P1003.1-2001 csplit utility Reply-To: "Tim J. Robbins" Sender: owner-freebsd-standards@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG The following reply was made to PR standards/36191; it has been noted by GNATS. From: "Tim J. Robbins" To: Stefan Farfeleder Cc: freebsd-gnats-submit@FreeBSD.ORG Subject: Re: standards/36191: P1003.1-2001 csplit utility Date: Sat, 30 Mar 2002 21:49:24 +1100 On Wed, Mar 27, 2002 at 06:10:03AM -0800, Stefan Farfeleder wrote: > * Posix says: ``An error shall be reported if an operand does not > reference a line between the current position and the end of the > file.'' > csplit now reports "out of range" if EOF is found before the line it > is looking for. > > * The computation of 10 ** sufflen does not used signed overflow. > > * There's a single call to ftell, all others are ftello. I think this > was a typo. Thanks for pointing these out. > Issues to be resolved: > > * Posix requires removal of output files on SIGHUP, SIGINT and SIGTERM. > A simple handler calling cleanup() would do this. [Is there any danger > in calling snprintf() from a signal handler?] > > * I think a matching re on the very first line must create an empty file > xx00. I have now fixed both of these. The manual page for sigaction doesn't say its safe to call snprintf() from a signal handler, but it still looks safe to do it. The alternatives are messy, less efficient, and would take longer to die after receiving the signal. --- csplit.c 2002/03/30 09:48:18 1.22 +++ csplit.c 2002/03/30 10:47:22 1.27 @@ -1,4 +1,4 @@ -/*- +*- * Copyright (c) 2002 Tim J. Robbins. * All rights reserved. * @@ -44,7 +44,7 @@ #include __FBSDID("$FreeBSD$"); -__RCSID("$Id: csplit.c,v 1.22 2002/03/30 09:48:18 tim Exp $"); +__RCSID("$Id: csplit.c,v 1.27 2002/03/30 10:47:22 tim Exp $"); #include @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -62,6 +63,7 @@ void do_lineno(const char *expr); void do_rexp(const char *expr); char *getline(void); +void handlesig(int sig); FILE *newfile(void); void toomuch(FILE *ofp, long n); void usage(void); @@ -87,6 +89,17 @@ FILE *overfile; /* Overflow file for toomuch() */ off_t truncofs; /* Offset this file should be truncated at */ int doclean; /* Should cleanup() remove output? */ +int doneregexp; /* Matched a regular expression pat. yet? */ + +void +handlesig(int sig) +{ + const char msg[] = "Caught fatal signal, cleaning up\n"; + + write(STDERR_FILENO, msg, sizeof(msg) - 1); + cleanup(); + _exit(2); +} /* Create a new output file */ FILE * @@ -245,13 +258,14 @@ while ((p = getline()) != NULL) { if (fputs(p, ofp) != 0) break; - if (!first && regexec(&cre, p, 0, NULL, 0) == 0) + if ((doneregexp || !first) && regexec(&cre, p, 0, NULL, 0) == 0) break; first = 0; } + doneregexp = 1; if (p == NULL) - errx(1, "%s: out of range", expr); + errx(1, "%s: no match", re); if (ofs <= 0) { /* @@ -369,12 +383,16 @@ if (!kflag) { doclean = 1; atexit(cleanup); + signal(SIGHUP, handlesig); + signal(SIGINT, handlesig); + signal(SIGTERM, handlesig); } lineno = 0; nfiles = 0; truncofs = 0; overfile = NULL; + doneregexp = 0; for (maxfiles = 1, i = 0; i < sufflen; i++) { if (maxfiles > LONG_MAX / 10) To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-standards" in the body of the message