Blame gnulib/tests/strerror.c

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