~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to lib/strerror.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 2004 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 2004 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: strerror.c,v 1.50 2008-05-12 21:43:29 bagder Exp $
 
21
 * $Id: strerror.c,v 1.54 2008-09-12 10:51:57 yangtse Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
25
25
 
26
26
#ifdef HAVE_STRERROR_R
27
 
#if !defined(HAVE_POSIX_STRERROR_R) && !defined(HAVE_GLIBC_STRERROR_R)
28
 
#error "you MUST have either POSIX or glibc strerror_r if strerror_r is found"
29
 
#endif /* !POSIX && !glibc */
30
 
#endif /* HAVE_STRERROR_R */
 
27
#  if (!defined(HAVE_POSIX_STRERROR_R) && !defined(HAVE_GLIBC_STRERROR_R)) || \
 
28
       (defined(HAVE_POSIX_STRERROR_R) &&  defined(HAVE_GLIBC_STRERROR_R))
 
29
#    error "strerror_r MUST be either POSIX-style or glibc-style"
 
30
#  endif
 
31
#endif
31
32
 
32
33
#include <curl/curl.h>
33
34
#include <stdlib.h>
43
44
#define _MPRINTF_REPLACE /* use our functions only */
44
45
#include <curl/mprintf.h>
45
46
 
46
 
#if defined(HAVE_STRERROR_R) && defined(HAVE_NO_STRERROR_R_DECL)
47
 
#ifdef HAVE_POSIX_STRERROR_R
48
 
/* seen on AIX 5100-02 gcc 2.9 */
49
 
extern int strerror_r(int errnum, char *strerrbuf, size_t buflen);
50
 
#else
51
 
extern char *strerror_r(int errnum, char *buf, size_t buflen);
52
 
#endif
53
 
#endif
54
47
 
55
48
const char *
56
49
curl_easy_strerror(CURLcode error)
222
215
  case CURLE_SSL_SHUTDOWN_FAILED:
223
216
    return "Failed to shut down the SSL connection";
224
217
 
 
218
  case CURLE_SSL_CRL_BADFILE:
 
219
    return "Failed to load CRL file (path? access rights?, format?)";
 
220
 
 
221
  case CURLE_SSL_ISSUER_ERROR:
 
222
    return "Issuer check against peer certificate failed";
 
223
 
225
224
  case CURLE_SEND_FAIL_REWIND:
226
225
    return "Send failed since rewinding of the data stream failed";
227
226
 
584
583
{
585
584
  char *buf, *p;
586
585
  size_t max;
 
586
  int old_errno = ERRNO;
587
587
 
588
588
  DEBUGASSERT(conn);
589
589
  DEBUGASSERT(err >= 0);
595
595
#ifdef USE_WINSOCK
596
596
 
597
597
#ifdef _WIN32_WCE
598
 
  buf[0]=0;
599
598
  {
600
599
    wchar_t wbuf[256];
 
600
    wbuf[0] = L'\0';
601
601
 
602
602
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err,
603
603
                  LANG_NEUTRAL, wbuf, sizeof(wbuf)/sizeof(wchar_t), NULL);
604
604
    wcstombs(buf,wbuf,max);
605
605
  }
606
 
 
607
606
#else
608
 
 
609
607
  /* 'sys_nerr' is the maximum errno number, it is not widely portable */
610
608
  if(err >= 0 && err < sys_nerr)
611
609
    strncpy(buf, strerror(err), max);
616
614
      snprintf(buf, max, "Unknown error %d (%#x)", err, err);
617
615
  }
618
616
#endif
 
617
 
619
618
#else /* not USE_WINSOCK coming up */
620
619
 
621
 
  /* These should be atomic and hopefully thread-safe */
622
 
#ifdef HAVE_STRERROR_R
623
 
  /* There are two different APIs for strerror_r(). The POSIX and the GLIBC
624
 
     versions. */
625
 
#ifdef HAVE_POSIX_STRERROR_R
626
 
  strerror_r(err, buf, max);
627
 
  /* this may set errno to ERANGE if insufficient storage was supplied via
628
 
     'strerrbuf' and 'buflen' to contain the generated message string, or
629
 
     EINVAL if the value of 'errnum' is not a valid error number.*/
630
 
#else
 
620
#if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
 
621
 /*
 
622
  * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
 
623
  * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
 
624
  * message string, or EINVAL if 'errnum' is not a valid error number.
 
625
  */
 
626
  if(0 != strerror_r(err, buf, max)) {
 
627
    if('\0' == buf[0])
 
628
      snprintf(buf, max, "Unknown error %d", err);
 
629
  }
 
630
#elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
 
631
 /*
 
632
  * The glibc-style strerror_r() only *might* use the buffer we pass to
 
633
  * the function, but it always returns the error message as a pointer,
 
634
  * so we must copy that string unconditionally (if non-NULL).
 
635
  */
631
636
  {
632
 
    /* HAVE_GLIBC_STRERROR_R */
633
637
    char buffer[256];
634
638
    char *msg = strerror_r(err, buffer, sizeof(buffer));
635
 
    /* this version of strerror_r() only *might* use the buffer we pass to
636
 
       the function, but it always returns the error message as a pointer,
637
 
       so we must copy that string unconditionally (if non-NULL) */
638
 
    if(msg)
639
 
      strncpy(buf, msg, max);
640
 
    else
641
 
      snprintf(buf, max, "Unknown error %d", err);
642
 
  }
643
 
#endif /* end of HAVE_GLIBC_STRERROR_R */
644
 
#else /* HAVE_STRERROR_R */
645
 
  strncpy(buf, strerror(err), max);
646
 
#endif /* end of HAVE_STRERROR_R */
 
639
    if(msg)
 
640
      strncpy(buf, msg, max);
 
641
    else
 
642
      snprintf(buf, max, "Unknown error %d", err);
 
643
  }
 
644
#else
 
645
  {
 
646
    char *msg = strerror(err);
 
647
    if(msg)
 
648
      strncpy(buf, msg, max);
 
649
    else
 
650
      snprintf(buf, max, "Unknown error %d", err);
 
651
  }
 
652
#endif
 
653
 
647
654
#endif /* end of ! USE_WINSOCK */
648
655
 
649
656
  buf[max] = '\0'; /* make sure the string is zero terminated */
653
660
     *p = '\0';
654
661
  if((p = strrchr(buf,'\r')) != NULL && (p - buf) >= 1)
655
662
     *p = '\0';
 
663
 
 
664
  if(old_errno != ERRNO)
 
665
    SET_ERRNO(old_errno);
 
666
 
656
667
  return buf;
657
668
}
658
669
 
674
685
 
675
686
  buf = conn->syserr_buf;
676
687
  max = sizeof(conn->syserr_buf)-1;
 
688
  *buf = '\0';
677
689
 
678
690
#ifndef CURL_DISABLE_VERBOSE_STRINGS
679
691
  switch ((Idna_rc)err) {
714
726
      str = "dlopen() error";
715
727
      break;
716
728
    default:
717
 
      snprintf(buf, max, "error %d", (int)err);
 
729
      snprintf(buf, max, "error %d", err);
718
730
      str = NULL;
719
731
      break;
720
732
  }