split.c revision 4fce32e15d9c284a06aea2e40852f6674f03b63e
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 1999 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <locale.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include <widec.h>
#include <ctype.h>
#include <errno.h>
#define DEFAULT_LINES 1000
#define ONE_K 1024
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
static void Usage();
static void next_file_name();
static char *progname;
static int suffix_length = 2;
int
{
long long line_count = 0;
long long byte_count = 0;
long long out;
char head[MAXPATHLEN];
char *output_file_name;
char *tail;
char *last;
int i;
int c;
int output_file_open;
int opt;
int non_standard_line_count = FALSE;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
/* check for explicit stdin "-" option */
for (i = 1; i < argc; i++) {
while (i < argc) {
/* a "-" before "--" is an error */
Usage();
}
i++;
}
argc--;
}
}
/* check for non-standard "-line-count" option */
for (i = 1; i < argc; i++) {
break;
"%s: Badly formed number\n"), progname);
Usage();
}
(char **)NULL, 10);
while (i < argc) {
i++;
}
argc--;
}
}
/* get options */
switch (opt) {
case 'a':
Usage();
}
(char **)NULL, 10);
if (suffix_length <= 0) {
"%s: Invalid \"-a %s\" option\n"),
Usage();
}
break;
case 'b':
Usage();
}
(char **)NULL, 10);
byte_count *= ONE_K;
byte_count *= ONE_M;
break;
case 'l':
Usage();
}
if (non_standard_line_count == TRUE) {
Usage();
}
(char **)NULL, 10);
break;
default:
Usage();
}
}
/* get input file */
(void) perror("split");
return (1);
}
}
}
/* get output file name */
} else {
tail++;
*++last = '\0';
}
return (1);
}
"%s: More than %d characters in file name\n"),
Usage();
}
} else
output_file_name = "x";
/* check options */
"%s: Output file name too long\n"), progname);
return (1);
}
if (line_count && byte_count) {
Usage();
}
/* use default line count if none specified */
if (line_count == 0) {
}
/*
* allocate buffer for the filenames we'll construct; it must be
* big enough to hold the name in 'output_file_name' + an n-char
* suffix + NULL terminator
*/
(void) perror("split");
return (1);
}
/* build first output file name */
for (i = 0; output_file_name[i]; i++) {
fname[i] = output_file_name[i];
}
fname[i++] = 'a';
}
if (suffix_length)
fname[i] = '\0';
for (; ; ) {
if (byte_count) {
errno = 0;
if (c == EOF) {
if (errno != 0) {
"%s: Read error at file offset %lld: %s, "
"aborting split\n"),
if (output_file_open == TRUE)
return (1);
}
if (output_file_open == TRUE)
return (0);
}
if (output_file_open == FALSE) {
(void) perror("split");
return (1);
}
}
perror("split");
if (output_file_open == TRUE)
return (1);
}
}
} else {
do {
errno = 0;
if (errno != 0) {
"%s: Invalid multibyte sequence "
"encountered at file offset %lld, "
"aborting split\n"),
} else {
(void) perror("split");
}
if (output_file_open == TRUE)
return (1);
}
if (output_file_open == TRUE)
return (0);
}
if (output_file_open == FALSE) {
(void) perror("split");
return (1);
}
}
(void) perror("split");
if (output_file_open == TRUE)
return (1);
}
} while (wc != '\n');
}
}
if (output_file_open == TRUE)
}
/*NOTREACHED*/
}
static void
next_file_name(char *name)
{
int i;
if (++name[i] <= 'z')
return;
name[i--] = 'a';
}
"%s: Exhausted output file names, aborting split\n"),
progname);
exit(1);
}
static void
Usage()
{
"Usage: %s [-l #] [-a #] [file [name]]\n"
" %s [-b #[k|m]] [-a #] [file [name]]\n"
" %s [-#] [-a #] [file [name]]\n"),
exit(1);
}