I might take a rep hit for this, but what the heck. The worst thing that can happen is I'll learn something.
I don't really use C these days, and I don't typically use C-style strings in C++. But one idea I have is to write a modified strcpy() that returns the end of the string:
char* my_strcpy(char*dest, const char* src)
{
while ((*dest = *src++))
++dest;
return dest;
}
Now Shlemiel can bring his bucket along with him:
char prefix[100] = "";
char* bucket = my_strcpy(prefix, argv[0]);
bucket = my_strcpy(bucket, ": ");
bucket = my_strcpy(bucket, cmd_argv[0]);
bucket = my_strcpy(bucket, ": ");
bucket = my_strcpy(bucket, cmd_argv[1]);
perror(prefix);
I haven't tested this. Comments?
EDIT: Removed the unnecessary my_strcat() function. Also it turns out to be the same as stpcpy(), which is apparently part of POSIX as of 2008. See http://www.manpagez.com/man/3/stpcpy/.
_snprintfwhich doesn't guarantee null termination of the destination buffer.