~ubuntu-branches/ubuntu/precise/wget/precise-proposed

« back to all changes in this revision

Viewing changes to lib/error.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-10-19 00:00:09 UTC
  • mfrom: (2.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20111019000009-8p33w3wz4b1rdri0
Tags: 1.13-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add wget-udeb to ship wget.gnu as alternative to busybox wget
    implementation.
  - Depend on libssl-dev 0.9.8k-7ubuntu4 (LP: #503339)
* Dropped changes, superseded in Debian:
  - Keep build dependencies in main:
    + debian/control: remove info2man build-dep
    + debian/patches/series: disable wget-infopod_generated_manpage
  - Mark wget Multi-Arch: foreign, so packages that aren't of the same arch
    can depend on it.
* Pass --with-ssl=openssl; we don't want to use gnutls, there's no udeb for
  it.
* Add a second build pass for the udeb, so we can build without libidn.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Error handler for noninteractive utilities
2
 
   Copyright (C) 1990-1998, 2000-2007, 2009 Free Software Foundation, Inc.
 
2
   Copyright (C) 1990-1998, 2000-2007, 2009-2011 Free Software Foundation, Inc.
3
3
   This file is part of the GNU C Library.
4
4
 
5
5
   This program is free software: you can redistribute it and/or modify
70
70
extern void __error (int status, int errnum, const char *message, ...)
71
71
     __attribute__ ((__format__ (__printf__, 3, 4)));
72
72
extern void __error_at_line (int status, int errnum, const char *file_name,
73
 
                             unsigned int line_number, const char *message,
74
 
                             ...)
 
73
                             unsigned int line_number, const char *message,
 
74
                             ...)
75
75
     __attribute__ ((__format__ (__printf__, 5, 6)));;
76
76
# define error __error
77
77
# define error_at_line __error_at_line
86
86
#else /* not _LIBC */
87
87
 
88
88
# include <fcntl.h>
89
 
 
90
 
# if !HAVE_DECL_STRERROR_R && STRERROR_R_CHAR_P
 
89
# include <unistd.h>
 
90
 
 
91
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 
92
/* Get declarations of the Win32 API functions.  */
 
93
#  define WIN32_LEAN_AND_MEAN
 
94
#  include <windows.h>
 
95
# endif
 
96
 
 
97
/* The gnulib override of fcntl is not needed in this file.  */
 
98
# undef fcntl
 
99
 
 
100
# if !HAVE_DECL_STRERROR_R
91
101
#  ifndef HAVE_DECL_STRERROR_R
92
102
"this configure-time declaration test was not run"
93
103
#  endif
 
104
#  if STRERROR_R_CHAR_P
94
105
char *strerror_r ();
 
106
#  else
 
107
int strerror_r ();
 
108
#  endif
95
109
# endif
96
110
 
97
111
/* The calling program should define program_name and set it to the
100
114
 
101
115
# if HAVE_STRERROR_R || defined strerror_r
102
116
#  define __strerror_r strerror_r
103
 
# endif /* HAVE_STRERROR_R || defined strerror_r */
104
 
#endif  /* not _LIBC */
 
117
# endif /* HAVE_STRERROR_R || defined strerror_r */
 
118
#endif  /* not _LIBC */
 
119
 
 
120
#if !_LIBC
 
121
/* Return non-zero if FD is open.  */
 
122
static inline int
 
123
is_open (int fd)
 
124
{
 
125
# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 
126
  /* On Win32: The initial state of unassigned standard file descriptors is
 
127
     that they are open but point to an INVALID_HANDLE_VALUE.  There is no
 
128
     fcntl, and the gnulib replacement fcntl does not support F_GETFL.  */
 
129
  return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE;
 
130
# else
 
131
#  ifndef F_GETFL
 
132
#   error Please port fcntl to your platform
 
133
#  endif
 
134
  return 0 <= fcntl (fd, F_GETFL);
 
135
# endif
 
136
}
 
137
#endif
 
138
 
 
139
static inline void
 
140
flush_stdout (void)
 
141
{
 
142
#if !_LIBC
 
143
  int stdout_fd;
 
144
 
 
145
# if GNULIB_FREOPEN_SAFER
 
146
  /* Use of gnulib's freopen-safer module normally ensures that
 
147
       fileno (stdout) == 1
 
148
     whenever stdout is open.  */
 
149
  stdout_fd = STDOUT_FILENO;
 
150
# else
 
151
  /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
 
152
     practice it is not a problem, because stdout is statically allocated and
 
153
     the fd of a FILE stream is stored as a field in its allocated memory.  */
 
154
  stdout_fd = fileno (stdout);
 
155
# endif
 
156
  /* POSIX states that fflush (stdout) after fclose is unspecified; it
 
157
     is safe in glibc, but not on all other platforms.  fflush (NULL)
 
158
     is always defined, but too draconian.  */
 
159
  if (0 <= stdout_fd && is_open (stdout_fd))
 
160
#endif
 
161
    fflush (stdout);
 
162
}
105
163
 
106
164
static void
107
165
print_errno_message (int errnum)
149
207
      bool use_malloc = false;
150
208
 
151
209
      while (1)
152
 
        {
153
 
          if (__libc_use_alloca (len * sizeof (wchar_t)))
154
 
            wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
155
 
          else
156
 
            {
157
 
              if (!use_malloc)
158
 
                wmessage = NULL;
159
 
 
160
 
              wchar_t *p = (wchar_t *) realloc (wmessage,
161
 
                                                len * sizeof (wchar_t));
162
 
              if (p == NULL)
163
 
                {
164
 
                  free (wmessage);
165
 
                  fputws_unlocked (L"out of memory\n", stderr);
166
 
                  return;
167
 
                }
168
 
              wmessage = p;
169
 
              use_malloc = true;
170
 
            }
171
 
 
172
 
          memset (&st, '\0', sizeof (st));
173
 
          tmp = message;
174
 
 
175
 
          res = mbsrtowcs (wmessage, &tmp, len, &st);
176
 
          if (res != len)
177
 
            break;
178
 
 
179
 
          if (__builtin_expect (len >= SIZE_MAX / 2, 0))
180
 
            {
181
 
              /* This really should not happen if everything is fine.  */
182
 
              res = (size_t) -1;
183
 
              break;
184
 
            }
185
 
 
186
 
          len *= 2;
187
 
        }
 
210
        {
 
211
          if (__libc_use_alloca (len * sizeof (wchar_t)))
 
212
            wmessage = (wchar_t *) alloca (len * sizeof (wchar_t));
 
213
          else
 
214
            {
 
215
              if (!use_malloc)
 
216
                wmessage = NULL;
 
217
 
 
218
              wchar_t *p = (wchar_t *) realloc (wmessage,
 
219
                                                len * sizeof (wchar_t));
 
220
              if (p == NULL)
 
221
                {
 
222
                  free (wmessage);
 
223
                  fputws_unlocked (L"out of memory\n", stderr);
 
224
                  return;
 
225
                }
 
226
              wmessage = p;
 
227
              use_malloc = true;
 
228
            }
 
229
 
 
230
          memset (&st, '\0', sizeof (st));
 
231
          tmp = message;
 
232
 
 
233
          res = mbsrtowcs (wmessage, &tmp, len, &st);
 
234
          if (res != len)
 
235
            break;
 
236
 
 
237
          if (__builtin_expect (len >= SIZE_MAX / 2, 0))
 
238
            {
 
239
              /* This really should not happen if everything is fine.  */
 
240
              res = (size_t) -1;
 
241
              break;
 
242
            }
 
243
 
 
244
          len *= 2;
 
245
        }
188
246
 
189
247
      if (res == (size_t) -1)
190
 
        {
191
 
          /* The string cannot be converted.  */
192
 
          if (use_malloc)
193
 
            {
194
 
              free (wmessage);
195
 
              use_malloc = false;
196
 
            }
197
 
          wmessage = (wchar_t *) L"???";
198
 
        }
 
248
        {
 
249
          /* The string cannot be converted.  */
 
250
          if (use_malloc)
 
251
            {
 
252
              free (wmessage);
 
253
              use_malloc = false;
 
254
            }
 
255
          wmessage = (wchar_t *) L"???";
 
256
        }
199
257
 
200
258
      __vfwprintf (stderr, wmessage, args);
201
259
 
202
260
      if (use_malloc)
203
 
        free (wmessage);
 
261
        free (wmessage);
204
262
    }
