This is Refactoring / code cleanup.
See:
* https://refactoring.com/catalog/extractMethod.html
* https://en.wikipedia.org/wiki/Code_refactoring
* https://www.refactoring.com/
* https://www.joelonsoftware.com/2002/01/23/rub-a-dub-dub/
Some small optimisations may have slipped in as well.
--- /dev/null
+#pragma once
+
+static void input_fn_2_data_fn(void)
+{
+ if (strlen(input_filename) > COUNT(data_filename) - 10)
+ {
+ perror("input is too long");
+ exit(1);
+ }
+ /* Hmm. Don't output anything if we can help it.
+ * fprintf(stderr, "Input file: %s\n",input_filename); */
+ char *const extc = strrchr(input_filename, '.');
+ if (!extc)
+ {
+ sprintf(data_filename, "%s.dat", input_filename);
+ }
+ else
+ {
+ strcpy(data_filename, input_filename);
+ *extc = '\0';
+ }
+}
static off_t pos, Seekpts[2]; /* seek pointers to fortunes */
+#include "fortune-util.h"
+
static void getargs(char *av[])
{
av += optind + 1;
if (*av)
{
input_filename = *av;
- if (strlen(input_filename) > COUNT(data_filename) - 10)
- {
- perror("input is too long");
- exit(1);
- }
- /* Hmm. Don't output anything if we can help it.
- * fprintf(stderr, "Input file: %s\n",input_filename); */
- char *const extc = strrchr(input_filename, '.');
- if (!extc)
- {
- sprintf(data_filename, "%s.dat", input_filename);
- }
- else
- {
- strcpy(data_filename, input_filename);
- *extc = '\0';
- }
+ input_fn_2_data_fn();
}
else
/* {
static FILE *Inf, *Dataf, *Outf;
+#include "fortune-util.h"
+
/* ARGSUSED */
static void getargs(int ac, char *av[])
{
if (*av)
{
input_filename = *av;
- fprintf(stderr, "Input file: %s\n", input_filename);
- if (strlen(input_filename) > COUNT(data_filename) - 10)
- {
- perror("input is too long");
- exit(1);
- }
- char *const extc = strrchr(input_filename, '.');
- if (!extc)
- {
- sprintf(data_filename, "%s.dat", input_filename);
- }
- else
- {
- strcpy(data_filename, input_filename);
- *extc = '\0';
- }
+ input_fn_2_data_fn();
if (*++av)
{
strcpy(output_filename, *av);