util: Cleanup usage of string types

Change-Id: I8fd459dd9cf42974b62176384b25cf57c48fba79
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 1c77f8d..87c660d 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -25,6 +25,7 @@
 #include <sstream>
 #include <stdplus/fd/create.hpp>
 #include <stdplus/raw.hpp>
+#include <stdplus/zstring.hpp>
 #include <string>
 #include <string_view>
 #include <unordered_map>
@@ -736,10 +737,10 @@
     return *ret;
 }
 
-static void setNICAdminState(const char* intf, bool up)
+static void setNICAdminState(stdplus::const_zstring intf, bool up)
 {
     ifreq ifr = {};
-    std::strncpy(ifr.ifr_name, intf, IF_NAMESIZE - 1);
+    std::strncpy(ifr.ifr_name, intf.data(), IF_NAMESIZE - 1);
     getIFSock().ioctl(SIOCGIFFLAGS, &ifr);
 
     ifr.ifr_flags &= ~IFF_UP;
@@ -760,9 +761,8 @@
     {
         // We only need to bring down the interface, networkd will always bring
         // up managed interfaces
-        manager.addReloadPreHook([ifname = interfaceName()]() {
-            setNICAdminState(ifname.c_str(), false);
-        });
+        manager.addReloadPreHook(
+            [ifname = interfaceName()]() { setNICAdminState(ifname, false); });
     }
     manager.reloadConfigs();
 
@@ -1123,14 +1123,14 @@
         writeConfigurationFile();
         manager.addReloadPreHook([interface]() {
             // The MAC and LLADDRs will only update if the NIC is already down
-            setNICAdminState(interface.c_str(), false);
+            setNICAdminState(interface, false);
         });
         manager.reloadConfigs();
     }
 
 #ifdef HAVE_UBOOT_ENV
     // Ensure that the valid address is stored in the u-boot-env
