-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfold.c
181 lines (160 loc) · 4.1 KB
/
unfold.c
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
172
173
174
175
176
177
178
179
180
181
/*
* unfold.c
* Copyright (C) 2017 Alexei Frolov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <fcntl.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define PROGRAM_NAME "unfold"
#define PROGRAM_VERSION "0.2.0"
#define BUFFER_SIZE 4096
static int print_delim = 0;
static int unfolded = 0;
static void unfold(int fd, int delim)
{
char buf[BUFFER_SIZE];
char *s;
ssize_t bytes;
while ((bytes = read(fd, buf, BUFFER_SIZE)) > 0) {
if (print_delim) {
write(STDOUT_FILENO, &delim, 1);
print_delim = 0;
}
/*
* Clear the last newline read in case it is the newline at the
* end of the input stream, which shouldn't be replaced with a
* delimiter.
*/
if (buf[bytes - 1] == '\n') {
--bytes;
print_delim = 1;
}
s = buf;
while ((s = strchr(s, '\n')))
*s = delim;
write(STDOUT_FILENO, buf, bytes);
}
if (bytes == -1)
perror("read");
else
unfolded = 1;
}
static int open_file(const char *path)
{
struct stat sb;
int fd;
if ((fd = open(path, O_RDONLY)) == -1) {
perror(path);
return -1;
}
if (fstat(fd, &sb) != 0) {
perror(path);
return -1;
}
if (!S_ISREG(sb.st_mode)) {
fprintf(stderr, "%s: not a regular file\n", path);
return -1;
}
return fd;
}
static void help(FILE *f, const char *name)
{
fprintf(f, "usage: %s [OPTION]... [FILE]...\n", name);
fprintf(f, "Join input lines and print to standard output\n\n");
fprintf(f, " -d, --delimiter=DELIM\n");
fprintf(f, " use DELIM instead of space to replace newlines\n");
fprintf(f, " -h, --help\n");
fprintf(f, " display this help text and exit\n");
fprintf(f, " -n, --no-newline\n");
fprintf(f, " do not output a trailing newline\n");
fprintf(f, " -S, --separate-files\n");
fprintf(f, " don't join lines from different files\n");
fprintf(f, " -v, --version\n");
fprintf(f, " display version information and exit\n");
}
static void version(FILE *f, const char *name)
{
fprintf(f, "%s %s\n", name, PROGRAM_VERSION);
fprintf(f, "Copyright (C) 2017 Alexei Frolov\n");
fprintf(f, "This program is distributed as free software under the\n");
fprintf(f, "terms of the GNU General Public License, version 3.\n");
}
int main(int argc, char **argv)
{
int c, delim, filebreaks, newline, status, fd;
static struct option long_opts[] = {
{ "delimiter", required_argument, 0, 'd' },
{ "help", no_argument, 0, 'h' },
{ "no-newline", no_argument, 0, 'n' },
{ "separate-files", no_argument, 0, 'S' },
{ "version", no_argument, 0, 'v' },
{ 0, 0, 0, 0 }
};
delim = ' ';
filebreaks = 0;
newline = 1;
status = EXIT_SUCCESS;
while ((c = getopt_long(argc, argv, "d:hnSv", long_opts, NULL))
!= EOF) {
switch (c) {
case 'd':
delim = optarg[0];
break;
case 'h':
help(stdout, PROGRAM_NAME);
exit(0);
case 'n':
newline = 0;
break;
case 'S':
filebreaks = 1;
break;
case 'v':
version(stdout, PROGRAM_NAME);
exit(0);
case '?':
default:
help(stderr, argv[0]);
exit(1);
}
}
if (optind == argc) {
unfold(STDIN_FILENO, delim);
} else {
for (; optind < argc; ++optind) {
if (strcmp(argv[optind], "-") == 0) {
fd = STDIN_FILENO;
} else if ((fd = open_file(argv[optind])) == -1) {
status = EXIT_FAILURE;
continue;
}
unfold(fd, delim);
if (filebreaks && optind < argc - 1) {
putchar('\n');
print_delim = 0;
}
if (fd != STDIN_FILENO)
close(fd);
}
}
if (unfolded && newline)
putchar('\n');
return status;
}