blob: 2d857ebb06f898a880956dc93ab10dc92fba90a5 [file] [log] [blame]
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001#ifndef _CAREFUULPUTC_H
2#define _CAREFUULPUTC_H
3
4/* putc() for use in write and wall (that sometimes are sgid tty) */
5/* Avoid control characters in our locale, and also ASCII control characters.
6 Note that the locale of the recipient is unknown. */
7#include <stdio.h>
8#include <ctype.h>
9
10#define iso8859x_iscntrl(c) \
11 (((c) & 0x7f) < 0x20 || (c) == 0x7f)
12
13static inline int carefulputc(int c, FILE *fp) {
14 int ret;
15
16 if (c == '\007' || c == '\t' || c == '\r' || c == '\n' ||
17 (!iso8859x_iscntrl(c) && (isprint(c) || isspace(c))))
18 ret = putc(c, fp);
19 else if ((c & 0x80) || !isprint(c^0x40))
20 ret = fprintf(fp, "\\%3o", (unsigned char) c);
21 else {
22 ret = putc('^', fp);
23 if (ret != EOF)
24 ret = putc(c^0x40, fp);
25 }
26 return (ret < 0) ? EOF : 0;
27}
28
29#endif /* _CAREFUULPUTC_H */