Blame lib/strerror.c

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