[SOLVED] carriage return in cmdline

Samuel Venable

Time Killer
What are the chances of a macOS process having a command line string that started it containing an invisible carriage return character i.e. printf "\r" doesn't print anything to the terminal (not even a new line)? I'm using that as a delimiter character because \0 null terminators can't be used with std::getline()

This delimiter character is used to parse the command line arguments retrieved from a specific process id using the sysctl() BSD kernal api that is accessible on mac. The arguments are not placed in quotes if they contain spaces, and I am iterating through each character to build the cmdline string while replacing the null characters that separate each argument by default with carriage returns.

I guess another approach would be to write my own implementation of getline which supports null characters, but I'm doubtful I'll be able to pull it off if even the C++ standard writers don't support it, even with the knowledge they have.

If a process can have a carriage return in its associated cmdline, that will break my existing code.

I'm happy to provide the code if needed, but it's mostly just something I found on google that was apparently pulled from the source code of an open source command line tool by Apple called "ps".

This is for a GameMaker extension I am revising.
 
Last edited:

Samuel Venable

Time Killer
I figured out a solution. Instead of replacing the null characters with carriage returns, I searched for the null characters one at a time with strchr(), in a while loop, and during each iteration of the loop I checked if the substring contained spaces, and if it did, wrap the substring in quotes, then I replaced the null delimiter one at a time with an empty whitespace. Before I did these things, I added a trailing null character so at the very end of the string there would be two null characters in a row, that allowed me to check for when the loop has reached the end of the string so that it doesn't loop forever. Lastly, I used pop_back() if the result ended in a trailing whitespace, and returned the result string.
 
Top