2017-10-24 23:19:19 -05:00
|
|
|
#ifndef UTIL_LINUX_CAREFULPUTC_H
|
|
|
|
#define UTIL_LINUX_CAREFULPUTC_H
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2014-04-28 11:08:42 -05:00
|
|
|
/*
|
|
|
|
* A putc() for use in write and wall (that sometimes are sgid tty).
|
|
|
|
* It avoids control characters in our locale, and also ASCII control
|
|
|
|
* characters. Note that the locale of the recipient is unknown.
|
|
|
|
*/
|
2006-12-06 17:25:44 -06:00
|
|
|
#include <stdio.h>
|
2014-03-19 07:25:45 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
2006-12-06 17:25:44 -06:00
|
|
|
|
2019-08-08 14:12:55 -05:00
|
|
|
#include "cctype.h"
|
|
|
|
|
2014-08-10 12:23:14 -05:00
|
|
|
static inline int fputc_careful(int c, FILE *fp, const char fail)
|
|
|
|
{
|
2006-12-06 17:25:44 -06:00
|
|
|
int ret;
|
|
|
|
|
2014-08-10 12:23:14 -05:00
|
|
|
if (isprint(c) || c == '\a' || c == '\t' || c == '\r' || c == '\n')
|
2006-12-06 17:25:44 -06:00
|
|
|
ret = putc(c, fp);
|
2019-08-08 14:12:55 -05:00
|
|
|
else if (!c_isascii(c))
|
2014-08-10 12:23:14 -05:00
|
|
|
ret = fprintf(fp, "\\%3o", (unsigned char)c);
|
2006-12-06 17:25:44 -06:00
|
|
|
else {
|
2013-08-17 13:15:11 -05:00
|
|
|
ret = putc(fail, fp);
|
2006-12-06 17:25:44 -06:00
|
|
|
if (ret != EOF)
|
2014-08-10 12:23:14 -05:00
|
|
|
ret = putc(c ^ 0x40, fp);
|
2006-12-06 17:25:44 -06:00
|
|
|
}
|
|
|
|
return (ret < 0) ? EOF : 0;
|
|
|
|
}
|
2006-12-06 17:26:54 -06:00
|
|
|
|
2015-06-05 04:58:42 -05:00
|
|
|
static inline void fputs_quoted_case(const char *data, FILE *out, int dir)
|
2014-03-19 07:25:45 -05:00
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
fputc('"', out);
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
if ((unsigned char) *p == 0x22 || /* " */
|
|
|
|
(unsigned char) *p == 0x5c || /* \ */
|
2014-11-27 06:36:09 -06:00
|
|
|
(unsigned char) *p == 0x60 || /* ` */
|
|
|
|
(unsigned char) *p == 0x24 || /* $ */
|
2014-03-19 07:25:45 -05:00
|
|
|
!isprint((unsigned char) *p) ||
|
|
|
|
iscntrl((unsigned char) *p)) {
|
|
|
|
|
|
|
|
fprintf(out, "\\x%02x", (unsigned char) *p);
|
|
|
|
} else
|
2015-06-05 04:58:42 -05:00
|
|
|
fputc(dir == 1 ? toupper(*p) :
|
|
|
|
dir == -1 ? tolower(*p) :
|
|
|
|
*p, out);
|
2014-03-19 07:25:45 -05:00
|
|
|
}
|
|
|
|
fputc('"', out);
|
|
|
|
}
|
|
|
|
|
2015-06-05 04:58:42 -05:00
|
|
|
#define fputs_quoted(_d, _o) fputs_quoted_case(_d, _o, 0)
|
|
|
|
#define fputs_quoted_upper(_d, _o) fputs_quoted_case(_d, _o, 1)
|
|
|
|
#define fputs_quoted_lower(_d, _o) fputs_quoted_case(_d, _o, -1)
|
|
|
|
|
2014-03-19 07:25:45 -05:00
|
|
|
static inline void fputs_nonblank(const char *data, FILE *out)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
if (isblank((unsigned char) *p) ||
|
|
|
|
(unsigned char) *p == 0x5c || /* \ */
|
|
|
|
!isprint((unsigned char) *p) ||
|
|
|
|
iscntrl((unsigned char) *p)) {
|
|
|
|
|
|
|
|
fprintf(out, "\\x%02x", (unsigned char) *p);
|
|
|
|
|
|
|
|
} else
|
|
|
|
fputc(*p, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-03 05:14:10 -06:00
|
|
|
static inline void fputs_shell_ident(const char *data, FILE *out)
|
|
|
|
{
|
|
|
|
const char *p = data;
|
|
|
|
|
|
|
|
/* convert "1FOO" to "_1FOO" */
|
|
|
|
if (p && !isalpha(*p))
|
|
|
|
fputc('_', out);
|
|
|
|
|
|
|
|
/* replace all "bad" chars with "_" */
|
|
|
|
for (p = data; p && *p; p++) {
|
|
|
|
if (!isalnum(*p))
|
|
|
|
fputc('_', out);
|
|
|
|
else
|
|
|
|
fputc(*p, out);
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 07:25:45 -05:00
|
|
|
|
2017-10-24 23:19:19 -05:00
|
|
|
#endif /* _CAREFULPUTC_H */
|