int safe_read ();
-static void cleanup __P ((void));
static void close_output_file __P ((void));
static void create_output_file __P ((void));
-static void save_line_to_file __P ((struct cstring *line));
+static void delete_all_files __P ((void));
+static void save_line_to_file __P ((const struct cstring *line));
static void usage __P ((int status));
/* The name this program was run with. */
{NULL, 0, NULL, 0}
};
+/* Optionally remove files created so far; then exit.
+ Called when an error detected. */
+
+static void
+cleanup (void)
+{
+ if (output_stream)
+ close_output_file ();
+
+ if (remove_files)
+ delete_all_files ();
+}
+
+static void
+cleanup_fatal (void)
+{
+ cleanup ();
+ exit (1);
+}
+
+static RETSIGTYPE
+interrupt_handler (int sig)
+{
+#ifdef SA_INTERRUPT
+ struct sigaction sigact;
+
+ sigact.sa_handler = SIG_DFL;
+ sigemptyset (&sigact.sa_mask);
+ sigact.sa_flags = 0;
+ sigaction (sig, &sigact, NULL);
+#else /* !SA_INTERRUPT */
+ signal (sig, SIG_DFL);
+#endif /* SA_INTERRUPT */
+ cleanup ();
+ kill (getpid (), sig);
+}
+
/* Allocate N bytes of memory dynamically, with error checking. */
static char *
if (p == NULL)
{
error (0, 0, _("virtual memory exhausted"));
- cleanup ();
+ cleanup_fatal ();
}
return p;
}
if (p == NULL)
{
error (0, 0, _("virtual memory exhausted"));
- cleanup ();
+ cleanup_fatal ();
}
return p;
}
if (bytes_read < 0)
{
error (0, errno, _("read error"));
- cleanup ();
+ cleanup_fatal ();
}
return bytes_read;
/* Set the name of the input file to NAME and open it. */
static void
-set_input_file (char *name)
+set_input_file (const char *name)
{
if (!strcmp (name, "-"))
input_desc = 0;
if (first_line > last_line)
{
error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
- cleanup ();
+ cleanup_fatal ();
}
lines = last_line - first_line;
if (line == NULL)
{
error (0, 0, _("%s: line number out of range"), global_argv[argnum]);
- cleanup ();
+ cleanup_fatal ();
}
if (!ignore)
save_line_to_file (line);
on iteration REPETITION if nonzero. */
static void
-handle_line_error (struct control *p, int repetition)
+handle_line_error (const struct control *p, int repetition)
{
fprintf (stderr, _("%s: `%d': line number out of range"),
program_name, p->lines_required);
else
fprintf (stderr, "\n");
- cleanup ();
+ cleanup_fatal ();
}
/* Determine the line number that marks the end of this file,
REPETITION is the repetition number. */
static void
-process_line_count (struct control *p, int repetition)
+process_line_count (const struct control *p, int repetition)
{
unsigned int linenum;
unsigned int last_line_to_save = p->lines_required * (repetition + 1);
dump_rest_of_file ();
close_output_file ();
}
- cleanup ();
+ cleanup_fatal ();
}
/* Read the input until a line matches the regexp in P, outputting
if (ret == -2)
{
error (0, 0, _("error in regular expression search"));
- cleanup ();
+ cleanup_fatal ();
}
if (ret == -1)
{
if (ret == -2)
{
error (0, 0, _("error in regular expression search"));
- cleanup ();
+ cleanup_fatal ();
}
if (ret >= 0)
break;
if (output_stream == NULL)
{
error (0, errno, "%s", output_filename);
- cleanup ();
+ cleanup_fatal ();
}
files_created++;
bytes_written = 0;
{
error (0, errno, _("write error for `%s'"), output_filename);
output_stream = NULL;
- cleanup ();
+ cleanup_fatal ();
}
if (bytes_written == 0 && elide_empty_files)
{
}
}
-/* Optionally remove files created so far; then exit.
- Called when an error detected. */
-
-static void
-cleanup (void)
-{
- if (output_stream)
- close_output_file ();
-
- if (remove_files)
- delete_all_files ();
-
- exit (1);
-}
-
/* Save line LINE to the output file and
increment the character count for the current file. */
static void
-save_line_to_file (struct cstring *line)
+save_line_to_file (const struct cstring *line)
{
fwrite (line->str, sizeof (char), line->len, output_stream);
bytes_written += line->len;
/* FIXME: use xstrtoul in place of this function. */
static boolean
-string_to_number (int *result, char *num)
+string_to_number (int *result, const char *num)
{
char ch;
int val = 0;
NUM is the numeric part of STR. */
static void
-check_for_offset (struct control *p, char *str, char *num)
+check_for_offset (struct control *p, const char *str, const char *num)
{
if (*num != '-' && *num != '+')
error (1, 0, _("%s: `+' or `-' expected after delimeter"), str);
if (err)
{
error (0, 0, _("%s: invalid regular expression: %s"), str, err);
- cleanup ();
+ cleanup_fatal ();
}
if (closing_delim[1])
return out_count;
}
-static void
-interrupt_handler (int signum)
-{
- error (0, 0, _("interrupted"));
- cleanup ();
-}
-
void
main (int argc, char **argv)
{
if (close (input_desc) < 0)
{
error (0, errno, _("read error"));
- cleanup ();
+ cleanup_fatal ();
}
exit (0);