I’m implementing the perror() equivalent to an API that I’m using.
The perror() ISO C std doc says:
The perror() function shall not change the orientation of the standard
error stream.
but programmatically, what it means?
I’m using fprintf(stderr, .. ) currently. Is a mistake use it? if true, why? if is there some error in my implementation(see below), points for me please.
Check out my C code based on my interpretation:
void
fooapi_perror(const char *s)
{
char *emsg;
if(s != NULL && *s != '\0')
fprintf(stderr, "%s: ", s);
emsg = fooapi_strerror(GetLastErrorCode());
fprintf(stderr, "%s\n", emsg);
free(emsg);
}
Each C stream has a property – “orientation” either “wide-oriented” or “byte-oriented” which is determined by the first operation on this steam. You are allowed to alter the orientation of a steam when the stream doesn’t have an “orientation”.Calling any function whose orientation conflicts with the orientation of the stream results in undefined behavior.
For example, printf would have the steam becoming a byte-oriented while the wprintf cause the steam being a wide-oriented.
As far as your question is concerned, perror should not change the orientation of its stream.
So in your code, if the stream the perror using have already had an orientation, you should make sure that you are not calling a function whose orientation conflicts with the current orientation of the stream.