Debian Codegraph @

lib/misc.c

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 2004, 2008 Antti-Juhani Kaijanaho
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <errno.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include "misc.h"
23 #include "msg.h"
24
25 int to_stdout (const char * fname)
26 {
27 FILE * f;
28 FILE * t = stdout;
29 bool pipe = false;
30 int c;
31 int rv = 1;
32
33 if (isatty(STDOUT_FILENO)) {
34 char * cmd = getenv("PAGER");
35 if (cmd == 0) cmd = "/usr/bin/pager";
36 if (do_msg(L_INFORMATIONAL)) {
37 fprintf(stderr, _("%s: using `%s' as pager\n"),
38 get_progname(), cmd);
39 }
40 // popen does not set errno if memory allocation fails
41 errno = ENOMEM;
42 FILE * p = popen(cmd, "w");
43 if (p != 0) {
44 t = p;
45 pipe = true;
46 } else if (do_msg(L_IMPORTANT)) {
47 fprintf(stderr, _("%s: popen failed for %s: %s\n"),
48 get_progname(), cmd, strerror(errno));
49 }
50 }
51
52 f = fopen (fname, "r");
53 if (f == 0) {
54 message(L_FATAL, COPYING, "%s", strerror (errno));
55 return 0;
56 }
57
58 while ( ( c = getc (f)) != EOF) putc(c, t);
59
60 if (ferror (f)) {
61 message(L_FATAL, COPYING, "%s", strerror(errno));
62 rv = 0;
63 }
64
65 fclose (f);
66
67 if (pipe) pclose(t);
68
69 return rv;
70 }
71