// SPDX-License-Identifier: GPL-2.0-or-later /* * Copyright 2011 The Chromium Authors, All Rights Reserved. * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc. * * util_is_printable_string contributed by * Pantelis Antoniou <pantelis.antoniou AT gmail.com>
*/
/* must terminate with zero */ if (s[len - 1] != '\0') return 0;
se = s + len;
while (s < se) {
ss = s; while (s < se && *s && isprint((unsignedchar)*s))
s++;
/* not zero, or not done yet */ if (*s != '\0' || s == ss) return 0;
s++;
}
return 1;
}
/* * Parse a octal encoded character starting at index i in string s. The * resulting character will be returned and the index i will be updated to * point at the character directly after the end of the encoding, this may be * the '\0' terminator of the string.
*/ staticchar get_oct_char(constchar *s, int *i)
{ char x[4]; char *endx; long val;
x[3] = '\0';
strncpy(x, s + *i, 3);
val = strtol(x, &endx, 8);
assert(endx > x);
(*i) += endx - x; return val;
}
/* * Parse a hexadecimal encoded character starting at index i in string s. The * resulting character will be returned and the index i will be updated to * point at the character directly after the end of the encoding, this may be * the '\0' terminator of the string.
*/ staticchar get_hex_char(constchar *s, int *i)
{ char x[3]; char *endx; long val;
x[2] = '\0';
strncpy(x, s + *i, 2);
val = strtol(x, &endx, 16); if (!(endx > x))
die("\\x used with no following hex digits\n");
(*i) += endx - x; return val;
}
char get_escape_char(constchar *s, int *i)
{ char c = s[*i]; int j = *i + 1; char val;
switch (c) { case'a':
val = '\a'; break; case'b':
val = '\b'; break; case't':
val = '\t'; break; case'n':
val = '\n'; break; case'v':
val = '\v'; break; case'f':
val = '\f'; break; case'r':
val = '\r'; break; case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7':
j--; /* need to re-read the first digit as
* part of the octal value */
val = get_oct_char(s, &j); break; case'x':
val = get_hex_char(s, &j); break; default:
val = c;
}
(*i) = j; return val;
}
int utilfdt_read_err(constchar *filename, char **buffp, size_t *len)
{ int fd = 0; /* assume stdin */ char *buf = NULL;
size_t bufsize = 1024, offset = 0; int ret = 0;
*buffp = NULL; if (strcmp(filename, "-") != 0) {
fd = open(filename, O_RDONLY); if (fd < 0) return errno;
}
/* Loop until we have read everything */
buf = xmalloc(bufsize); do { /* Expand the buffer to hold the next chunk */ if (offset == bufsize) {
bufsize *= 2;
buf = xrealloc(buf, bufsize);
}
ret = read(fd, &buf[offset], bufsize - offset); if (ret < 0) {
ret = errno; break;
}
offset += ret;
} while (ret != 0);
/* Clean up, including closing stdin; return errno on error */
close(fd); if (ret)
free(buf); else
*buffp = buf; if (len)
*len = bufsize; return ret;
}
char *utilfdt_read(constchar *filename, size_t *len)
{ char *buff; int ret = utilfdt_read_err(filename, &buff, len);
if (ret) {
fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
strerror(ret)); return NULL;
} /* Successful read */ return buff;
}
int utilfdt_write_err(constchar *filename, constvoid *blob)
{ int fd = 1; /* assume stdout */ int totalsize; int offset; int ret = 0; constchar *ptr = blob;
/* prescan the --long opt length to auto-align */
optlen = 0; for (i = 0; long_opts[i].name; ++i) { /* +1 is for space between --opt and help text */ int l = strlen(long_opts[i].name) + 1; if (long_opts[i].has_arg == a_argument)
l += a_arg_len; if (optlen < l)
optlen = l;
}
for (i = 0; long_opts[i].name; ++i) { /* helps when adding new applets or options */
assert(opts_help[i] != NULL);
/* first output the short flag if it has one */ if (long_opts[i].val > '~')
fprintf(fp, " "); else
fprintf(fp, " -%c, ", long_opts[i].val);
/* then the long flag */ if (long_opts[i].has_arg == no_argument)
fprintf(fp, "--%-*s", optlen, long_opts[i].name); else
fprintf(fp, "--%s %s%*s", long_opts[i].name, a_arg,
(int)(optlen - strlen(long_opts[i].name) - a_arg_len), "");
/* finally the help text */
fprintf(fp, "%s\n", opts_help[i]);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.