source: freewrt/tools/paxmirabilis/src/tty_subs.c@ a569125

freewrt_1_0 freewrt_2_0
Last change on this file since a569125 was a569125, checked in by Thorsten Glaser <tg@…>, 14 years ago

even FreeWRT 1.0-stable deserves paxmirabilis-20120216 compiled with LTO ☺

git-svn-id: svn://www.freewrt.org/branches/freewrt_1_0@3981 afb5a338-a214-0410-bd46-81f09a774fd1

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/* $OpenBSD: tty_subs.c,v 1.14 2009/10/27 23:59:22 deraadt Exp $ */
2/* $NetBSD: tty_subs.c,v 1.5 1995/03/21 09:07:52 cgd Exp $ */
3
4/*-
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/param.h>
38#include <sys/time.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <errno.h>
43#include <unistd.h>
44#include <stdlib.h>
45#include <string.h>
46#include "pax.h"
47#include "extern.h"
48#include <stdarg.h>
49
50__RCSID("$MirOS: src/bin/pax/tty_subs.c,v 1.4 2012/02/16 16:01:09 tg Exp $");
51
52/*
53 * routines that deal with I/O to and from the user
54 */
55
56#define DEVTTY "/dev/tty" /* device for interactive i/o */
57static FILE *ttyoutf = NULL; /* output pointing at control tty */
58static FILE *ttyinf = NULL; /* input pointing at control tty */
59
60/*
61 * tty_init()
62 * try to open the controlling terminal (if any) for this process. if the
63 * open fails, future ops that require user input will get an EOF
64 */
65
66int
67tty_init(void)
68{
69 int ttyfd;
70
71 if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
72 if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
73 if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
74 return(0);
75 (void)fclose(ttyoutf);
76 }
77 (void)close(ttyfd);
78 }
79
80 if (iflag) {
81 paxwarn(1, "Fatal error, cannot open %s", DEVTTY);
82 return(-1);
83 }
84 return(0);
85}
86
87/*
88 * tty_prnt()
89 * print a message using the specified format to the controlling tty
90 * if there is no controlling terminal, just return.
91 */
92
93void
94tty_prnt(const char *fmt, ...)
95{
96 va_list ap;
97
98 va_start(ap, fmt);
99 if (ttyoutf == NULL) {
100 va_end(ap);
101 return;
102 }
103 (void)vfprintf(ttyoutf, fmt, ap);
104 va_end(ap);
105 (void)fflush(ttyoutf);
106}
107
108/*
109 * tty_read()
110 * read a string from the controlling terminal if it is open into the
111 * supplied buffer
112 * Return:
113 * 0 if data was read, -1 otherwise.
114 */
115
116int
117tty_read(char *str, int len)
118{
119 if (ttyinf == NULL || fgets(str, len, ttyinf) == NULL)
120 return(-1);
121
122 /*
123 * strip off that trailing newline
124 */
125 str[strcspn(str, "\n")] = '\0';
126 return(0);
127}
128
129/*
130 * paxwarn()
131 * write a warning message to stderr. if "set" the exit value of pax
132 * will be non-zero.
133 */
134
135void
136paxwarn(int set, const char *fmt, ...)
137{
138 va_list ap;
139
140 va_start(ap, fmt);
141 if (set)
142 exit_val = 1;
143 /*
144 * when vflag we better ship out an extra \n to get this message on a
145 * line by itself
146 */
147 if (vfpart) {
148 (void)fflush(listf);
149 (void)fputc('\n', stderr);
150 vfpart = 0;
151 }
152 (void)fprintf(stderr, "%s: ", argv0);
153 (void)vfprintf(stderr, fmt, ap);
154 va_end(ap);
155 (void)fputc('\n', stderr);
156}
157
158/*
159 * syswarn()
160 * write a warning message to stderr. if "set" the exit value of pax
161 * will be non-zero.
162 */
163
164void
165syswarn(int set, int errnum, const char *fmt, ...)
166{
167 va_list ap;
168
169 va_start(ap, fmt);
170 if (set)
171 exit_val = 1;
172 /*
173 * when vflag we better ship out an extra \n to get this message on a
174 * line by itself
175 */
176 if (vfpart) {
177 (void)fflush(listf);
178 (void)fputc('\n', stderr);
179 vfpart = 0;
180 }
181 (void)fprintf(stderr, "%s: ", argv0);
182 (void)vfprintf(stderr, fmt, ap);
183 va_end(ap);
184
185 /*
186 * format and print the errno
187 */
188 if (errnum > 0)
189 (void)fprintf(stderr, ": %s", strerror(errnum));
190 (void)fputc('\n', stderr);
191}
Note: See TracBrowser for help on using the repository browser.