This repository was archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacheline.c
103 lines (90 loc) · 2.82 KB
/
cacheline.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
/* cacheline.c - Delays printing of last newline
*
* Copyright (C) 2001-2005 Oskar Liljeblad
*
* 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 2 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 Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <getopt.h> /* Gnulib/GNU Libc/POSIX */
#include <stdbool.h> /* Gnulib/C99/POSIX */
#include <stdlib.h> /* C89 */
#include <stdio.h> /* C89 */
#include "gettext.h" /* Gnulib */
#define _(String) gettext(String)
#include "getline.h" /* Gnulib/GNU Libc */
#include "version-etc.h" /* Gnulib */
enum {
HELP_OPT = 256,
VERSION_OPT
};
static struct option long_opts[] = {
{ "help", no_argument, NULL, HELP_OPT },
{ "version", no_argument, NULL, VERSION_OPT },
{ 0, 0, 0, 0 }
};
const char version_etc_copyright[] = "Copyright (C) 2001-2005 Oskar Liljeblad";
int
main(int argc, char **argv)
{
char *buf = NULL;
size_t buflen = 0;
bool keep = false;
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
while (true) {
int c;
c = getopt_long(argc, argv, "", long_opts, NULL);
if (c == -1)
break;
switch (c) {
case HELP_OPT:
printf(_("Usage: %s [OPTION]...\n"), argv[0]);
puts(_("Read from in and write to standard out, delaying newline until a complete\n"
"line has been read.\n"));
printf(_(" --help display this help and exit\n"));
printf(_(" --version output version information and exit\n"));
printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
exit(EXIT_SUCCESS);
case VERSION_OPT:
version_etc(stdout, "cacheline", PACKAGE, VERSION, "Oskar Liljeblad", NULL);
exit(EXIT_SUCCESS);
}
}
for (;;) {
ssize_t linelen;
linelen = getline(&buf, &buflen, stdin);
if (linelen < 0) {
if (ferror(stdin)) {
perror(argv[0]);
exit(1);
}
break;
}
if (linelen > 0) {
if (keep) {
fputc('\n', stdout);
keep = false;
}
if (buf[linelen-1] == '\n') {
keep = true;
linelen--;
}
fwrite(buf, linelen, 1, stdout);
fflush(stdout);
}
}
exit(0);
}