r/C_Programming • u/neko-box-coder • Mar 04 '24
A small library for running shell commands cross-platform, which can read stdout, run asynchronous, etc...
This is my first time writing a C library as I normally do C++ stuff, if there's anything wrong, please let me know. I call it "System2" (Great name I know) because it does a little bit more than the standard system() function :)
https://github.com/Neko-Box-Coder/System2
The reason for making this library is because I wanted to invoke shell commands in my C++ code and capture the output but I couldn't find a library that suits my needs so I spent 2 weekends to quickly wrote one myself.
So long story short, here are a few notable features for the (small) library:
- Send stdin and get stdout
- Run synchronously and asynchronously
- Works on POSIX and Windows (Not tested on MacOS though, but should work?)
- C99 (Can probably make it C89 with a few tweaks)
...
9
u/EpochVanquisher Mar 04 '24
Unfortunately this does not work for multithreaded programs:
https://github.com/Neko-Box-Coder/System2/blob/92550e9ac40ca60e7d08da156e09f77f1ac1cf82/System2.h#L220
It is unsafe to call
malloc()in the child after fork, in multithreaded programs. If you want to allocate memory, you can do it on the stack, or you can do it in the parent process before callingfork()and let the child inherit the result.You also should not call
exit(). This is why_Exit()exists.See the
fork()documentation:https://man7.org/linux/man-pages/man2/fork.2.html
1
u/inz__ Mar 04 '24
Did you test the POSIX code, the unnecessary argument escaping looks like it would break your example code.
1
u/thelowsunoverthemoon Mar 04 '24
I only took a quick look but I noticed that you use CMD /S /V /C in your Windows commands always. Maybe you could add it in the documentation or add some sort of option in case the user does not want delayed expansion behaviour (obviously they could always just set it themselves in the command). Also is there a reason in your example that you start a new CMD process to ECHO the variable? Why not ECHO is directly?
1
u/skeeto Mar 04 '24
Interesting project. It would be more useful if the library treated the input command string as UTF-8, and on Windows created the process with CreateProcessW instead of the narrow API. This would more closely match the behavior on other platforms. It's simple: Build the command line just as you already do, but MultiByteToWideChar to a UTF-16 string just before creating the process.
You can drop the
SECURITY_ATTRIBUTESstuff by reversing the inherit bits in SetHandleInformation. The official documentation, which is probably what led you to the current solution, is written poorly.Though this criticism kind of undermines your purpose, more generally
system()is nearly always the wrong tool. Unless the command is trivial, the application has to deal with complex quoting rules, which it's almost guaranteed to get wrong. It's even worse in a multi-platform situation where the quoting rules are different. More useful would be to present an exec-like interface which accepts anargvarray. No shell, so no quoting to worry about — or, really, it pushes that work into the library.On unix this would be trivial to implement, even more than the current situation of concatenating a command string, because you'd just pass it along to exec. On Windows you'd need to do more work to convert
argvinto a command string following quoting conventions (also).