205
263
  else
206
264
#endif
235
293
     cancellation.  Therefore disable cancellation for now.  */
236
294
  int state = PTHREAD_CANCEL_ENABLE;
237
295
  __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
238
 
                   0);
 
296
                   0);
239
297
#endif
240
298
 
241
 
#if !_LIBC && defined F_GETFL
242
 
  /* POSIX states that fflush (stdout) after fclose is unspecified; it
243
 
     is safe in glibc, but not on all other platforms.  fflush (NULL)
244
 
     is always defined, but too draconian.  */
245
 
  if (0 <= fcntl (1, F_GETFL))
246
 
#endif
247
 
  fflush (stdout);
 
299
  flush_stdout ();
248
300
#ifdef _LIBC
249
301
  _IO_flockfile (stderr);
250
302
#endif
276
328
 
277
329
void
278
330
error_at_line (int status, int errnum, const char *file_name,
279
 
               unsigned int line_number, const char *message, ...)
 
331
               unsigned int line_number, const char *message, ...)
280
332
{
281
333
  va_list args;
282
334
 
286
338
      static unsigned int old_line_number;
287
339
 
288
340
      if (old_line_number == line_number
289
 
          && (file_name == old_file_name
290
 
              || strcmp (old_file_name, file_name) == 0))
291
 
        /* Simply return and print nothing.  */
292
 
        return;
 
341
          && (file_name == old_file_name
 
342
              || strcmp (old_file_name, file_name) == 0))
 
343
        /* Simply return and print nothing.  */
 
344
        return;
293
345
 
294
346
      old_file_name = file_name;
295
347
      old_line_number = line_number;
300
352
     cancellation.  Therefore disable cancellation for now.  */
301
353
  int state = PTHREAD_CANCEL_ENABLE;
302
354
  __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state),
303
 
                   0);
 
355
                   0);
304
356
#endif
305
357
 
306
 
#if !_LIBC && defined F_GETFL
307
 
  /* POSIX states that fflush (stdout) after fclose is unspecified; it
308
 
     is safe in glibc, but not on all other platforms.  fflush (NULL)
309
 
     is always defined, but too draconian.  */
310
 
  if (0 <= fcntl (1, F_GETFL))
311
 
#endif
312
 
  fflush (stdout);
 
358
  flush_stdout ();
313
359
#ifdef _LIBC
314
360
  _IO_flockfile (stderr);
315
361
#endif
326
372
 
327
373
#if _LIBC
328
374
  __fxprintf (NULL, file_name != NULL ? "%s:%d: " : " ",
329
 
              file_name, line_number);
 
375
              file_name, line_number);
330
376
#else
331
377
  fprintf (stderr, file_name != NULL ? "%s:%d: " : " ",
332
 
           file_name, line_number);
 
378
           file_name, line_number);
333
379
#endif
334
380
 
335
381
  va_start (args, message);