2013-11-26 22:55:28 +00:00
|
|
|
/* strerror.c --- POSIX compatible system error routine
|
|
|
|
|
2023-10-18 07:29:37 +00:00
|
|
|
Copyright (C) 2007-2023 Free Software Foundation, Inc.
|
2013-11-26 22:55:28 +00:00
|
|
|
|
2023-10-18 07:29:37 +00:00
|
|
|
This file is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
License, or (at your option) any later version.
|
2013-11-26 22:55:28 +00:00
|
|
|
|
2023-10-18 07:29:37 +00:00
|
|
|
This file is distributed in the hope that it will be useful,
|
2013-11-26 22:55:28 +00:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2023-10-18 07:29:37 +00:00
|
|
|
GNU Lesser General Public License for more details.
|
2013-11-26 22:55:28 +00:00
|
|
|
|
2023-10-18 07:29:37 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2013-11-26 22:55:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
/* Specification. */
|
2013-11-26 22:55:28 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2013-11-26 22:56:50 +00:00
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
#include "intprops.h"
|
|
|
|
#include "strerror-override.h"
|
2013-11-26 22:55:28 +00:00
|
|
|
|
2013-11-26 22:57:29 +00:00
|
|
|
/* Use the system functions, not the gnulib overrides in this file. */
|
2013-11-26 22:59:47 +00:00
|
|
|
#undef sprintf
|
2013-11-26 22:55:28 +00:00
|
|
|
|
|
|
|
char *
|
2013-11-26 22:59:47 +00:00
|
|
|
strerror (int n)
|
|
|
|
#undef strerror
|
2013-11-26 22:55:28 +00:00
|
|
|
{
|
2013-11-26 22:59:47 +00:00
|
|
|
static char buf[STACKBUF_LEN];
|
|
|
|
size_t len;
|
2013-11-26 22:55:28 +00:00
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
/* Cast away const, due to the historical signature of strerror;
|
|
|
|
callers should not be modifying the string. */
|
|
|
|
const char *msg = strerror_override (n);
|
2013-11-26 22:56:50 +00:00
|
|
|
if (msg)
|
|
|
|
return (char *) msg;
|
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
msg = strerror (n);
|
2013-11-26 22:56:50 +00:00
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
/* Our strerror_r implementation might use the system's strerror
|
|
|
|
buffer, so all other clients of strerror have to see the error
|
|
|
|
copied into a buffer that we manage. This is not thread-safe,
|
|
|
|
even if the system strerror is, but portable programs shouldn't
|
|
|
|
be using strerror if they care about thread-safety. */
|
|
|
|
if (!msg || !*msg)
|
|
|
|
{
|
|
|
|
static char const fmt[] = "Unknown error %d";
|
2023-10-18 07:29:37 +00:00
|
|
|
static_assert (sizeof buf >= sizeof (fmt) + INT_STRLEN_BOUND (n));
|
2013-11-26 22:59:47 +00:00
|
|
|
sprintf (buf, fmt, n);
|
|
|
|
errno = EINVAL;
|
|
|
|
return buf;
|
|
|
|
}
|
2013-11-26 22:56:50 +00:00
|
|
|
|
2013-11-26 22:59:47 +00:00
|
|
|
/* Fix STACKBUF_LEN if this ever aborts. */
|
|
|
|
len = strlen (msg);
|
|
|
|
if (sizeof buf <= len)
|
|
|
|
abort ();
|
2013-11-26 22:55:28 +00:00
|
|
|
|
2023-10-18 07:29:37 +00:00
|
|
|
memcpy (buf, msg, len + 1);
|
|
|
|
return buf;
|
2013-11-26 22:59:47 +00:00
|
|
|
}
|