maint: update copyright
[gnulib.git] / tests / test-fprintf-posix2.c
1 /* Test of POSIX compatible fprintf() function.
2    Copyright (C) 2007, 2009-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22
23 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
24
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <sys/resource.h>
29 #include <string.h>
30 #include <errno.h>
31
32 int
33 main (int argc, char *argv[])
34 {
35   struct rlimit limit;
36   int arg;
37   int ret;
38
39   /* Some printf implementations allocate temporary space with malloc.  */
40   /* On BSD systems, malloc() is limited by RLIMIT_DATA.  */
41 #ifdef RLIMIT_DATA
42   if (getrlimit (RLIMIT_DATA, &limit) < 0)
43     return 77;
44   if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
45     limit.rlim_max = 5000000;
46   limit.rlim_cur = limit.rlim_max;
47   if (setrlimit (RLIMIT_DATA, &limit) < 0)
48     return 77;
49 #endif
50   /* On Linux systems, malloc() is limited by RLIMIT_AS.  */
51 #ifdef RLIMIT_AS
52   if (getrlimit (RLIMIT_AS, &limit) < 0)
53     return 77;
54   if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
55     limit.rlim_max = 5000000;
56   limit.rlim_cur = limit.rlim_max;
57   if (setrlimit (RLIMIT_AS, &limit) < 0)
58     return 77;
59 #endif
60   /* Some printf implementations allocate temporary space on the stack.  */
61 #ifdef RLIMIT_STACK
62   if (getrlimit (RLIMIT_STACK, &limit) < 0)
63     return 77;
64   if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > 5000000)
65     limit.rlim_max = 5000000;
66   limit.rlim_cur = limit.rlim_max;
67   if (setrlimit (RLIMIT_STACK, &limit) < 0)
68     return 77;
69 #endif
70
71   arg = atoi (argv[1]);
72   switch (arg)
73     {
74     case 0:
75       {
76         void *memory = malloc (5000000);
77         if (memory == NULL)
78           return 1;
79         memset (memory, 17, 5000000);
80         return 78;
81       }
82     case 1:
83       ret = fprintf (stdout, "%.5000000f", 1.0);
84       return !(ret == 5000002 || (ret < 0 && errno == ENOMEM));
85     case 2:
86       ret = fprintf (stdout, "%.5000000f", -1.0);
87       return !(ret == 5000003 || (ret < 0 && errno == ENOMEM));
88     case 3:
89       ret = fprintf (stdout, "%.5000000e", 1.0);
90       return !(ret >= 5000006 || (ret < 0 && errno == ENOMEM));
91     case 4:
92       ret = fprintf (stdout, "%.5000000d", 1);
93       return !(ret == 5000000 || (ret < 0 && errno == ENOMEM));
94     case 5:
95       ret = fprintf (stdout, "%.5000000d", -1);
96       return !(ret == 5000001 || (ret < 0 && errno == ENOMEM));
97     case 6:
98       ret = fprintf (stdout, "%.5000000u", 1);
99       return !(ret == 5000000 || (ret < 0 && errno == ENOMEM));
100     }
101   return 0;
102 }
103
104 #else
105
106 int
107 main (int argc, char *argv[])
108 {
109   return 77;
110 }
111
112 #endif