1*454af1c0Sdsl /* $NetBSD: eprintf.c,v 1.2 2009/03/14 15:36:04 dsl Exp $ */
2b24ea33cSleo
3b24ea33cSleo /*
4b24ea33cSleo * Copyright (c) 1995 Waldi Ravens.
5b24ea33cSleo * All rights reserved.
6b24ea33cSleo *
7b24ea33cSleo * Redistribution and use in source and binary forms, with or without
8b24ea33cSleo * modification, are permitted provided that the following conditions
9b24ea33cSleo * are met:
10b24ea33cSleo * 1. Redistributions of source code must retain the above copyright
11b24ea33cSleo * notice, this list of conditions and the following disclaimer.
12b24ea33cSleo * 2. Redistributions in binary form must reproduce the above copyright
13b24ea33cSleo * notice, this list of conditions and the following disclaimer in the
14b24ea33cSleo * documentation and/or other materials provided with the distribution.
15b24ea33cSleo * 3. All advertising materials mentioning features or use of this software
16b24ea33cSleo * must display the following acknowledgement:
17b24ea33cSleo * This product includes software developed by Waldi Ravens.
18b24ea33cSleo * 4. The name of the author may not be used to endorse or promote products
19b24ea33cSleo * derived from this software without specific prior written permission.
20b24ea33cSleo *
21b24ea33cSleo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22b24ea33cSleo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23b24ea33cSleo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24b24ea33cSleo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25b24ea33cSleo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26b24ea33cSleo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27b24ea33cSleo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28b24ea33cSleo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29b24ea33cSleo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30b24ea33cSleo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31b24ea33cSleo */
32b24ea33cSleo
33b24ea33cSleo #include <fcntl.h>
34b24ea33cSleo #include <errno.h>
35b24ea33cSleo #include <stdio.h>
36b24ea33cSleo #include <stdarg.h>
37b24ea33cSleo #include <unistd.h>
38b24ea33cSleo #include "libtos.h"
39b24ea33cSleo
40b24ea33cSleo static int output_redirected = 0;
41b24ea33cSleo
42b24ea33cSleo void
redirect_output(char * fn)43*454af1c0Sdsl redirect_output(char *fn)
44b24ea33cSleo {
45b24ea33cSleo int fd;
46b24ea33cSleo
47b24ea33cSleo fd = creat(fn, 0666);
48b24ea33cSleo if (fd < 0 || dup2(fd, STDOUT_FILENO) < 0)
49b24ea33cSleo fatal(errno, "%s", fn);
50b24ea33cSleo output_redirected = 1;
51b24ea33cSleo }
52b24ea33cSleo
53b24ea33cSleo /*
54b24ea33cSleo * Print output to stderr. When output is redirected,
55b24ea33cSleo * also write it to stdout.
56b24ea33cSleo */
57b24ea33cSleo int
eprintf(char * frm)58*454af1c0Sdsl eprintf(char *frm)
59b24ea33cSleo {
60b24ea33cSleo va_list args;
61b24ea33cSleo int rv;
62b24ea33cSleo
63b24ea33cSleo va_start(args, frm);
64b24ea33cSleo rv = veprintf(frm, args);
65b24ea33cSleo va_end(args);
66b24ea33cSleo return(rv);
67b24ea33cSleo }
68b24ea33cSleo
69b24ea33cSleo int
veprintf(char * frm,va_list args)70*454af1c0Sdsl veprintf(char *frm, va_list args)
71b24ea33cSleo {
72b24ea33cSleo int rv;
73b24ea33cSleo
74b24ea33cSleo rv = vfprintf(stderr, frm, args);
75b24ea33cSleo if (output_redirected)
76b24ea33cSleo vfprintf(stdout, frm, args);
77b24ea33cSleo return(rv);
78b24ea33cSleo }
79