From 6341ad7d58adff3f367c8561dd23a30a46391273 Mon Sep 17 00:00:00 2001
From: Roberto Reale <roberto@reale.me>
Date: Sun, 16 Aug 2020 23:02:34 +0200
Subject: [PATCH] csplit: add option -o / --offset

* src/csplit.c: Add option -o / --offset to start counting
file names from a given number
* doc/coreutils.texi: Document option
---
 doc/coreutils.texi |  6 ++++++
 src/csplit.c       | 13 ++++++++++++-
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index a55514d59..7c8b0ab0f 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3613,6 +3613,12 @@ individual output files in turn.  If this option is used, the
 Use output file names containing numbers that are @var{digits} digits
 long instead of the default 2.
 
+@item -o @var{offset}
+@itemx --offset=@var{offset}
+@opindex -n
+@opindex --offset
+Start count of file names at @var{offset} instead of the default 0.
+
 @item -k
 @itemx --keep-files
 @opindex -k
diff --git a/src/csplit.c b/src/csplit.c
index 93ff60dc6..cae494b7e 100644
--- a/src/csplit.c
+++ b/src/csplit.c
@@ -145,6 +145,9 @@ static char *volatile suffix = NULL;
 /* Number of digits to use in output file names. */
 static int volatile digits = 2;
 
+/* Numerical suffix start value.  */
+static int volatile numeric_suffix_start = 0;
+
 /* Number of files created so far. */
 static unsigned int volatile files_created = 0;
 
@@ -192,6 +195,7 @@ enum
 static struct option const longopts[] =
 {
   {"digits", required_argument, NULL, 'n'},
+  {"offset", required_argument, NULL, 'o'},
   {"quiet", no_argument, NULL, 'q'},
   {"silent", no_argument, NULL, 's'},
   {"keep-files", no_argument, NULL, 'k'},
@@ -932,6 +936,7 @@ split_file (void)
 static char *
 make_filename (unsigned int num)
 {
+  num += numeric_suffix_start;
   strcpy (filename_space, prefix);
   if (suffix)
     sprintf (filename_space + strlen (prefix), suffix, num);
@@ -1355,7 +1360,7 @@ main (int argc, char **argv)
   suppress_matched = false;
   prefix = DEFAULT_PREFIX;
 
-  while ((optc = getopt_long (argc, argv, "f:b:kn:sqz", longopts, NULL)) != -1)
+  while ((optc = getopt_long (argc, argv, "f:b:kn:o:sqz", longopts, NULL)) != -1)
     switch (optc)
       {
       case 'f':
@@ -1375,6 +1380,11 @@ main (int argc, char **argv)
                              _("invalid number"), 0);
         break;
 
+      case 'o':
+        numeric_suffix_start = xdectoimax (optarg, 0, MIN (INT_MAX, SIZE_MAX), "",
+                             _("invalid number"), 0);
+        break;
+
       case 's':
       case 'q':
         suppress_count = true;
@@ -1504,6 +1514,7 @@ Read standard input if FILE is -\n\
 "), stdout);
       fputs (_("\
   -n, --digits=DIGITS        use specified number of digits instead of 2\n\
+  -o, --offset=OFFSET        allow setting the start value\n\
   -s, --quiet, --silent      do not print counts of output file sizes\n\
   -z, --elide-empty-files    remove empty output files\n\
 "), stdout);
-- 
2.18.2

