The R Project SVN R

Rev

Rev 36792 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36792 Rev 41539
Line 1... Line 1...
1
/* Formatted output to strings, using POSIX/XSI format strings with positions.
1
/* Formatted output to strings, using POSIX/XSI format strings with positions.
2
   Copyright (C) 2003 Free Software Foundation, Inc.
2
   Copyright (C) 2003, 2006 Free Software Foundation, Inc.
3
   Written by Bruno Haible <bruno@clisp.org>, 2003.
3
   Written by Bruno Haible <bruno@clisp.org>, 2003.
4
 
4
 
5
   This program is free software; you can redistribute it and/or modify it
5
   This program is free software; you can redistribute it and/or modify it
6
   under the terms of the GNU Library General Public License as published
6
   under the terms of the GNU Library General Public License as published
7
   by the Free Software Foundation; either version 2, or (at your option)
7
   by the Free Software Foundation; either version 2, or (at your option)
Line 45... Line 45...
45
 
45
 
46
#include <stdio.h>
46
#include <stdio.h>
47
 
47
 
48
#if !HAVE_POSIX_PRINTF
48
#if !HAVE_POSIX_PRINTF
49
 
49
 
-
 
50
#include <errno.h>
-
 
51
#include <limits.h>
50
#include <stdlib.h>
52
#include <stdlib.h>
51
#include <string.h>
53
#include <string.h>
52
 
54
 
-
 
55
/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
-
 
56
#ifndef EOVERFLOW
-
 
57
# define EOVERFLOW E2BIG
-
 
58
#endif
-
 
59
 
53
/* When building a DLL, we must export some functions.  Note that because
60
/* When building a DLL, we must export some functions.  Note that because
54
   the functions are only defined for binary backward compatibility, we
61
   the functions are only defined for binary backward compatibility, we
55
   don't need to use __declspec(dllimport) in any case.  */
62
   don't need to use __declspec(dllimport) in any case.  */
56
#if defined _MSC_VER && BUILDING_DLL
63
#if defined _MSC_VER && BUILDING_DLL
57
# define DLL_EXPORTED __declspec(dllexport)
64
# define DLL_EXPORTED __declspec(dllexport)
Line 59... Line 66...
59
# define DLL_EXPORTED
66
# define DLL_EXPORTED
60
#endif
67
#endif
61
 
68
 
62
#define STATIC static
69
#define STATIC static
63
 
70
 
-
 
71
/* This needs to be consistent with libgnuintl.h.in.  */
-
 
72
#if defined __NetBSD__ || defined __CYGWIN__ || defined __MINGW32__
-
 
73
/* Don't break __attribute__((format(printf,M,N))).
-
 
74
   This redefinition is only possible because the libc in NetBSD, Cygwin,
-
 
75
   mingw does not have a function __printf__.  */
-
 
76
# define libintl_printf __printf__
-
 
77
#endif
-
 
78
 
64
/* Define auxiliary functions declared in "printf-args.h".  */
79
/* Define auxiliary functions declared in "printf-args.h".  */
65
#include "printf-args.c"
80
#include "printf-args.c"
66
 
81
 
67
/* Define auxiliary functions declared in "printf-parse.h".  */
82
/* Define auxiliary functions declared in "printf-parse.h".  */
68
#include "printf-parse.c"
83
#include "printf-parse.c"
Line 86... Line 101...
86
      size_t length;
101
      size_t length;
87
      char *result = libintl_vasnprintf (NULL, &length, format, args);
102
      char *result = libintl_vasnprintf (NULL, &length, format, args);
88
      int retval = -1;
103
      int retval = -1;
89
      if (result != NULL)
104
      if (result != NULL)
90
	{
105
	{
91
	  if (fwrite (result, 1, length, stream) == length)
106
	  size_t written = fwrite (result, 1, length, stream);
92
	    retval = length;
-
 
93
	  free (result);
107
	  free (result);
-
 
108
	  if (written == length)
-
 
109
	    {
-
 
110
	      if (length > INT_MAX)
-
 
111
		errno = EOVERFLOW;
-
 
112
	      else
-
 
113
		retval = length;
-
 
114
	    }
94
	}
115
	}
95
      return retval;
116
      return retval;
96
    }
117
    }
97
}
118
}
98
 
119
 
Line 142... Line 163...
142
      if (result != resultbuf)
163
      if (result != resultbuf)
143
	{
164
	{
144
	  free (result);
165
	  free (result);
145
	  return -1;
166
	  return -1;
146
	}
167
	}
-
 
168
      if (length > INT_MAX)
