split.c (bcd34af4731db389706eda26b068ce5a880e3d9b) split.c (5c053aa3c5e907bdd1ac466ce9b58611781c2c20)
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 56 unchanged lines hidden (view full) ---

65
66#define DEFLINE 1000 /* Default num lines per file. */
67
68static off_t bytecnt; /* Byte count to split on. */
69static off_t chunks = 0; /* Chunks count to split into. */
70static long numlines; /* Line count to split on. */
71static int file_open; /* If a file open. */
72static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 56 unchanged lines hidden (view full) ---

65
66#define DEFLINE 1000 /* Default num lines per file. */
67
68static off_t bytecnt; /* Byte count to split on. */
69static off_t chunks = 0; /* Chunks count to split into. */
70static long numlines; /* Line count to split on. */
71static int file_open; /* If a file open. */
72static int ifd = -1, ofd = -1; /* Input/output file descriptors. */
73static char bfr[MAXBSIZE]; /* I/O buffer. */
74static char fname[MAXPATHLEN]; /* File name prefix. */
75static regex_t rgx;
76static int pflag;
77static bool dflag;
78static long sufflen = 2; /* File name suffix length. */
79
80static void newfile(void);
81static void split1(void);

--- 116 unchanged lines hidden (view full) ---

198
199/*
200 * split1 --
201 * Split the input by bytes.
202 */
203static void
204split1(void)
205{
73static char fname[MAXPATHLEN]; /* File name prefix. */
74static regex_t rgx;
75static int pflag;
76static bool dflag;
77static long sufflen = 2; /* File name suffix length. */
78
79static void newfile(void);
80static void split1(void);

--- 116 unchanged lines hidden (view full) ---

197
198/*
199 * split1 --
200 * Split the input by bytes.
201 */
202static void
203split1(void)
204{
205 static char bfr[MAXBSIZE];
206 off_t bcnt;
207 char *C;
208 ssize_t dist, len;
209 int nfiles;
210
211 nfiles = 0;
212
213 for (bcnt = 0;;)
206 off_t bcnt;
207 char *C;
208 ssize_t dist, len;
209 int nfiles;
210
211 nfiles = 0;
212
213 for (bcnt = 0;;)
214 switch ((len = read(ifd, bfr, MAXBSIZE))) {
214 switch ((len = read(ifd, bfr, sizeof(bfr)))) {
215 case 0:
216 exit(0);
217 case -1:
218 err(EX_IOERR, "read");
219 /* NOTREACHED */
220 default:
221 if (!file_open) {
222 if (!chunks || (nfiles < chunks)) {

--- 36 unchanged lines hidden (view full) ---

259
260/*
261 * split2 --
262 * Split the input by lines.
263 */
264static void
265split2(void)
266{
215 case 0:
216 exit(0);
217 case -1:
218 err(EX_IOERR, "read");
219 /* NOTREACHED */
220 default:
221 if (!file_open) {
222 if (!chunks || (nfiles < chunks)) {

--- 36 unchanged lines hidden (view full) ---

259
260/*
261 * split2 --
262 * Split the input by lines.
263 */
264static void
265split2(void)
266{
267 char *buf;
268 size_t bufsize;
269 ssize_t len;
267 long lcnt = 0;
268 FILE *infp;
269
270 long lcnt = 0;
271 FILE *infp;
272
273 buf = NULL;
274 bufsize = 0;
275
270 /* Stick a stream on top of input file descriptor */
271 if ((infp = fdopen(ifd, "r")) == NULL)
272 err(EX_NOINPUT, "fdopen");
273
274 /* Process input one line at a time */
276 /* Stick a stream on top of input file descriptor */
277 if ((infp = fdopen(ifd, "r")) == NULL)
278 err(EX_NOINPUT, "fdopen");
279
280 /* Process input one line at a time */
275 while (fgets(bfr, sizeof(bfr), infp) != NULL) {
276 const int len = strlen(bfr);
277
278 /* If line is too long to deal with, just write it out */
279 if (bfr[len - 1] != '\n')
280 goto writeit;
281
281 while ((len = getline(&buf, &bufsize, infp)) > 0) {
282 /* Check if we need to start a new file */
283 if (pflag) {
284 regmatch_t pmatch;
285
286 pmatch.rm_so = 0;
287 pmatch.rm_eo = len - 1;
282 /* Check if we need to start a new file */
283 if (pflag) {
284 regmatch_t pmatch;
285
286 pmatch.rm_so = 0;
287 pmatch.rm_eo = len - 1;
288 if (regexec(&rgx, bfr, 0, &pmatch, REG_STARTEND) == 0)
288 if (regexec(&rgx, buf, 0, &pmatch, REG_STARTEND) == 0)
289 newfile();
290 } else if (lcnt++ == numlines) {
291 newfile();
292 lcnt = 1;
293 }
294
289 newfile();
290 } else if (lcnt++ == numlines) {
291 newfile();
292 lcnt = 1;
293 }
294
295writeit:
296 /* Open output file if needed */
297 if (!file_open)
298 newfile();
299
300 /* Write out line */
295 /* Open output file if needed */
296 if (!file_open)
297 newfile();
298
299 /* Write out line */
301 if (write(ofd, bfr, len) != len)
300 if (write(ofd, buf, len) != len)
302 err(EX_IOERR, "write");
303 }
304
305 /* EOF or error? */
301 err(EX_IOERR, "write");
302 }
303
304 /* EOF or error? */
306 if (ferror(infp))
305 if ((len == -1 && errno != 0) || ferror(infp))
307 err(EX_IOERR, "read");
308 else
309 exit(0);
310}
311
312/*
313 * split3 --
314 * Split the input into specified number of chunks

--- 90 unchanged lines hidden ---
306 err(EX_IOERR, "read");
307 else
308 exit(0);
309}
310
311/*
312 * split3 --
313 * Split the input into specified number of chunks

--- 90 unchanged lines hidden ---