views:

250

answers:

2

I would still like to know how to pass in a null character as a command line argument, maybe so that a single string can be passed in as an argument in the form:

"to\0be\0or\0not\0to\0be\0"

And then parse it. However the program would treat this string as:

"to\\0be\\0or\\0not\\0to\\0be\\0"

How can I work around this? Is there any way?

+5  A: 

C strings are null-terminated, so passing strings containing NUL characters is not possible in C. :-P

Now, if you just wanted a way to convert \0 (in the user input, i.e., "\\0" as a C string) into actual NUL characters, that's another matter. In that case, your program just needs a parser to treat \0 as separators.

Chris Jester-Young
Well, it is, but you can't use any of the standard library functions...
Chinmay Kanchi
@Chinmay: The usual program execution system calls, at least in Unix (`execve` and its siblings) and Windows (`CreateProcess`), all trim strings off at NUL. So, that's at the OS level, not just library. :-)
Chris Jester-Young
Fair enough :-)
Chinmay Kanchi
+9  A: 

You cannot.

The C program receives arguments as zero-terminated strings. Such a string cannot contain a null character, by definition.

If you want to pass a null character, then you must somewhat encode it with some syntax, and your C program must then decode it by interpreting that syntax.

Thomas Pornin
You could also read it from stdin, like `xargs -0` does.
Nate C-K