-
 
169
	{
-
 
170
	  errno = EOVERFLOW;
-
 
171
	  return -1;
-
 
172
	}
147
      else
173
      else
148
	return length;
174
	return length;
149
    }
175
    }
150
}
176
}
151
 
177
 
Line 184... Line 210...
184
      char *result = libintl_vasnprintf (resultbuf, &length, format, args);
210
      char *result = libintl_vasnprintf (resultbuf, &length, format, args);
185
      if (result != resultbuf)
211
      if (result != resultbuf)
186
	{
212
	{
187
	  if (maxlength > 0)
213
	  if (maxlength > 0)
188
	    {
214
	    {
189
	      if (length < maxlength)
215
	      size_t pruned_length =
190
		abort ();
216
		(length < maxlength ? length : maxlength - 1);
191
	      memcpy (resultbuf, result, maxlength - 1);
217
	      memcpy (resultbuf, result, pruned_length);
192
	      resultbuf[maxlength - 1] = '\0';
218
	      resultbuf[pruned_length] = '\0';
193
	    }
219
	    }
194
	  free (result);
220
	  free (result);
-
 
221
	}
-
 
222
      if (length > INT_MAX)
-
 
223
	{
-
 
224
	  errno = EOVERFLOW;
195
	  return -1;
225
	  return -1;
196
	}
226
	}
197
      else
227
      else
198
	return length;
228
	return length;
199
    }
229
    }
Line 222... Line 252...
222
{
252
{
223
  size_t length;
253
  size_t length;
224
  char *result = libintl_vasnprintf (NULL, &length, format, args);
254
  char *result = libintl_vasnprintf (NULL, &length, format, args);
225
  if (result == NULL)
255
  if (result == NULL)
226
    return -1;
256
    return -1;
-
 
257
  if (length > INT_MAX)
-
 
258
    {
-
 
259
      free (result);
-
 
260
      errno = EOVERFLOW;
-
 
261
      return -1;
-
 
262
    }
227
  *resultp = result;
263
  *resultp = result;
228
  return length;
264
  return length;
229
}
265
}
230
 
266
 
231
DLL_EXPORTED
267
DLL_EXPORTED
Line 283... Line 319...
283
	{
319
	{
284
	  size_t i;
320
	  size_t i;
285
	  for (i = 0; i < length; i++)
321
	  for (i = 0; i < length; i++)
286
	    if (fputwc (result[i], stream) == WEOF)
322
	    if (fputwc (result[i], stream) == WEOF)
287
	      break;
323
	      break;
288
	  if (i == length)
-
 
289
	    retval = length;
-
 
290
	  free (result);
324
	  free (result);
-
 
325
	  if (i == length)
-
 
326
	    {
-
 
327
	      if (length > INT_MAX)
-
 
328
		errno = EOVERFLOW;
-
 
329
	      else
-
 
330
		retval = length;
-
 
331
	    }
291
	}
332
	}
292
      return retval;
333
      return retval;
293
    }
334
    }
294
}
335
}
295
 
336
 
Line 338... Line 379...
338
      wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
379
      wchar_t *result = libintl_vasnwprintf (resultbuf, &length, format, args);
339
      if (result != resultbuf)
380
      if (result != resultbuf)
340
	{
381
	{
341
	  if (maxlength > 0)
382
	  if (maxlength > 0)
342
	    {
383
	    {
343
	      if (length < maxlength)
384
	      size_t pruned_length =
344
		abort ();
385
		(length < maxlength ? length : maxlength - 1);
345
	      memcpy (resultbuf, result, (maxlength - 1) * sizeof (wchar_t));
386
	      memcpy (resultbuf, result, pruned_length * sizeof (wchar_t));
346
	      resultbuf[maxlength - 1] = 0;
387
	      resultbuf[pruned_length] = 0;
347
	    }
388
	    }
348
	  free (result);
389
	  free (result);
-
 
390
	  /* Unlike vsnprintf, which has to return the number of character that
-
 
391
	     would have been produced if the resultbuf had been sufficiently
-
 
392
	     large, the vswprintf function has to return a negative value if
-
 
393
	     the resultbuf was not sufficiently large.  */
-
 
394
	  if (length >= maxlength)
-
 
395
	    return -1;
-
 
396
	}
-
 
397
      if (length > INT_MAX)
-
 
398
	{
-
 
399
	  errno = EOVERFLOW;
349
	  return -1;
400
	  return -1;
350
	}
401
	}
351
      else
402
      else
352
	return length;
403
	return length;
353
    }
404
    }