[PseudoTerminal][NFC] Use llvm errno helpers
authorDavid Bolvansky <david.bolvansky@gmail.com>
Mon, 3 Sep 2018 14:59:57 +0000 (14:59 +0000)
committerDavid Bolvansky <david.bolvansky@gmail.com>
Mon, 3 Sep 2018 14:59:57 +0000 (14:59 +0000)
Summary:
LLVM provide (str)errno helpers, so convert code to use it.

Also fixes warning:
/home/xbolva00/LLVM/llvm/tools/lldb/source/Host/common/PseudoTerminal.cpp:248:25: warning: ignoring return value of ‘char* strerror_r(int, char*, size_t)’, declared with attribute warn_unused_result [-Wunused-result]
             ::strerror_r(errno, error_str, error_len);

Reviewers: JDevlieghere

Reviewed By: JDevlieghere

Subscribers: abidh, lldb-commits

Differential Revision: https://reviews.llvm.org/D51591

llvm-svn: 341320

lldb/source/Host/common/PseudoTerminal.cpp

index c9b2900..08d4fa2 100644 (file)
@@ -10,7 +10,8 @@
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Config.h"
 
-#include <errno.h>
+#include "llvm/Support/Errno.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -27,6 +28,14 @@ int posix_openpt(int flags);
 using namespace lldb_private;
 
 //----------------------------------------------------------------------
+// Write string describing error number
+//----------------------------------------------------------------------
+static void ErrnoToStr(char *error_str, size_t error_len) {
+  std::string strerror = llvm::sys::StrError();
+  ::snprintf(error_str, error_len, "%s", strerror.c_str());
+}
+
+//----------------------------------------------------------------------
 // PseudoTerminal constructor
 //----------------------------------------------------------------------
 PseudoTerminal::PseudoTerminal()
@@ -88,14 +97,14 @@ bool PseudoTerminal::OpenFirstAvailableMaster(int oflag, char *error_str,
   m_master_fd = ::posix_openpt(oflag);
   if (m_master_fd < 0) {
     if (error_str)
-      ::strerror_r(errno, error_str, error_len);
+      ErrnoToStr(error_str, error_len);
     return false;
   }
 
   // Grant access to the slave pseudo terminal
   if (::grantpt(m_master_fd) < 0) {
     if (error_str)
-      ::strerror_r(errno, error_str, error_len);
+      ErrnoToStr(error_str, error_len);
     CloseMasterFileDescriptor();
     return false;
   }
@@ -103,7 +112,7 @@ bool PseudoTerminal::OpenFirstAvailableMaster(int oflag, char *error_str,
   // Clear the lock flag on the slave pseudo terminal
   if (::unlockpt(m_master_fd) < 0) {
     if (error_str)
-      ::strerror_r(errno, error_str, error_len);
+      ErrnoToStr(error_str, error_len);
     CloseMasterFileDescriptor();
     return false;
   }
@@ -143,7 +152,7 @@ bool PseudoTerminal::OpenSlave(int oflag, char *error_str, size_t error_len) {
 
   if (m_slave_fd < 0) {
     if (error_str)
-      ::strerror_r(errno, error_str, error_len);
+      ErrnoToStr(error_str, error_len);
     return false;
   }
 
@@ -175,7 +184,7 @@ const char *PseudoTerminal::GetSlaveName(char *error_str,
   const char *slave_name = ::ptsname(m_master_fd);
 
   if (error_str && slave_name == nullptr)
-    ::strerror_r(errno, error_str, error_len);
+    ErrnoToStr(error_str, error_len);
 
   return slave_name;
 }
@@ -213,7 +222,7 @@ lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
     if (pid < 0) {
       // Fork failed
       if (error_str)
-        ::strerror_r(errno, error_str, error_len);
+        ErrnoToStr(error_str, error_len);
     } else if (pid == 0) {
       // Child Process
       ::setsid();
@@ -229,23 +238,23 @@ lldb::pid_t PseudoTerminal::Fork(char *error_str, size_t error_len) {
         // Acquire the controlling terminal
         if (::ioctl(m_slave_fd, TIOCSCTTY, (char *)0) < 0) {
           if (error_str)
-            ::strerror_r(errno, error_str, error_len);
+            ErrnoToStr(error_str, error_len);
         }
 #endif
         // Duplicate all stdio file descriptors to the slave pseudo terminal
         if (::dup2(m_slave_fd, STDIN_FILENO) != STDIN_FILENO) {
           if (error_str && !error_str[0])
-            ::strerror_r(errno, error_str, error_len);
+            ErrnoToStr(error_str, error_len);
         }
 
         if (::dup2(m_slave_fd, STDOUT_FILENO) != STDOUT_FILENO) {
           if (error_str && !error_str[0])
-            ::strerror_r(errno, error_str, error_len);
+            ErrnoToStr(error_str, error_len);
         }
 
         if (::dup2(m_slave_fd, STDERR_FILENO) != STDERR_FILENO) {
           if (error_str && !error_str[0])
-            ::strerror_r(errno, error_str, error_len);
+            ErrnoToStr(error_str, error_len);
         }
       }
     } else {