view lib/strerror.c @ 14758:0ed8de9d6bbe

strerror_r: Avoid clobbering the strerror buffer when possible. * lib/strerror_r.c (strerror_r): Merge the three implementations. Handle gnulib defined errno values here. When strerror() returns NULL or an empty string, return EINVAL. * lib/strerror.c (strerror): Always call strerror_r. Don't handle gnulib defined errno values here. * modules/strerror (Depends-on): Add verify, strerror_r-posix.
author Bruno Haible <bruno@clisp.org>
date Thu, 19 May 2011 05:24:33 +0200 (2011-05-19)
parents 97fc9a21a8fb
children ff7953936223
line wrap: on
line source
/* strerror.c --- POSIX compatible system error routine

   Copyright (C) 2007-2011 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>

/* Specification.  */
#include <string.h>

#if REPLACE_STRERROR

# include <errno.h>
# include <stdio.h>
# include <stdlib.h>

# include "intprops.h"
# include "verify.h"

/* Use the system functions, not the gnulib overrides in this file.  */
# undef sprintf

char *
strerror (int n)
{
  static char buf[256];

  int ret = strerror_r (n, buf, sizeof (buf));

  if (ret == 0)
    return buf;

  if (ret == ERANGE)
    /* If this happens, increase the size of buf.  */
    abort ();

  {
    static char const fmt[] = "Unknown error (%d)";
    verify (sizeof (buf) >= sizeof (fmt) + INT_STRLEN_BOUND (n));
    sprintf (buf, fmt, n);
    return buf;
  }
}

#endif