]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/carefulputc.h
taskset: Accept 0 pid for current process
[thirdparty/util-linux.git] / include / carefulputc.h
1 #ifndef UTIL_LINUX_CAREFULPUTC_H
2 #define UTIL_LINUX_CAREFULPUTC_H
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <ctype.h>
7 #include <stdbool.h>
8
9 #include "widechar.h"
10 #include "cctype.h"
11
12 /*
13 * A puts() for use in write and wall (that sometimes are sgid tty).
14 * It avoids control and invalid characters.
15 * The locale of the recipient is nominally unknown,
16 * but it's a solid bet that it's compatible with the author's.
17 * Use soft_width=0 to disable wrapping.
18 */
19 static inline int fputs_careful(const char * s, FILE *fp, const char ctrl, bool cr_lf, int soft_width)
20 {
21 int ret = 0, col = 0;
22
23 for (size_t slen = strlen(s); *s; ++s, --slen) {
24 if (*s == '\t')
25 col += (7 - (col % 8)) - 1;
26 else if (*s == '\r')
27 col = -1;
28 else if (*s == '\a')
29 --col;
30
31 if ((soft_width && col >= soft_width) || *s == '\n') {
32 if (soft_width) {
33 fprintf(fp, "%*s", soft_width - col, "");
34 col = 0;
35 }
36 ret = fputs(cr_lf ? "\r\n" : "\n", fp);
37 if (*s == '\n' || ret < 0)
38 goto wrote;
39 }
40
41 if (isprint(*s) || *s == '\a' || *s == '\t' || *s == '\r') {
42 ret = putc(*s, fp);
43 ++col;
44 } else if (!c_isascii(*s)) {
45 #ifdef HAVE_WIDECHAR
46 wchar_t w;
47 size_t clen = mbtowc(&w, s, slen);
48 switch(clen) {
49 case (size_t)-2: // incomplete
50 case (size_t)-1: // EILSEQ
51 mbtowc(NULL, NULL, 0);
52 nonprint:
53 col += ret = fprintf(fp, "\\%3hho", *s);
54 break;
55 default:
56 if(!iswprint(w))
57 goto nonprint;
58 ret = fwrite(s, 1, clen, fp);
59 if (soft_width)
60 col += wcwidth(w);
61 s += clen - 1;
62 slen -= clen - 1;
63 break;
64 }
65 #else
66 col += ret = fprintf(fp, "\\%3hho", *s);
67 #endif
68 } else {
69 ret = fputs((char[]){ ctrl, *s ^ 0x40, '\0' }, fp);
70 col += 2;
71 }
72
73 wrote:
74 if (ret < 0)
75 return EOF;
76 }
77 return 0;
78 }
79
80 static inline void fputs_quoted_case(const char *data, FILE *out, int dir)
81 {
82 const char *p;
83
84 fputc('"', out);
85 for (p = data; p && *p; p++) {
86 if ((unsigned char) *p == 0x22 || /* " */
87 (unsigned char) *p == 0x5c || /* \ */
88 (unsigned char) *p == 0x60 || /* ` */
89 (unsigned char) *p == 0x24 || /* $ */
90 !isprint((unsigned char) *p) ||
91 iscntrl((unsigned char) *p)) {
92
93 fprintf(out, "\\x%02x", (unsigned char) *p);
94 } else
95 fputc(dir == 1 ? toupper(*p) :
96 dir == -1 ? tolower(*p) :
97 *p, out);
98 }
99 fputc('"', out);
100 }
101
102 #define fputs_quoted(_d, _o) fputs_quoted_case(_d, _o, 0)
103 #define fputs_quoted_upper(_d, _o) fputs_quoted_case(_d, _o, 1)
104 #define fputs_quoted_lower(_d, _o) fputs_quoted_case(_d, _o, -1)
105
106 static inline void fputs_nonblank(const char *data, FILE *out)
107 {
108 const char *p;
109
110 for (p = data; p && *p; p++) {
111 if (isblank((unsigned char) *p) ||
112 (unsigned char) *p == 0x5c || /* \ */
113 !isprint((unsigned char) *p) ||
114 iscntrl((unsigned char) *p)) {
115
116 fprintf(out, "\\x%02x", (unsigned char) *p);
117
118 } else
119 fputc(*p, out);
120 }
121 }
122
123 #endif /* _CAREFULPUTC_H */