/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* csplit - Context or line file splitter
* Compile: cc -O -s -o csplit csplit.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <regexpr.h>
#include <signal.h>
#include <locale.h>
#include <libintl.h>
#define FALSE 0
/* Globals */
char *expbuf;
char *sptr;
int errflg;
extern int optind;
extern char *optarg;
/*
* These defines are needed for regexp handling(see regexp(7))
*/
static int asc_to_ll(char *, long long *);
static void closefile(void);
static void fatal(char *, char *);
static void flush(void);
static char *getaline(int);
static void line_arg(char *);
static void num_arg(char *, int);
static void re_arg(char *);
static void sig(int);
static void usage(void);
int
{
char *ptr;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
switch (ch) {
case 'f':
else
ptr++;
break;
case 'n': /* POSIX.2 */
break;
case 'k':
keep++;
break;
case 's':
silent++;
break;
case '?':
errflg++;
}
}
usage();
"No space left on device\n"));
exit(1);
} else {
"Bad write to temporary "
"file\n"));
exit(1);
}
/* clear the buffer to get correct size when writing buffer */
}
++argv;
/*
* The following for loop handles the different argument types.
* A switch is performed on the first character of the argument
* and each case calls the appropriate argument handling routine.
*/
switch (**argv) {
case '/':
break;
case '%':
break;
case '{':
break;
default:
break;
}
}
return (0);
}
/*
* asc_to_ll takes an ascii argument(str) and converts it to a long long(plc)
* It returns ERR if an illegal character. The reason that asc_to_ll
* does not return an answer(long long) is that any value for the long
* long is legal, and this version of asc_to_ll detects error strings.
*/
static int
{
int f;
*plc = 0;
f = 0;
for (; ; str++) {
switch (*str) {
case ' ':
case '\t':
continue;
case '-':
f++;
/* FALLTHROUGH */
case '+':
str++;
}
break;
}
else
return (ERR);
if (f)
return (TRUE); /* not error */
}
/*
* Closefile prints the byte count of the file created,(via fseeko
* and ftello), if the create flag is on and the silent flag is not on.
* If the create flag is on closefile then closes the file(fclose).
*/
static void
{
}
if (create)
}
/*
* Fatal handles error messages and cleanup.
* Because "arg" can be the global file, and the cleanup processing
* uses the global file, the error message is printed first. If the
* "keep" flag is not set, fatal unlinks all created files. If the
* "keep" flag is set, fatal closes the current file(if there is one).
* Fatal exits with a value of 1.
*/
static void
{
char *fls;
int num;
/* gettext dynamically replaces string */
if (!keep) {
if (outfile) {
continue;
}
}
} else
if (outfile)
closefile();
exit(1);
}
/*
* Findline returns the line number referenced by the current argument.
* Its arguments are a pointer to the compiled regular expression(expr),
* and an offset(oset). The variable lncnt is used to count the number
* of lines searched. First the current stream location is saved via
* ftello(), and getaline is called so that R.E. searching starts at the
* line after the previously referenced line. The while loop checks
* that there are more lines(error if none), bumps the line count, and
* checks for the R.E. on each line. If the R.E. matches on one of the
* lines the old stream location is restored, and the line number
* referenced by the R.E. and the offset is returned.
*/
static offset_t
{
static int benhere = 0;
else
lncnt--;
benhere = 1;
lncnt++;
*sptr = '\0';
}
}
}
/*
* Flush uses fputs to put lines on the output file stream(outfile)
* Since fputs does its own buffering, flush doesn't need to.
* Flush does nothing if the create flag is not set.
*/
static void
flush()
{
if (create)
}
/*
* Getfile does nothing if the create flag is not set. If the create
* flag is set, getfile positions the file pointer(fptr) at the end of
* the file name prefix on the first call(fptr=0). The file counter is
* stored in the file name and incremented. If the subsequent fopen
* fails, the file name is copied to tfile for the error message, the
* previous file name is restored for cleanup, and fatal is called. If
* the fopen succeeds, the stream(opfil) is returned.
*/
FILE *
getfile()
{
static char *fptr;
static int ctr;
char *delim;
char savedelim;
if (create) {
if (fptr == 0)
continue;
/* check for suffix length overflow */
fatal("Suffix longer than %ld chars; increase -n\n",
(char *)fiwidth);
}
/* check for filename length overflow */
}
} else {
/* truncate file at pathname delim to do pathconf */
*delim = '\0';
/*
* file: pppppppp\0fffff\0
* ..... ^ file
* ............. ^ delim
*/
}
}
}
return (opfil);
}
return (NULL);
}
/*
* Getline gets a line via fgets from the input stream "infile".
* The line is put into linbuf and may not be larger than LINSIZ.
* If getaline is called with a non-zero value, the current line
* is bumped, otherwise it is not(for R.E. searching).
*/
static char *
{
char *ret;
if (bumpcur)
curline++;
return (ret);
}
/*
* Line_arg handles line number arguments.
* line_arg takes as its argument a pointer to a character string
* (assumed to be a line number). If that character string can be
* converted to a number(long long), to_line is called with that number,
* otherwise error.
*/
static void
{
long long to;
}
/*
* Num_arg handles repeat arguments.
* Num_arg copies the numeric argument to "rep" (error if number is
* larger than 20 characters or } is left off). Num_arg then converts
* the number and checks for validity. Next num_arg checks the mode
* of the previous argument, and applys the argument the correct number
* of times. If the mode is not set properly its an error.
*/
static void
{
char *ptr;
int len;
len = 1;
}
}
else
}
/*
* Re_arg handles regular expression arguments.
* Re_arg takes a csplit regular expression argument. It checks for
* delimiter balance, computes any offset, and compiles the regular
* expression. Findline is called with the compiled expression and
* offset, and returns the corresponding line number, which is used
* as input to the to_line function.
*/
static void
{
char *ptr;
char ch;
int len;
ptr++;
if (*ptr == '\\')
++ptr;
len = 1;
}
/*
* The line below was added because compile no longer supports
* the fourth argument being passed. The fourth argument used
* to be '/' or '%'.
*/
/*
* The line below was added because INIT which did this for us
* was removed from compile in regexp.h
*/
string++;
if (regerrno)
}
/*
* Sig handles breaks. When a break occurs the signal is reset,
* and fatal is called to clean up and print the argument which
* was being processed at the time the interrupt occured.
*/
/* ARGSUSED */
static void
sig(int s)
{
}
/*
* To_line creates split files.
* To_line gets as its argument the line which the current argument
* referenced. To_line calls getfile for a new output stream, which
* does nothing if create is False. If to_line's argument is not LAST
* it checks that the current line is not greater than its argument.
* While the current line is less than the desired line to_line gets
* lines and flushes(error if EOF is reached).
* If to_line's argument is LAST, it checks for more lines, and gets
* and flushes lines till the end of file.
* Finally, to_line calls closefile to close the output stream.
*/
static void
{
flush();
}
} else /* last file */
flush();
for (;;) {
break;
flush();
}
} else
closefile();
}
static void
usage()
{
"usage: csplit [-ks] [-f prefix] [-n number] "
"file arg1 ...argn\n"));
exit(1);
}