added updatedb.c and argmax.c.
[findutils.git] / xargs / argmax.c
blob7a8160c20d901ad786e0217d4b128316fe5d0f7f
1 /*
2 * TODO:
3 * * should probably use execve() for portability
4 */
6 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/wait.h>
10 #include <unistd.h>
11 #include <stdlib.h> /* malloc */
12 #include <limits.h>
13 #include <errno.h>
16 #define STEPS_DEFAULT 10
17 #define SAFETY_MARGIN 2048
18 #define TEST_PROGRAM "/bin/true"
23 * ``It is implementation‐defined whether null terminators, pointers, and/or any
24 * alignment bytes are included in this total.''
25 * -- IEEE Std 1003.1‐2003 on ARG_MAX
27 * http://www.in-ulm.de/~mascheck/various/argmax/
31 #define FUZZ 4
33 #include <string.h> /* temp: strnlen */
35 static int test_maxsize_binary()
37 const long sys_arg_max = sysconf(_SC_ARG_MAX);
38 char* testarg = malloc(sys_arg_max);
39 int n = (sys_arg_max-1)/2;
40 int i;
42 for (i=sys_arg_max; i>=0; --i)
43 testarg[i] = 1;
44 testarg[n] = 0;
46 i=0;
48 //printf("ARG_MAX: %d\n", sys_arg_max);
49 //printf("testarg@%p\n", testarg);
51 while (n > 0)
53 printf("strlen: %d\n", strnlen(testarg, sys_arg_max));
54 ++i;
56 if (fork() == 0)
58 /* child */
59 execl("/bin/true", testarg, (char*)NULL);
61 exit(errno);
63 else
65 /* parent */
66 int status;
68 wait(&status);
70 switch (WEXITSTATUS(status))
72 case 0: /* worked */
73 printf("current fuzz: %d\n", n*0.5);
74 if (n*0.5 < FUZZ)
76 printf("%d iterations\n", i);
77 return(n);
79 printf("worked, n=%d/%f\n", n, n*1.5);
80 testarg[n] = 1;
81 #define MIN(x,y) (x<y?x:y)
82 n = MIN(1.5*n, sys_arg_max-1);
83 testarg[n] = 0;
84 break;
86 case E2BIG: /* argument list too long */
87 printf("E2BIG, n=%d/%d\n", n, n*0.5);
88 n *= 0.5;
89 testarg[n] = 0;
90 break;
92 default: /* other error */
93 fprintf(stderr, "WARNING: child encountered unexpected error: %s\n",
94 strerror(WEXITSTATUS(status)));
99 return 0; /* NEVER REACHED */
102 static int test_maxsize_linear()
104 const long sys_arg_max = sysconf(_SC_ARG_MAX);
105 char* testarg = malloc(sys_arg_max);
106 int n = sys_arg_max-1;
107 int i;
109 for (i=n; i>=0; --i)
110 testarg[i] = 1;
111 testarg[n] = 0;
113 while (n > 0)
115 if (fork() == 0)
117 /* child */
118 execl("/bin/true", testarg, (char*)NULL);
120 exit(errno);
122 else
124 /* parent */
125 int status;
127 wait(&status);
129 switch (WEXITSTATUS(status))
131 case 0: /* worked */
132 printf("%d iterations\n", sys_arg_max-n);
133 return(n);
134 break;
136 case E2BIG: /* argument list too long */
137 testarg[--n] = 0;
138 break;
140 default: /* other error */
141 fprintf(stderr, "WARNING: child encountered unexpected error: %s\n",
142 strerror(WEXITSTATUS(status)));
147 return 0; /* NEVER REACHED */
151 /* binary search */
152 static int
153 argmax_binary(int steps)
155 if (steps == 0)
156 steps = STEPS_DEFAULT;
158 return 0;
162 static int
163 argmax_calc()
165 return 0;
169 static int
170 argnum_binary()
172 return 0;
177 main(int ac, char** av, char** env)
179 // printf("maximum size of args (linear search): %d\n", test_maxsize_linear());
180 printf("maximum size of args (binary search): %d\n", test_maxsize_binary());
181 //printf("maximum size of args: %d\n", test_maxsize_linear());
182 //printf("maximum number of args: %d\n", test_maxnum_linear());
183 return 0;