preserve errno across free
[musl.git] / src / stdio / perror.c
blobd0943f26a938bd9dad153c1d64dd88f643c31097
1 #include <stdio.h>
2 #include <string.h>
3 #include <errno.h>
4 #include "stdio_impl.h"
6 void perror(const char *msg)
8 FILE *f = stderr;
9 char *errstr = strerror(errno);
11 FLOCK(f);
13 /* Save stderr's orientation and encoding rule, since perror is not
14 * permitted to change them. */
15 void *old_locale = f->locale;
16 int old_mode = f->mode;
18 if (msg && *msg) {
19 fwrite(msg, strlen(msg), 1, f);
20 fputc(':', f);
21 fputc(' ', f);
23 fwrite(errstr, strlen(errstr), 1, f);
24 fputc('\n', f);
26 f->mode = old_mode;
27 f->locale = old_locale;
29 FUNLOCK(f);