commit:     8a4231619033fa7e5fb43402e75bf94d77c1e279
Author:     Michał Górny <mgorny <AT> gentoo <DOT> org>
AuthorDate: Fri Dec 19 19:16:04 2025 +0000
Commit:     Michał Górny <mgorny <AT> gentoo <DOT> org>
CommitDate: Fri Dec 19 19:16:04 2025 +0000
URL:        https://gitweb.gentoo.org/proj/steve.git/commit/?id=8a423161

Read /proc/*/cmdline into std::string

Signed-off-by: Michał Górny <mgorny <AT> gentoo.org>

 steve.cxx | 33 +++++++++++++++------------------
 1 file changed, 15 insertions(+), 18 deletions(-)

diff --git a/steve.cxx b/steve.cxx
index b7a7b11..46b2fba 100644
--- a/steve.cxx
+++ b/steve.cxx
@@ -20,6 +20,7 @@
 #include <climits>
 #include <cmath>
 #include <csignal>
+#include <algorithm>
 #include <deque>
 #include <functional>
 #include <memory>
@@ -403,25 +404,21 @@ static void steve_open(fuse_req_t req, struct 
fuse_file_info *fi)
        static_assert(sizeof(fi->fh) >= sizeof(context->pid));
        fi->fh = context->pid;
 
-       if (state->verbose) {
-               char cmdline[128] = {};
-
-               std::string path = std::format("/proc/{}/cmdline", fi->fh);
-               FILE *cmdline_file = fopen(path.c_str(), "r");
-               if (cmdline_file) {
-                       size_t rd = fread(cmdline, 1, sizeof(cmdline) - 1, 
cmdline_file);
-                       if (rd > 0) {
-                               /* replace all NULs with spaces, except for the 
final one */
-                               for (size_t i = 0; i < rd - 1; ++i) {
-                                       if (cmdline[i] == 0)
-                                               cmdline[i] = ' ';
-                               }
-                               /* ensure a NUL, in case it was truncated */
-                               cmdline[rd] = 0;
-                       }
-                       fclose(cmdline_file);
-               }
+       std::string cmdline;
+       std::string path = std::format("/proc/{}/cmdline", fi->fh);
+       if (FILE *cmdline_file = fopen(path.c_str(), "r")) {
+               cmdline.resize(128);
+               size_t rd = fread(cmdline.data(), 1, cmdline.size(), 
cmdline_file);
+               /* remove the final null terminator (if any) */
+               if (cmdline[rd] == '\0')
+                       --rd;
+               cmdline.resize(rd > 0 ? rd : 0);
+               /* replace all NULs with spaces */
+               std::replace(cmdline.begin(), cmdline.end(), '\0', ' ');
+               fclose(cmdline_file);
+       }
 
+       if (state->verbose) {
                if (cmdline[0]) {
                        std::print(stderr, "Device open by PID {} ({})\n", 
fi->fh, cmdline);
                } else

Reply via email to