Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 30 Mar 2002 03:00:14 -0800 (PST)
From:      "Tim J. Robbins" <tim@robbins.dropbear.id.au>
To:        freebsd-standards@FreeBSD.org
Subject:   Re: standards/36191: P1003.1-2001 csplit utility
Message-ID:  <200203301100.g2UB0EW11994@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help
The following reply was made to PR standards/36191; it has been noted by GNATS.

From: "Tim J. Robbins" <tim@robbins.dropbear.id.au>
To: Stefan Farfeleder <e0026813@stud3.tuwien.ac.at>
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 <sys/cdefs.h>
  __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 <sys/types.h>
  
 @@ -53,6 +53,7 @@
  #include <errno.h>
  #include <limits.h>
  #include <regex.h>
 +#include <signal.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
 @@ -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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200203301100.g2UB0EW11994>