pkg-monitoring-plugins/gl/strerror.c

71 lines
2 KiB
C
Raw Normal View History

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>
/* Specification. */
2013-11-26 22:55:28 +00:00
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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. */
#undef sprintf
2013-11-26 22:55:28 +00:00
char *
strerror (int n)
#undef strerror
2013-11-26 22:55:28 +00:00
{
static char buf[STACKBUF_LEN];
size_t len;
2013-11-26 22:55:28 +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);
if (msg)
return (char *) msg;
msg = strerror (n);
/* 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));
sprintf (buf, fmt, n);
errno = EINVAL;
return buf;
}
/* 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;
}