summaryrefslogtreecommitdiff
path: root/split.c
blob: 81671a78e3f10456495bfa3bdc80dd9e7337c4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * UNG's Not GNU
 * 
 * Copyright (c) 2011, Jakob Kaivo <jakob@kaivo.net>
 * 
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 */

#define _POSIX_C_SOURCE 200809L
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define K_BYTES			1024
#define M_BYTES			1048576

#define DEFAULT_BASE		"x"
#define DEFAULT_SUFFIX_LENGTH	2
#define DEFAULT_LINES		1000

static char *nextsuffix(size_t n, char s[])
{
	const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
	if (s[0] == '\0') {
		memset(s, alphabet[0], n);
		return s;
	}

	for (size_t i = n - 1; ; i--) {
		s[i] = *(strchr(alphabet, s[i]) + 1);
		if (s[i] == '\0') {
			if (i == 0) {
				return NULL;
			}
			s[i] = alphabet[0];
		} else {
			break;
		}
	}
	return s;
}

static int
split(const char *in, const char *base, size_t suffixlen, uintmax_t lines, uintmax_t bytes)
{
	char oname[FILENAME_MAX] = {0};
	char suffix[suffixlen + 2];
	FILE *o = NULL;
	FILE *f = stdin;
	uintmax_t count = 0;

	memset(suffix, '\0', suffixlen + 1);

	if (in != NULL && strcmp("-", in) != 0) {
		f = fopen(in, "r");
	}

	if (f == NULL) {
		perror("fopen");
		return 1;
	}

	if (in != NULL && base == NULL) {
		base = DEFAULT_BASE;
	}

	int c;
	while ((c = fgetc(f)) != EOF) {
		if (o == NULL) {
			char *s = nextsuffix(suffixlen, suffix);
			if (s == NULL) {
				fprintf(stderr, "too many chunks\n");
				return 1;
			}
			snprintf(oname, sizeof(oname), "%s%s", base, s);
			o = fopen(oname, "w");
			if (o == NULL) {
				perror("fopen");
				return 1;
			}
		}

		fputc(c, o);

		if (bytes != 0 || c == '\n') {
			count++;
		}

		if ((bytes != 0 && count == bytes) || count == lines) {
			fclose(o);
			o = NULL;
			count = 0;
		}
	}

	return 0;
}

int main(int argc, char **argv)
{
	uintmax_t lines = 0;
	uintmax_t bytes = 0;
	size_t suffix = DEFAULT_SUFFIX_LENGTH;
	char *end;

	int c;
	while ((c = getopt(argc, argv, "l:a:b:")) != -1) {
		switch (c) {
		case 'l':	/* line_count */
			bytes = 0;
			lines = strtoumax(optarg, &end, 10);
			if (*end != '\0') {
				return 1;
			}
			break;

		case 'a':	/* suffix_length */
			suffix = strtoul(optarg, &end, 10);
			if (end != NULL && strlen(end) > 0) {
				return 1;
			}
			break;

		case 'b':	/* bytes */
			lines = 0;
			bytes = strtoumax(optarg, &end, 10);
			if (*end != '\0') {
				if (!strcmp("k", end)) {
					bytes *= K_BYTES;
				} else if (!strcmp("m", end)) {
					bytes *= M_BYTES;
				} else {
					return 1;
				}
			}
			break;

		default:
			return 1;
		}
	}

	if (lines == 0 && bytes == 0) {
		lines = DEFAULT_LINES;
	}

	if (optind >= argc) {
		split(NULL, NULL, suffix, lines, bytes);
	} else if (optind == argc - 1) {
		split(argv[optind], NULL, suffix, lines, bytes);
	} else if (optind == argc - 2) {
		split(argv[optind], argv[optind + 1], suffix, lines, bytes);
	} else {
		return 1;
	}

	return 0;
}