clang-format
This commit is contained in:
@@ -38,13 +38,11 @@
|
||||
* Print a message to stderr and bail out of the program.
|
||||
*/
|
||||
|
||||
void
|
||||
__bad_assert(const char *file, int line, const char *expr)
|
||||
{
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "Assertion failed: %s (%s line %d)\n",
|
||||
expr, file, line);
|
||||
void __bad_assert(const char *file, int line, const char *expr) {
|
||||
char buf[256];
|
||||
snprintf(buf, sizeof(buf), "Assertion failed: %s (%s line %d)\n", expr, file,
|
||||
line);
|
||||
|
||||
write(STDERR_FILENO, buf, strlen(buf));
|
||||
abort();
|
||||
write(STDERR_FILENO, buf, strlen(buf));
|
||||
abort();
|
||||
}
|
||||
|
||||
@@ -47,72 +47,60 @@ extern char **__argv;
|
||||
/*
|
||||
* Routine to print error message text to stderr.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__senderr(void *junk, const char *data, size_t len)
|
||||
{
|
||||
(void)junk; /* not needed or used */
|
||||
static void __senderr(void *junk, const char *data, size_t len) {
|
||||
(void)junk; /* not needed or used */
|
||||
|
||||
write(STDERR_FILENO, data, len);
|
||||
write(STDERR_FILENO, data, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* Shortcut to call __senderr on a null-terminated string.
|
||||
* (__senderr is set up to be called by __vprintf.)
|
||||
*/
|
||||
static
|
||||
void
|
||||
__senderrstr(const char *str)
|
||||
{
|
||||
__senderr(NULL, str, strlen(str));
|
||||
}
|
||||
static void __senderrstr(const char *str) { __senderr(NULL, str, strlen(str)); }
|
||||
|
||||
/*
|
||||
* Common routine for all the *err* and *warn* functions.
|
||||
*/
|
||||
static
|
||||
void
|
||||
__printerr(int use_errno, const char *fmt, va_list ap)
|
||||
{
|
||||
const char *errmsg;
|
||||
const char *prog;
|
||||
static void __printerr(int use_errno, const char *fmt, va_list ap) {
|
||||
const char *errmsg;
|
||||
const char *prog;
|
||||
|
||||
/*
|
||||
* Get the error message for the current errno.
|
||||
* Do this early, before doing anything that might change the
|
||||
* value in errno.
|
||||
*/
|
||||
errmsg = strerror(errno);
|
||||
/*
|
||||
* Get the error message for the current errno.
|
||||
* Do this early, before doing anything that might change the
|
||||
* value in errno.
|
||||
*/
|
||||
errmsg = strerror(errno);
|
||||
|
||||
/*
|
||||
* Look up the program name.
|
||||
* Strictly speaking we should pull off the rightmost
|
||||
* path component of argv[0] and use that as the program
|
||||
* name (this is how BSD err* prints) but it doesn't make
|
||||
* much difference.
|
||||
*/
|
||||
if (__argv!=NULL && __argv[0]!=NULL) {
|
||||
prog = __argv[0];
|
||||
}
|
||||
else {
|
||||
prog = "(program name unknown)";
|
||||
}
|
||||
/*
|
||||
* Look up the program name.
|
||||
* Strictly speaking we should pull off the rightmost
|
||||
* path component of argv[0] and use that as the program
|
||||
* name (this is how BSD err* prints) but it doesn't make
|
||||
* much difference.
|
||||
*/
|
||||
if (__argv != NULL && __argv[0] != NULL) {
|
||||
prog = __argv[0];
|
||||
} else {
|
||||
prog = "(program name unknown)";
|
||||
}
|
||||
|
||||
/* print the program name */
|
||||
__senderrstr(prog);
|
||||
__senderrstr(": ");
|
||||
/* print the program name */
|
||||
__senderrstr(prog);
|
||||
__senderrstr(": ");
|
||||
|
||||
/* process the printf format and args */
|
||||
__vprintf(__senderr, NULL, fmt, ap);
|
||||
/* process the printf format and args */
|
||||
__vprintf(__senderr, NULL, fmt, ap);
|
||||
|
||||
/* if we're using errno, print the error string from above. */
|
||||
if (use_errno) {
|
||||
__senderrstr(": ");
|
||||
__senderrstr(errmsg);
|
||||
}
|
||||
/* if we're using errno, print the error string from above. */
|
||||
if (use_errno) {
|
||||
__senderrstr(": ");
|
||||
__senderrstr(errmsg);
|
||||
}
|
||||
|
||||
/* and always add a newline. */
|
||||
__senderrstr("\n");
|
||||
/* and always add a newline. */
|
||||
__senderrstr("\n");
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -120,33 +108,21 @@ __printerr(int use_errno, const char *fmt, va_list ap)
|
||||
*/
|
||||
|
||||
/* warn/vwarn: use errno, don't exit */
|
||||
void
|
||||
vwarn(const char *fmt, va_list ap)
|
||||
{
|
||||
__printerr(1, fmt, ap);
|
||||
}
|
||||
void vwarn(const char *fmt, va_list ap) { __printerr(1, fmt, ap); }
|
||||
|
||||
/* warnx/vwarnx: don't use errno, don't exit */
|
||||
void
|
||||
vwarnx(const char *fmt, va_list ap)
|
||||
{
|
||||
__printerr(0, fmt, ap);
|
||||
}
|
||||
void vwarnx(const char *fmt, va_list ap) { __printerr(0, fmt, ap); }
|
||||
|
||||
/* err/verr: use errno, then exit */
|
||||
void
|
||||
verr(int exitcode, const char *fmt, va_list ap)
|
||||
{
|
||||
__printerr(1, fmt, ap);
|
||||
exit(exitcode);
|
||||
void verr(int exitcode, const char *fmt, va_list ap) {
|
||||
__printerr(1, fmt, ap);
|
||||
exit(exitcode);
|
||||
}
|
||||
|
||||
/* errx/verrx: don't use errno, but do then exit */
|
||||
void
|
||||
verrx(int exitcode, const char *fmt, va_list ap)
|
||||
{
|
||||
__printerr(0, fmt, ap);
|
||||
exit(exitcode);
|
||||
void verrx(int exitcode, const char *fmt, va_list ap) {
|
||||
__printerr(0, fmt, ap);
|
||||
exit(exitcode);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -154,38 +130,30 @@ verrx(int exitcode, const char *fmt, va_list ap)
|
||||
* Just hand off to the va_list versions.
|
||||
*/
|
||||
|
||||
void
|
||||
warn(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarn(fmt, ap);
|
||||
va_end(ap);
|
||||
void warn(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarn(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
warnx(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarnx(fmt, ap);
|
||||
va_end(ap);
|
||||
void warnx(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vwarnx(fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
err(int exitcode, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verr(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
void err(int exitcode, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verr(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
void
|
||||
errx(int exitcode, const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verrx(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
void errx(int exitcode, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
verrx(exitcode, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
@@ -38,54 +38,51 @@
|
||||
* POSIX C function: exec a program on the search path. Tries
|
||||
* execv() repeatedly until one of the choices works.
|
||||
*/
|
||||
int
|
||||
execvp(const char *prog, char *const *args)
|
||||
{
|
||||
const char *searchpath, *s, *t;
|
||||
char progpath[PATH_MAX];
|
||||
size_t len;
|
||||
int execvp(const char *prog, char *const *args) {
|
||||
const char *searchpath, *s, *t;
|
||||
char progpath[PATH_MAX];
|
||||
size_t len;
|
||||
|
||||
if (strchr(prog, '/') != NULL) {
|
||||
execv(prog, args);
|
||||
return -1;
|
||||
}
|
||||
if (strchr(prog, '/') != NULL) {
|
||||
execv(prog, args);
|
||||
return -1;
|
||||
}
|
||||
|
||||
searchpath = getenv("PATH");
|
||||
if (searchpath == NULL) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
searchpath = getenv("PATH");
|
||||
if (searchpath == NULL) {
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (s = searchpath; s != NULL; s = t) {
|
||||
t = strchr(s, ':');
|
||||
if (t != NULL) {
|
||||
len = t - s;
|
||||
/* advance past the colon */
|
||||
t++;
|
||||
}
|
||||
else {
|
||||
len = strlen(s);
|
||||
}
|
||||
if (len == 0) {
|
||||
continue;
|
||||
}
|
||||
if (len >= sizeof(progpath)) {
|
||||
continue;
|
||||
}
|
||||
memcpy(progpath, s, len);
|
||||
snprintf(progpath + len, sizeof(progpath) - len, "/%s", prog);
|
||||
execv(progpath, args);
|
||||
switch (errno) {
|
||||
case ENOENT:
|
||||
case ENOTDIR:
|
||||
case ENOEXEC:
|
||||
/* routine errors, try next dir */
|
||||
break;
|
||||
default:
|
||||
/* oops, let's fail */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
for (s = searchpath; s != NULL; s = t) {
|
||||
t = strchr(s, ':');
|
||||
if (t != NULL) {
|
||||
len = t - s;
|
||||
/* advance past the colon */
|
||||
t++;
|
||||
} else {
|
||||
len = strlen(s);
|
||||
}
|
||||
if (len == 0) {
|
||||
continue;
|
||||
}
|
||||
if (len >= sizeof(progpath)) {
|
||||
continue;
|
||||
}
|
||||
memcpy(progpath, s, len);
|
||||
snprintf(progpath + len, sizeof(progpath) - len, "/%s", prog);
|
||||
execv(progpath, args);
|
||||
switch (errno) {
|
||||
case ENOENT:
|
||||
case ENOTDIR:
|
||||
case ENOEXEC:
|
||||
/* routine errors, try next dir */
|
||||
break;
|
||||
default:
|
||||
/* oops, let's fail */
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
errno = ENOENT;
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -36,21 +36,19 @@
|
||||
* all the work.
|
||||
*/
|
||||
|
||||
char *
|
||||
getcwd(char *buf, size_t buflen)
|
||||
{
|
||||
int r;
|
||||
char *getcwd(char *buf, size_t buflen) {
|
||||
int r;
|
||||
|
||||
if (buflen < 1) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
if (buflen < 1) {
|
||||
errno = EINVAL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
r = __getcwd(buf, buflen-1);
|
||||
if (r < 0) {
|
||||
return NULL;
|
||||
}
|
||||
r = __getcwd(buf, buflen - 1);
|
||||
if (r < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buf[r] = 0;
|
||||
return buf;
|
||||
buf[r] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user