int reversed, struct termios *mode);
static speed_t string_to_baud(const char *arg);
static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode);
-static void display_all(struct termios *mode, int fd,
- const char *device_name);
-static void display_changed(struct termios *mode, int fd,
- const char *device_name);
-static void display_recoverable(struct termios *mode, int fd,
- const char *device_name);
+static void display_all(struct termios *mode, int fd);
+static void display_changed(struct termios *mode, int fd);
+static void display_recoverable(struct termios *mode, int fd);
static void display_speed(struct termios *mode, int fancy);
-static void display_window_size(int fancy, int fd,
- const char *device_name);
+static void display_window_size(int fancy, int fd);
static void sane_mode(struct termios *mode);
static void set_control_char(const struct control_info *info,
const char *arg, struct termios *mode);
static void set_speed(enum speed_setting type,
const char *arg, struct termios *mode);
-static void set_window_size(int rows, int cols, int fd,
- const char *device_name);
+static void set_window_size(int rows, int cols, int fd);
+
+static const char *device_name;
+
+static __attribute__ ((noreturn)) void perror_on_device(const char *fmt)
+{
+ bb_perror_msg_and_die(fmt, device_name);
+}
+
/* The width of the screen, for output wrapping. */
static int max_col;
#endif
{
struct termios mode;
- void (*output_func)(struct termios *, int, const char *);
+ void (*output_func)(struct termios *, int);
int optc;
int require_set_attr;
int speed_was_set;
int noargs = 1;
char * file_name = NULL;
int fd;
- const char *device_name;
+
output_func = display_changed;
verbose_output = 0;
int fdflags;
device_name = file_name;
- fd = open(device_name, O_RDONLY | O_NONBLOCK);
- if (fd < 0)
- bb_perror_msg_and_die("%s", device_name);
+ fd = bb_xopen(device_name, O_RDONLY | O_NONBLOCK);
if ((fdflags = fcntl(fd, F_GETFL)) == -1
|| fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
- bb_perror_msg_and_die("%s: couldn't reset non-blocking mode",
- device_name);
+ perror_on_device("%s: couldn't reset non-blocking mode");
} else {
fd = 0;
device_name = bb_msg_standard_input;
spurious difference in an uninitialized portion of the structure. */
memset(&mode, 0, sizeof(mode));
if (tcgetattr(fd, &mode))
- bb_perror_msg_and_die("%s", device_name);
+ perror_on_device("%s");
if (verbose_output | recoverable_output | noargs) {
max_col = screen_columns();
current_col = 0;
- output_func(&mode, fd, device_name);
+ output_func(&mode, fd);
return EXIT_SUCCESS;
}
bb_error_msg_and_die("missing argument to `%s'", argv[k]);
++k;
set_window_size((int) bb_xparse_number(argv[k], stty_suffixes),
- -1, fd, device_name);
+ -1, fd);
} else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {
if (k == argc - 1)
bb_error_msg_and_die("missing argument to `%s'", argv[k]);
++k;
set_window_size(-1,
(int) bb_xparse_number(argv[k], stty_suffixes),
- fd, device_name);
+ fd);
} else if (STREQ(argv[k], "size")) {
max_col = screen_columns();
current_col = 0;
- display_window_size(0, fd, device_name);
+ display_window_size(0, fd);
}
#endif
#ifdef HAVE_C_LINE
struct termios new_mode;
if (tcsetattr(fd, TCSADRAIN, &mode))
- bb_perror_msg_and_die("%s", device_name);
+ perror_on_device("%s");
/* POSIX (according to Zlotnick's book) tcsetattr returns zero if
it performs *any* of the requested operations. This means it
spurious difference in an uninitialized portion of the structure. */
memset(&new_mode, 0, sizeof(new_mode));
if (tcgetattr(fd, &new_mode))
- bb_perror_msg_and_die("%s", device_name);
+ perror_on_device("%s");
/* Normally, one shouldn't use memcmp to compare structures that
may have `holes' containing uninitialized data, but we have been
new_mode.c_cflag &= (~CIBAUD);
if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
#endif
- bb_error_msg_and_die ("%s: unable to perform all requested operations",
- device_name);
+ perror_on_device ("%s: unable to perform all requested operations");
}
}
}
static void
-set_window_size(int rows, int cols, int fd, const char *device_name)
+set_window_size(int rows, int cols, int fd)
{
struct winsize win;
if (get_win_size(fd, &win)) {
if (errno != EINVAL)
- bb_perror_msg_and_die("%s", device_name);
+ perror_on_device("%s");
memset(&win, 0, sizeof(win));
}
if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0)
|| (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) {
- bb_perror_msg_and_die("%s", device_name);
+ perror_on_device("%s");
return;
}
# endif
if (ioctl(fd, TIOCSWINSZ, (char *) &win))
- bb_perror_msg_and_die("%s", device_name);
+ perror_on_device("%s");
}
-static void display_window_size(int fancy, int fd, const char *device_name)
+static void display_window_size(int fancy, int fd)
{
const char *fmt_str = "%s" "\0" "%s: no size information for this device";
struct winsize win;
if (get_win_size(fd, &win)) {
if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
- bb_perror_msg_and_die(fmt_str, device_name);
+ perror_on_device(fmt_str);
}
} else {
wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
return NULL;
}
-static void display_changed(struct termios *mode, int fd, const char *device_name)
+static void display_changed(struct termios *mode, int fd)
{
int i;
int empty_line;
}
static void
-display_all(struct termios *mode, int fd, const char *device_name)
+display_all(struct termios *mode, int fd)
{
int i;
tcflag_t *bitsp;
display_speed(mode, 1);
#ifdef TIOCGWINSZ
- display_window_size(1, fd, device_name);
+ display_window_size(1, fd);
#endif
#ifdef HAVE_C_LINE
wrapf("line = %d;", mode->c_line);
current_col = 0;
}
-static void display_recoverable(struct termios *mode, int fd, const char *device_name)
+static void display_recoverable(struct termios *mode, int fd)
{
int i;