From 8c690feda90f5bd20934a48bd346e6d8e54100b4 Mon Sep 17 00:00:00 2001
From: Stephen Hemminger <shemminger@vyatta.com>
Date: Mon, 18 Jun 2012 09:56:31 -0700
Subject: ioctl interface improvements

Do some minor enhancements for usability.
  * export get_terminal_size and get_interface_flags
  * handle case where /dev/tty is inaccessible
  * turn on warnings in this code
---
 lib/Vyatta/ioctl.pm | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

(limited to 'lib')

diff --git a/lib/Vyatta/ioctl.pm b/lib/Vyatta/ioctl.pm
index b237854..8ed8b93 100644
--- a/lib/Vyatta/ioctl.pm
+++ b/lib/Vyatta/ioctl.pm
@@ -20,14 +20,23 @@
 package Vyatta::ioctl;
 
 use strict;
+use warnings;
 use Socket;
 use Socket6;
+
 require 'sys/ioctl.ph';
 
+our @EXPORT = qw(get_terminal_size get_interface_flags);
+use base qw(Exporter);
+
+
 # returns (rows, columns) for terminal size;
 sub get_terminal_size {
+    # undefined if not terminal attached
+    open(my $TTY, '>', '/dev/tty')
+	or return;
+
     my $winsize = '';
-    open(my $TTY, '>', '/dev/tty');
     # undefined if output not going to terminal
     return unless (ioctl($TTY, &TIOCGWINSZ, $winsize));
     close($TTY);
@@ -40,15 +49,11 @@ sub get_terminal_size {
 sub get_interface_flags {
     my $name  = shift;
 
-    my $SIOCGIFFLAGS = &SIOCGIFFLAGS;
-    die "SIOCGIFFLAGS not found"
-	unless defined($SIOCGIFFLAGS);
-
     socket (my $sock, AF_INET, SOCK_DGRAM, 0)
 	or die "open UDP socket failed: $!";
 
     my $ifreq = pack('a16', $name);
-    ioctl($sock, $SIOCGIFFLAGS, $ifreq)
+    ioctl($sock, &SIOCGIFFLAGS, $ifreq)
 	or return; #undef
 
     my (undef, $flags) = unpack('a16s', $ifreq);
-- 
cgit v1.2.3