Blame lib/strerror.c

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