From nobody Tue May 30 13:13:50 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QVtBl1gbgz4XSqF; Tue, 30 May 2023 13:13:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QVtBl1BvWz4Jdb; Tue, 30 May 2023 13:13:51 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1685452431; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mHQz7m5M/ahV+KiL7os/PF59M2LlUfcTULDzoddF9DA=; b=C4u4CvbYPG4mofmJGo0SDS7eHbtTwiqSgUMX+0DWRm/I2epvSJkV9fUSh0astmrqNj6/L+ og9S4kffksoWY3r9CaTdOW7upMeKMy8suK7JE5LixYGXr+J+BepaeiVHgll4+RLOrJHZ/g Ia6WYTrgCahaTKQKTxtquCvMQ7+4ASjD4+QOY9/71WSAJCN+w2qoL/DbPGV8KgokYNNEro uRRbdFBviW7KOFdSKdOuPwMQNBzjZ3z2gx3xbe7IG/56ImviZia6FdOtmGPrSisJ2MMzY4 06lT+3hgpHOqoC7CJwAgqqVeg2eio3Cce2tyuxV9++7NxHWKuWfvjmUzuSo0Gw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1685452431; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=mHQz7m5M/ahV+KiL7os/PF59M2LlUfcTULDzoddF9DA=; b=tmcf0r2xXhx/50RLIEJrHOa4G6i03z0wlW4KQRoCqTUFF4nM0x3W9TTWDbm72skbzKduy7 YV4jImR5MWDLYB4h33jZqx5xIptsAwvEXM+omAp3j5a+T/ocTGnxBR4yMWyu4UhL43DcL1 Sus3k3FfFE+h6AUtFLpW/jkjMqgvJ272TWZfEjARjL4wGiit6Yn4dPXMjW+QtZvu3FS3Ok Iuki76OWvQLAUAV9KSNo6FIKAWyfXGqTY+YfAuY49CD6fYbFfPxnmh0XxMuXZ+WUjRFzca vPvNatcVPlfYGRn/n70RcPAtApPbQLGR/WLjN5jT6NYv3/p3K/J0ufKxUGMN2w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1685452431; a=rsa-sha256; cv=none; b=qYXc53LTW6oFn/LPNjlaYoHYSnhkKBP3/hMOc2fdzh+TXwOkEzmU0E2HoeK3gbgjyoLlQr oMsi7TMkWFsnZhUqQG7TKXuCg+pArSpCf4IhDDOB2Fk6Dr+IrF70suVpycF/22L0uFJwCO 0IdiKpQaO8eADE58gE08BciMDI7zfbKa8KHh1QEiB0u2gwzjKVIUBJ2T0s9XqNg5mOJ++J Zrg4RTXS9asLw+WYqrvhR/TU3e8H4Ls02JUT4nifD06di9gZKGULNVynp93DDoZIrqaxUU +inoj9IvfRhKtZhbMM1hbrJr1twBrVEGzGgyNyZDIpdyYO0fHqTVUsKoGXHLKw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QVtBl0Fy6zvbp; Tue, 30 May 2023 13:13:51 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 34UDDorJ033748; Tue, 30 May 2023 13:13:50 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 34UDDole033747; Tue, 30 May 2023 13:13:50 GMT (envelope-from git) Date: Tue, 30 May 2023 13:13:50 GMT Message-Id: <202305301313.34UDDole033747@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Christos Margiolis Subject: git: ac17fc816e67 - main - split(1): add '-c' to continue creating files List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: christos X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: ac17fc816e67a4e5e2e481b5001577a8d589f8b6 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by christos: URL: https://cgit.FreeBSD.org/src/commit/?id=ac17fc816e67a4e5e2e481b5001577a8d589f8b6 commit ac17fc816e67a4e5e2e481b5001577a8d589f8b6 Author: Jan Schaumann AuthorDate: 2023-05-30 13:13:16 +0000 Commit: Christos Margiolis CommitDate: 2023-05-30 13:13:16 +0000 split(1): add '-c' to continue creating files Currently, split(1) will clobber any existing output files: $ split file; ls xaa xab xac xad $ split second-file; ls xaa xab xac xad xae xaf This patch adds a flag "-c" (mnemonic "create, don't overwrite" or "continue where you left off"): $ split file; ls xaa xab xac xad $ split -c second-file; ls xaa xab xac xad xae xaf xag xah xai xaj Reviewed by: christos Approved by: kevans Different Revision: https://reviews.freebsd.org/D38553 --- usr.bin/split/split.1 | 20 ++++++++++++++++---- usr.bin/split/split.c | 28 ++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/usr.bin/split/split.1 b/usr.bin/split/split.1 index ee7c3d412db4..31a82073a801 100644 --- a/usr.bin/split/split.1 +++ b/usr.bin/split/split.1 @@ -36,12 +36,12 @@ .Nd split a file into pieces .Sh SYNOPSIS .Nm -.Op Fl d +.Op Fl cd .Op Fl l Ar line_count .Op Fl a Ar suffix_length .Op Ar file Op Ar prefix .Nm -.Op Fl d +.Op Fl cd .Fl b Ar byte_count Ns .Oo .Sm off @@ -51,12 +51,12 @@ .Op Fl a Ar suffix_length .Op Ar file Op Ar prefix .Nm -.Op Fl d +.Op Fl cd .Fl n Ar chunk_count .Op Fl a Ar suffix_length .Op Ar file Op Ar prefix .Nm -.Op Fl d +.Op Fl cd .Fl p Ar pattern .Op Fl a Ar suffix_length .Op Ar file Op Ar prefix @@ -112,6 +112,9 @@ or is appended to the number, the file is split into .Ar byte_count gigabyte pieces. +.It Fl c +Continue creating files and do not overwrite existing +output files. .It Fl d Use a numeric suffix instead of a alphabetic suffix. .It Fl l Ar line_count @@ -163,6 +166,15 @@ argument is not specified, the file is split into lexically ordered files named with the prefix .Dq Li x and with suffixes as above. +.Pp +By default, +.Nm +will overwrite any existing output files. +If the +.Fl c +flag is specified, +.Nm +will instead create files with names that do not already exist. .Sh ENVIRONMENT The .Ev LANG , LC_ALL , LC_CTYPE diff --git a/usr.bin/split/split.c b/usr.bin/split/split.c index 769567b28325..91710899a9e9 100644 --- a/usr.bin/split/split.c +++ b/usr.bin/split/split.c @@ -67,6 +67,7 @@ static const char sccsid[] = "@(#)split.c 8.2 (Berkeley) 4/16/94"; static off_t bytecnt; /* Byte count to split on. */ static off_t chunks = 0; /* Chunks count to split into. */ +static bool clobber = true; /* Whether to overwrite existing output files. */ static long numlines; /* Line count to split on. */ static int file_open; /* If a file open. */ static int ifd = -1, ofd = -1; /* Input/output file descriptors. */ @@ -93,7 +94,7 @@ main(int argc, char **argv) setlocale(LC_ALL, ""); dflag = false; - while ((ch = getopt(argc, argv, "0123456789a:b:dl:n:p:")) != -1) + while ((ch = getopt(argc, argv, "0123456789a:b:cdl:n:p:")) != -1) switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -125,6 +126,9 @@ main(int argc, char **argv) if (error == -1) errx(EX_USAGE, "%s: offset too large", optarg); break; + case 'c': /* Continue, don't overwrite output files. */ + clobber = false; + break; case 'd': /* Decimal suffix */ dflag = true; break; @@ -347,6 +351,10 @@ newfile(void) static char *fpnt; char beg, end; int pattlen; + int flags = O_WRONLY | O_CREAT | O_TRUNC; + + if (!clobber) + flags |= O_EXCL; if (ofd == -1) { if (fname[0] == '\0') { @@ -355,9 +363,10 @@ newfile(void) } else { fpnt = fname + strlen(fname); } - ofd = fileno(stdout); - } + } else if (close(ofd) != 0) + err(1, "%s", fname); + again: if (dflag) { beg = '0'; end = '9'; @@ -417,8 +426,11 @@ newfile(void) fpnt[sufflen] = '\0'; ++fnum; - if (!freopen(fname, "w", stdout)) + if ((ofd = open(fname, flags, DEFFILEMODE)) < 0) { + if (!clobber && errno == EEXIST) + goto again; err(EX_IOERR, "%s", fname); + } file_open = 1; } @@ -426,9 +438,9 @@ static void usage(void) { (void)fprintf(stderr, -"usage: split [-d] [-l line_count] [-a suffix_length] [file [prefix]]\n" -" split [-d] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" -" split [-d] -n chunk_count [-a suffix_length] [file [prefix]]\n" -" split [-d] -p pattern [-a suffix_length] [file [prefix]]\n"); +"usage: split [-cd] [-l line_count] [-a suffix_length] [file [prefix]]\n" +" split [-cd] -b byte_count[K|k|M|m|G|g] [-a suffix_length] [file [prefix]]\n" +" split [-cd] -n chunk_count [-a suffix_length] [file [prefix]]\n" +" split [-cd] -p pattern [-a suffix_length] [file [prefix]]\n"); exit(EX_USAGE); }