Blame gnulib/lib/strerror.c

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