about summary refs log tree commit diff
path: root/rnl.c
blob: 594e5c54b63f66ebedfe8a78d6603de6c9da16ce (plain) (blame)
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
/* rnl - remove final newlines */

#include <sys/stat.h>
#include <sys/types.h>

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, char *argv[])
{
	int c;
	int i;
	int mode = -1;
	char nl = '\n';

	while ((c = getopt(argc, argv, "0aso")) != -1)
		switch (c) {
		case '0': nl = '\0'; break;
		case 'a': mode = 0; break;
		case 's': mode = -1; break;
		case 'o': mode = 1; break;
		default:
			fprintf(stderr,
"Usage: %s [-0aso] < INPUT           writes to stdout\n"
"       %s [-0aso] INPUT...          modifies files in-place!\n"
"  -0  strip NUL bytes instead of newlines\n"
"  -a  strip all newlines at end\n"
"  -s  default: strip all but one newline at end\n"
"  -o  strip one newline at end\n",
			    argv[0], argv[0]);
			exit(1);
		}

	if (optind == argc) {
		/* pipe mode stdin -> stdout */
		off_t n = 0;

		while ((c = getchar()) != EOF) {
			if (c == nl) {
				n++;
				continue;
			}
			for (i = 0; i < n; i++)
				putchar(nl);
			n = 0;
			putchar(c);
		}
		if (mode == 1)
			for (i = 0; i < n - 1; i++)
				putchar(nl);
		else if (mode == -1 && n > 0)
			putchar(nl);
		/* else (mode == 0) ; noop */

		return 0;
	}

	/* in-place mode */
	for (i = optind; i < argc; i++) {
		int fd = open(argv[i], O_RDWR);
		if (fd < 0)
			perror("rnl: open");

		off_t pos = lseek(fd, 0, SEEK_END);

		if (mode == 1) {
			if (pos > 0) {
				char buf;
				if (pread(fd, &buf, 1, pos - 1) != 1)
					perror("rnl: pread");
				if (buf == nl)
					if (ftruncate(fd, pos - 1) < 0)
						perror("rnl: ftruncate");

			}
		} else {
			char buf[1024];
			off_t n = 0;

			off_t endpos = pos;
			ssize_t rd;
			do {
				off_t l = sizeof buf;
				if (pos > l)
					pos -= l;
				else {
					l = pos;
					pos = 0;
				}
				rd = pread(fd, buf, l, pos);
				while (rd > 0 && buf[--rd] == nl)
					n++;
			} while (rd == 0 && n > 0 && pos > 0);

			if (rd < 0)
				perror("rnl: pread");

			if (mode == -1 && n > 0)
				n--;

			if (n > 0)
				if (ftruncate(fd, endpos - n) < 0)
					perror("rnl: ftruncate");
		}

		close(fd);
	}

	return 0;
}