-    auto envVar = interfaceToUbootEthAddr(interface.c_str());
+    auto envVar = interfaceToUbootEthAddr(interface);
     if (envVar)
     {
         // Trimming MAC addresses that are out of range. eg: AA:FF:FF:FF:FF:100;
diff --git a/src/util.cpp b/src/util.cpp
index fb59348..59e83bf 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -14,6 +14,7 @@
 
 #include <algorithm>
 #include <cctype>
+#include <charconv>
 #include <cstdlib>
 #include <cstring>
 #include <fstream>
@@ -34,34 +35,22 @@
 namespace network
 {
 
+using std::literals::string_view_literals::operator""sv;
 using namespace phosphor::logging;
 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
 
 namespace internal
 {
 
-void executeCommandinChildProcess(const char* path, char** args)
+void executeCommandinChildProcess(stdplus::const_zstring path, char** args)
 {
     using namespace std::string_literals;
     pid_t pid = fork();
-    int status{};
 
     if (pid == 0)
     {
-        execv(path, args);
-        auto error = errno;
-        // create the command from var args.
-        std::string command = path + " "s;
-
-        for (int i = 0; args[i]; i++)
-        {
-            command += args[i] + " "s;
-        }
-
-        log<level::ERR>("Couldn't exceute the command",
-                        entry("ERRNO=%d", error),
-                        entry("CMD=%s", command.c_str()));
-        elog<InternalFailure>();
+        execv(path.c_str(), args);
+        exit(255);
     }
     else if (pid < 0)
     {
@@ -71,10 +60,11 @@
     }
     else if (pid > 0)
     {
+        int status;
         while (waitpid(pid, &status, 0) == -1)
         {
             if (errno != EINTR)
-            { // Error other than EINTR
+            {
                 status = -1;
                 break;
             }
@@ -82,14 +72,15 @@
 
         if (status < 0)
         {
-            std::string command = path + " "s;
-            for (int i = 0; args[i]; i++)
+            fmt::memory_buffer buf;
+            fmt::format_to(fmt::appender(buf), "`{}`", path);
+            for (size_t i = 0; args[i] != nullptr; ++i)
             {
-                command += args[i] + " "s;
+                fmt::format_to(fmt::appender(buf), " `{}`", args[i]);
             }
-
+            buf.push_back('\0');
             log<level::ERR>("Unable to execute the command",
-                            entry("CMD=%s", command.c_str()),
+                            entry("CMD=%s", buf.data()),
                             entry("STATUS=%d", status));
             elog<InternalFailure>();
         }
@@ -236,10 +227,9 @@
     throw std::runtime_error("Invalid addr type");
 }
 
-bool isValidIP(int addressFamily, const std::string& address)
+bool isValidIP(int addressFamily, stdplus::const_zstring address)
 {
     unsigned char buf[sizeof(struct in6_addr)];
-
     return inet_pton(addressFamily, address.c_str(), buf) > 0;
 }
 
@@ -298,7 +288,7 @@
     return interfaces;
 }
 
-void deleteInterface(const std::string& intf)
+void deleteInterface(stdplus::const_zstring intf)
 {
     pid_t pid = fork();
     int status{};
@@ -339,22 +329,18 @@
     }
 }
 
-std::optional<std::string> interfaceToUbootEthAddr(const char* intf)
+std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf)
 {
-    constexpr char ethPrefix[] = "eth";
-    constexpr size_t ethPrefixLen = sizeof(ethPrefix) - 1;
-    if (strncmp(ethPrefix, intf, ethPrefixLen) != 0)
+    constexpr auto pfx = "eth"sv;
+    if (!intf.starts_with(pfx))
     {
         return std::nullopt;
     }
-    const auto intfSuffix = intf + ethPrefixLen;
-    if (intfSuffix[0] == '\0')
-    {
-        return std::nullopt;
-    }
-    char* end;
-    unsigned long idx = strtoul(intfSuffix, &end, 10);
-    if (end[0] != '\0')
+    intf.remove_prefix(pfx.size());
+    auto last = intf.data() + intf.size();
+    unsigned long idx;
+    auto res = std::from_chars(intf.data(), last, idx);
+    if (res.ec != std::errc() || res.ptr != last)
     {
         return std::nullopt;
     }
@@ -362,7 +348,7 @@
     {
         return "ethaddr";
     }
-    return "eth" + std::to_string(idx) + "addr";
+    return fmt::format(FMT_COMPILE("eth{}addr"), idx);
 }
 
 static std::optional<DHCPVal> systemdParseDHCP(std::string_view str)
@@ -545,23 +531,22 @@
     return fromString(std::get<std::string>(value));
 }
 
-ether_addr fromString(const char* str)
+ether_addr fromString(stdplus::zstring_view str)
 {
     std::string genstr;
 
     // MAC address without colons
-    std::string_view strv = str;
-    if (strv.size() == 12 && strv.find(":") == strv.npos)
+    if (str.size() == 12 && str.find(":") == str.npos)
     {
         genstr =
-            fmt::format(FMT_COMPILE("{}:{}:{}:{}:{}:{}"), strv.substr(0, 2),
-                        strv.substr(2, 2), strv.substr(4, 2), strv.substr(6, 2),
-                        strv.substr(8, 2), strv.substr(10, 2));
-        str = genstr.c_str();
+            fmt::format(FMT_COMPILE("{}:{}:{}:{}:{}:{}"), str.substr(0, 2),
+                        str.substr(2, 2), str.substr(4, 2), str.substr(6, 2),
+                        str.substr(8, 2), str.substr(10, 2));
+        str = genstr;
     }
 
     ether_addr addr;
-    if (ether_aton_r(str, &addr) == nullptr)
+    if (ether_aton_r(str.c_str(), &addr) == nullptr)
     {
         throw std::invalid_argument("Invalid MAC Address");
     }
@@ -570,12 +555,8 @@
 
 std::string toString(const ether_addr& mac)
 {
-    char buf[18] = {0};
-    snprintf(buf, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac.ether_addr_octet[0],
-             mac.ether_addr_octet[1], mac.ether_addr_octet[2],
-             mac.ether_addr_octet[3], mac.ether_addr_octet[4],
-             mac.ether_addr_octet[5]);
-    return buf;
+    return fmt::format(FMT_COMPILE("{:02x}"),
+                       fmt::join(mac.ether_addr_octet, ":"));
 }
 
 bool isEmpty(const ether_addr& mac)
diff --git a/src/util.hpp b/src/util.hpp
index 5705eda..ded3d27 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -9,6 +9,8 @@
 #include <filesystem>
 #include <optional>
 #include <sdbusplus/bus.hpp>
+#include <stdplus/zstring.hpp>
+#include <stdplus/zstring_view.hpp>
 #include <string>
 #include <string_view>
 #include <unordered_set>
@@ -40,11 +42,7 @@
  *  @returns A mac address in network byte order
  *  @throws std::runtime_error for bad mac
  */
-ether_addr fromString(const char* str);
-inline ether_addr fromString(const std::string& str)
-{
-    return fromString(str.c_str());
-}
+ether_addr fromString(stdplus::zstring_view str);
 
 /** @brief Converts the given mac address bytes into a string
  *  @param[in] mac - The mac address
@@ -95,7 +93,7 @@
  * @param[in] address - IP address.
  * @returns true if it is valid otherwise false.
  */
-bool isValidIP(int addressFamily, const std::string& address);
+bool isValidIP(int addressFamily, stdplus::const_zstring address);
 
 /* @brief checks that the given prefix is valid or not.
  * @param[in] addressFamily - IP address family(AF_INET/AF_INET6).
@@ -112,7 +110,7 @@
 /** @brief Delete the given interface.
  *  @param[in] intf - interface name.
  */
-void deleteInterface(const std::string& intf);
+void deleteInterface(stdplus::const_zstring intf);
 
 /** @brief Converts the interface name into a u-boot environment
  *         variable that would hold its ethernet address.
@@ -120,7 +118,7 @@
  *  @param[in] intf - interface name
  *  @return The name of th environment key
  */
-std::optional<std::string> interfaceToUbootEthAddr(const char* intf);
+std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf);
 
 /** @brief read the IPv6AcceptRA value from the configuration file
  *  @param[in] config - The parsed configuration.
@@ -149,7 +147,7 @@
  * @param[in] path - path of the binary file which needs to be execeuted.
  * @param[in] args - arguments of the command.
  */
-void executeCommandinChildProcess(const char* path, char** args);
+void executeCommandinChildProcess(stdplus::const_zstring path, char** args);
 
 /** @brief Get ignored interfaces from environment */
 std::string_view getIgnoredInterfacesEnv();
@@ -168,7 +166,7 @@
  * @param[in] tArgs - arguments of the command.
  */
 template <typename... ArgTypes>
-void execute(const char* path, ArgTypes&&... tArgs)
+void execute(stdplus::const_zstring path, ArgTypes&&... tArgs)
 {
     using expandType = char*[];