| Summary: | libguestfs uses putc on stderr, results in many individual 1 byte writes of debug messages | ||
|---|---|---|---|
| Product: | [Community] Virtualization Tools | Reporter: | Richard W.M. Jones <rjones> |
| Component: | libguestfs | Assignee: | Richard W.M. Jones <rjones> |
| Status: | CLOSED UPSTREAM | QA Contact: | |
| Severity: | unspecified | Docs Contact: | |
| Priority: | unspecified | ||
| Version: | unspecified | CC: | mbooth, meyering, virt-maint |
| Target Milestone: | --- | ||
| Target Release: | --- | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
| Whiteboard: | |||
| Fixed In Version: | Doc Type: | Bug Fix | |
| Doc Text: | Story Points: | --- | |
| Clone Of: | Environment: | ||
| Last Closed: | 2012-03-13 09:28:01 UTC | Type: | --- |
| Regression: | --- | Mount Type: | --- |
| Documentation: | --- | CRM: | |
| Verified Versions: | Category: | --- | |
| oVirt Team: | --- | RHEL 7.3 requirements from Atomic Host: | |
| Cloudforms Team: | --- | Target Upstream Version: | |
|
Description
Richard W.M. Jones
2012-03-11 11:19:48 UTC
CC to Jim Meyering. Any comment on the right way to fix (or ignore?) this problem? Hi Rich,
I presume you don't want to allocate memory, otherwise it'd be trivial:
write the escaped bytes into that buffer and then print the result
with fwrite.
A compromise is to identify contiguous bytes that will not
be escaped and print them using fwrite. Then handle the
presumably few escapables, then look for more contiguous
unescapables, etc:
#include <stdio.h>
#include <string.h>
static char const *
print_to_first_quotable (char const *p0, char const *end)
{
int quoted = 0;
char const *p = p0;
while (p < end)
{
switch (*p)
{
case '\0': quoted = '0'; goto done;
case '\a': quoted = 'a'; goto done;
case '\b': quoted = 'b'; goto done;
case '\f': quoted = 'f'; goto done;
case '\n': quoted = 'n'; goto done;
case '\r': quoted = 'r'; goto done;
case '\t': quoted = 't'; goto done;
case '\v': quoted = 'v'; goto done;
default:
p++;
break;
}
}
done:
fwrite (p0, 1, p - p0, stderr);
if (quoted)
{
fputc ('\\', stderr);
fputc (quoted, stderr);
}
return p + 1;
}
int
main (int argc, char **argv)
{
if (argc < 2)
return 99;
char const *p = argv[1];
char const *end = p + strlen (p);
while (p < end)
{
p = print_to_first_quotable (p, end);
}
return 0;
}
=======================
If you care about the syscall-per-backslash+escaped byte,
you could put the two of them in a 2-byte buffer and save half
of the fputc calls. If you care about strings of two or more
escapables, add a small local buffer of length say 10-20,
and handle sequences of escapables (up to half of that length)
the same way.
|