C++ Logo

std-proposals

Advanced search

Re: [std-proposals] A draft for a std::arguments proposal

From: Thiago Macieira <thiago_at_[hidden]>
Date: Wed, 16 Oct 2024 08:54:26 -0700
On Monday 7 October 2024 22:04:22 GMT-7 Jeremy Rifkin via Std-Proposals wrote:
> /proc/self/cmdline might indeed be sufficient to solve a lot of problems
> with arguments being edited in main. My main concern would be more niche
> operating systems that might not have a mechanism like this or
> GetCommandLineW or what OSX provides. I can do some more research later.

/proc/self/cmdline only changes if something writes to the strings pointed to
by argv. If you just reorder the pointer array, which is what most of those
tools do, it does not change.

$ cat /proc/self/cmdline -t; echo
cat^@/proc/self/cmdline^_at_-t^@

$ ltrace cat /proc/self/cmdline -t
strrchr("cat", '/') = nil
setlocale(LC_ALL, "") = "C"
bindtextdomain("coreutils", "/usr/share/locale") = "/usr/share/locale"
textdomain("coreutils") = "coreutils"
__cxa_atexit(0x5560f42ad1b0, 0, 0x5560f42b1008, 0) = 0
getopt_long(3, 0x7fffe629d688, "benstuvAET", 0x5560f42b0aa0, nil) = 116
getopt_long(3, 0x7fffe629d688, "benstuvAET", 0x5560f42b0aa0, nil) = -1
fstat(1, 0x7fffe629d320, 0x7ff64ce09e60, 0) = 0
getpagesize() = 4096
open("/proc/self/cmdline", 0, 02) = 3
...

You can see getopt_long() returned 116 ('t') and later the tool did open on
the argv[1], so this shows how getopt_long() reordered the arguments.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel DCAI Platform & System Engineering

Received on 2024-10-16 15:54:29