Blame lib/strerror.c

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