Fix the creation of the dumpdir directory in stress_floppy Makefile
[ltp-debian.git] / lib / parse_opts.c
blob3bc304cc3887ef9b4ac2d8c4e2c9ffe9430edd30
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /* $Id: parse_opts.c,v 1.14 2009/08/28 09:59:52 vapier Exp $ */
35 /**********************************************************
37 * OS Testing - Silicon Graphics, Inc.
39 * FUNCTION NAME : parse_opts
41 * FUNCTION TITLE : parse standard & user options for system call tests
43 * SYNOPSIS:
44 * #include "usctest.h"
46 * char *parse_opts(ac, av, user_optarr, uhf)
47 * int ac;
48 * char **av;
49 * option_t user_optarr[];
50 * void (*uhf)();
52 * AUTHOR : William Roske/Richard Logan
54 * INITIAL RELEASE : UNICOS 7.0
56 * DESCRIPTION
57 * The parse_opts library routine takes that argc and argv parameters
58 * recevied by main() and an array of structures defining user options.
59 * It parses the command line setting flag and argument locations
60 * associated with the options. It uses getopt to do the actual cmd line
61 * parsing. uhf() is a function to print user define help
63 * This module contains the functions usc_global_setup_hook and
64 * usc_test_looping, which are called by marcos defined in usctest.h.
66 * RETURN VALUE
67 * parse_opts returns a pointer to an error message if an error occurs.
68 * This pointer is (char *)NULL if parsing is successful.
70 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
71 #include <errno.h>
72 #include <stdlib.h>
73 #include <string.h>
74 #include <sys/param.h>
75 #include <sys/signal.h>
76 #include <sys/types.h>
77 #include <unistd.h>
78 #include <sys/time.h>
81 #if UNIT_TEST
82 #include <time.h>
83 #endif /* UNIT_TEST */
85 #include "test.h"
86 #define _USC_LIB_ 1 /* indicates we are the library to the usctest.h include */
87 #include "usctest.h"
89 #ifndef USC_COPIES
90 #define USC_COPIES "USC_COPIES"
91 #endif
93 #ifndef UNIT_TEST
94 #define UNIT_TEST 0
95 #endif
97 #ifndef DEBUG
98 #define DEBUG 0
99 #endif
101 /* The timing information block. */
102 struct tblock tblock={0,((long) -1)>>1,0,0};
105 /* Define flags and args for standard options */
106 int STD_FUNCTIONAL_TEST=1, /* flag indicating to do functional testing code */
107 STD_TIMING_ON=0, /* flag indicating to print timing stats */
108 STD_PAUSE=0, /* flag indicating to pause before actual start, */
109 /* for contention mode */
110 STD_INFINITE=0, /* flag indciating to loop forever */
111 STD_LOOP_COUNT=1, /* number of iterations */
112 STD_COPIES=1, /* number of copies */
113 STD_ERRNO_LOG=0; /* flag indicating to do errno logging */
115 float STD_LOOP_DURATION=0.0, /* duration value in fractional seconds */
116 STD_LOOP_DELAY=0.0; /* loop delay value in fractional seconds */
119 char **STD_opt_arr = NULL; /* array of option strings */
120 int STD_nopts=0, /* number of elements in STD_opt_arr */
121 STD_argind=1; /* argv index to next argv element */
122 /* (first argument) */
123 /* To getopt users, it is like optind */
126 * The following variables are to support system testing additions.
128 static int STD_TP_barrier=0; /* flag to do barrier in TEST_PAUSE */
129 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
130 static int STD_LP_barrier=0; /* flag to do barrier in TEST_LOOPING */
131 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
132 static int STD_TP_shmem_sz=0; /* shmalloc this many words per pe in TEST_PAUSE */
133 static int STD_LD_shmem=0; /* flag to do shmem_puts and shmem_gets during delay */
134 static int STD_LP_shmem=0; /* flag to do shmem_puts and gets during TEST_LOOPING */
135 static int STD_LD_recfun=0; /* do recressive function calls in loop delay */
136 static int STD_LP_recfun=0; /* do recressive function calls in TEST_LOOPING */
137 static int STD_TP_sbrk=0; /* do sbrk in TEST_PAUSE */
138 static int STD_LP_sbrk=0; /* do sbrk in TEST_LOOPING */
139 static char *STD_start_break=0; /* original sbrk size */
140 static int Debug=0;
142 struct std_option_t {
143 char *optstr;
144 char *help;
145 char *flag;
146 char **arg;
147 } std_options[] = {
148 { "c:", " -c n Run n copies concurrently\n", NULL, NULL},
149 { "e" , " -e Turn on errno logging\n", NULL, NULL},
150 { "f" , " -f Turn off functional testing\n", NULL, NULL},
151 { "h" , " -h Show this help screen\n", NULL, NULL},
152 { "i:", " -i n Execute test n times\n", NULL, NULL},
153 { "I:", " -I x Execute test for x seconds\n", NULL, NULL},
154 { "p" , " -p Pause for SIGUSR1 before starting\n", NULL, NULL},
155 { "P:", " -P x Pause for x seconds between iterations\n", NULL, NULL},
156 { "t" , " -t Turn on syscall timing\n", NULL, NULL},
157 #ifdef UCLINUX
158 { "C:", " -C ARG Run the child process with arguments ARG (for internal use)\n",
159 NULL, NULL},
160 #endif
161 {NULL, NULL, NULL, NULL}};
163 void print_help(void (*user_help)());
166 * Structure for usc_recressive_func argument
168 struct usc_bigstack_t {
169 char space[4096];
172 static struct usc_bigstack_t *STD_bigstack=NULL;
175 * Counter of errnos returned (-e option). Indexed by errno.
176 * Make the array USC_MAX_ERRNO long. That is the first Fortran
177 * Lib errno. No syscall should return an errno that high.
179 int STD_ERRNO_LIST[USC_MAX_ERRNO];
181 /* define the string length for Mesg and Mesg2 strings */
182 #define STRLEN 2048
184 static char Mesg2[STRLEN]; /* holds possible return string */
185 static void usc_recressive_func();
188 * Define bits for options that might have env variable default
190 #define OPT_iteration 01
191 #define OPT_nofunccheck 02
192 #define OPT_duration 04
193 #define OPT_delay 010
194 #define OPT_copies 020
196 #ifdef UCLINUX
197 /* Allocated and used in self_exec.c: */
198 extern char *child_args; /* Arguments to child when -C is used */
199 #endif
201 /**********************************************************************
202 * parse_opts:
203 **********************************************************************/
204 char *
205 parse_opts(int ac, char **av, option_t *user_optarr, void (*uhf)())
207 int found; /* flag to indicate that an option specified was */
208 /* found in the user's list */
209 int k; /* scratch integer for returns and short time usage */
210 float ftmp; /* tmp float for parsing env variables */
211 char *ptr; /* used in getting env variables */
212 int options=0; /* no options specified */
213 int optstrlen, i;
214 char *optionstr;
215 int opt; /* return of getopt */
218 * If not the first time this function is called, release the old STD_opt_arr
219 * vector.
222 if ( STD_opt_arr != NULL ) {
223 free(STD_opt_arr);
224 STD_opt_arr=NULL;
226 /* Calculate how much space we need for the option string */
227 optstrlen = 0;
228 for (i = 0; std_options[i].optstr; ++i)
229 optstrlen += strlen(std_options[i].optstr);
230 if (user_optarr)
231 for (i = 0; user_optarr[i].option; ++i) {
232 if (strlen(user_optarr[i].option) > 2)
233 return "parse_opts: ERROR - Only short options are allowed";
234 optstrlen += strlen(user_optarr[i].option);
236 optstrlen += 1;
238 /* Create the option string for getopt */
239 optionstr = (char *)malloc(optstrlen);
240 if (!optionstr)
241 return "parse_opts: ERROR - Could not allocate memory for optionstr";
243 optionstr[0] = '\0';
245 for (i = 0; std_options[i].optstr; ++i)
246 strcat(optionstr, std_options[i].optstr);
247 if (user_optarr)
248 for (i = 0; user_optarr[i].option; ++i)
249 /* only add the option if it wasn't there already */
250 if (strchr(optionstr, user_optarr[i].option[0]) == NULL)
251 strcat(optionstr, user_optarr[i].option);
253 #if DEBUG > 1
254 printf("STD_nopts = %d\n", STD_nopts);
255 #endif
258 * Loop through av parsing options.
260 while ( (opt = getopt(ac, av, optionstr)) > 0) {
262 STD_argind = optind;
263 #if DEBUG > 0
264 printf("parse_opts: getopt returned '%c'\n", opt);
265 #endif
267 switch (opt) {
268 case '?': /* Unknown option */
269 return "Unknown option";
270 break;
271 case ':': /* Missing Arg */
272 return "Missing argument";
273 break;
274 case 'i': /* Iterations */
275 options |= OPT_iteration;
276 STD_LOOP_COUNT = atoi(optarg);
277 if (STD_LOOP_COUNT == 0) STD_INFINITE = 1;
278 break;
279 case 'P': /* Delay between iterations */
280 options |= OPT_delay;
281 STD_LOOP_DELAY = atof(optarg);
282 break;
283 case 'I': /* Time duration */
284 options |= OPT_duration;
285 STD_LOOP_DURATION = atof(optarg);
286 if ( STD_LOOP_DURATION == 0.0 ) STD_INFINITE=1;
287 break;
288 case 'c': /* Copies */
289 options |= OPT_copies;
290 STD_COPIES = atoi(optarg);
291 break;
292 case 'f': /* Functional testing */
293 STD_FUNCTIONAL_TEST = 0;
294 break;
295 case 'p': /* Pause for SIGUSR1 */
296 STD_PAUSE = 1;
297 break;
298 case 't': /* syscall timing */
299 STD_TIMING_ON = 1;
300 break;
301 case 'e': /* errno loggin */
302 STD_ERRNO_LOG = 1;
303 break;
304 case 'h': /* Help */
305 print_help(uhf);
306 exit(0);
307 break;
308 #ifdef UCLINUX
309 case 'C': /* Run child */
310 child_args = optarg;
311 break;
312 #endif
313 default:
315 /* Check all the user specified options */
316 found=0;
317 for(i = 0; user_optarr[i].option; ++i) {
319 if (opt == user_optarr[i].option[0]) {
320 /* Yup, This is a user option, set the flag and look for argument */
321 if ( user_optarr[i].flag ) {
322 *user_optarr[i].flag=1;
324 found++;
326 /* save the argument at the user's location */
327 if ( user_optarr[i].option[strlen(user_optarr[i].option)-1] == ':' ) {
328 *user_optarr[i].arg=optarg;
330 break; /* option found - break out of the for loop */
333 /* This condition "should never happen". SO CHECK FOR IT!!!! */
334 if ( ! found ) {
335 sprintf(Mesg2,
336 "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL ERROR", opt);
337 return(Mesg2);
341 } /* end of while */
342 free(optionstr);
344 STD_argind = optind;
347 * Turn on debug
349 if ( (ptr=getenv("USC_DEBUG")) != NULL ) {
350 Debug=1;
351 printf("env USC_DEBUG is defined, turning on debug\n");
353 if ( (ptr=getenv("USC_VERBOSE")) != NULL ) {
354 Debug=1;
355 printf("env USC_VERBOSE is defined, turning on debug\n");
359 * If the USC_ITERATION_ENV environmental variable is set to
360 * a number, use that number as iteration count (same as -c option).
361 * The -c option with arg will be used even if this env var is set.
363 if ( !(options & OPT_iteration) && (ptr=getenv(USC_ITERATION_ENV)) != NULL ) {
364 if ( sscanf(ptr, "%i", &k) == 1) {
365 if ( k == 0 ) { /* if arg is 0, set infinite loop flag */
366 STD_INFINITE=1;
367 if ( Debug )
368 printf("Using env %s, set STD_INFINITE to 1\n",
369 USC_ITERATION_ENV);
370 } else { /* else, set the loop count to the arguement */
371 STD_LOOP_COUNT=k;
372 if ( Debug )
373 printf("Using env %s, set STD_LOOP_COUNT to %d\n",
374 USC_ITERATION_ENV, k);
380 * If the USC_NO_FUNC_CHECK environmental variable is set, we'll
381 * unset the STD_FUNCTIONAL_TEST variable.
383 if ( !(options & OPT_nofunccheck) && (ptr=getenv(USC_NO_FUNC_CHECK)) != NULL ) {
384 STD_FUNCTIONAL_TEST=0; /* Turn off functional testing */
385 if ( Debug )
386 printf("Using env %s, set STD_FUNCTIONAL_TEST to 0\n",
387 USC_NO_FUNC_CHECK);
391 * If the USC_LOOP_WALLTIME environmental variable is set,
392 * use that number as duration (same as -I option).
393 * The -I option with arg will be used even if this env var is set.
396 if ( !(options & OPT_duration) && (ptr=getenv(USC_LOOP_WALLTIME)) != NULL ) {
397 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
398 STD_LOOP_DURATION=ftmp;
399 if ( Debug )
400 printf("Using env %s, set STD_LOOP_DURATION to %f\n",
401 USC_LOOP_WALLTIME, ftmp);
402 if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */
403 STD_INFINITE=1;
404 if ( Debug )
405 printf("Using env %s, set STD_INFINITE to 1\n", USC_LOOP_WALLTIME);
409 if ( !(options & OPT_duration) && (ptr=getenv("USC_DURATION")) != NULL ) {
410 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
411 STD_LOOP_DURATION=ftmp;
412 if ( Debug )
413 printf("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n", ftmp);
414 if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */
415 STD_INFINITE=1;
416 if ( Debug )
417 printf("Using env USC_DURATION, set STD_INFINITE to 1\n");
422 * If the USC_LOOP_DELAY environmental variable is set,
423 * use that number as delay in factional seconds (same as -P option).
424 * The -P option with arg will be used even if this env var is set.
426 if ( !(options & OPT_delay) && (ptr=getenv(USC_LOOP_DELAY)) != NULL ) {
427 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
428 STD_LOOP_DELAY=ftmp;
429 if ( Debug )
430 printf("Using env %s, set STD_LOOP_DELAY = %f\n",
431 USC_LOOP_DELAY, ftmp);
436 * If the USC_COPIES environmental variable is set,
437 * use that number as copies (same as -c option).
438 * The -c option with arg will be used even if this env var is set.
440 if ( !(options & OPT_copies) && (ptr=getenv(USC_COPIES)) != NULL ) {
441 if ( sscanf(ptr, "%d", &STD_COPIES) == 1 && STD_COPIES >= 0 ) {
442 if ( Debug )
443 printf("Using env %s, set STD_COPIES = %d\n",
444 USC_COPIES, STD_COPIES);
449 * The following are special system testing envs to turn on special
450 * hooks in the code.
452 if ( (ptr=getenv("USC_TP_BARRIER")) != NULL ) {
453 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
454 STD_TP_barrier=k;
456 else
457 STD_TP_barrier=1;
458 if ( Debug )
459 printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
460 STD_TP_barrier);
463 if ( (ptr=getenv("USC_LP_BARRIER")) != NULL ) {
464 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
465 STD_LP_barrier=k;
467 else
468 STD_LP_barrier=1;
469 if ( Debug )
470 printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
471 STD_LP_barrier);
474 if ( (ptr=getenv("USC_TP_SHMEM")) != NULL ) {
475 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
476 STD_TP_shmem_sz=k;
477 if ( Debug )
478 printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
479 STD_TP_shmem_sz);
483 if ( (ptr=getenv("USC_LP_SHMEM")) != NULL ) {
484 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
485 STD_LP_shmem=k;
486 if ( Debug )
487 printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
488 STD_LP_shmem);
492 if ( (ptr=getenv("USC_LD_SHMEM")) != NULL ) {
493 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
494 STD_LD_shmem=k;
495 if ( Debug )
496 printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
497 STD_LD_shmem);
501 if ( (ptr=getenv("USC_TP_SBRK")) != NULL ) {
502 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
503 STD_TP_sbrk=k;
504 if ( Debug )
505 printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
506 STD_TP_sbrk);
510 #if !defined(UCLINUX)
511 if ( (ptr=getenv("USC_LP_SBRK")) != NULL ) {
512 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
513 STD_LP_sbrk=k;
514 if ( Debug )
515 printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
516 STD_LP_sbrk);
519 #endif /* if !defined(UCLINUX) */
521 if ( (ptr=getenv("USC_LP_RECFUN")) != NULL ) {
522 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
523 STD_LP_recfun=k;
524 if ( STD_bigstack != (struct usc_bigstack_t *)NULL )
525 STD_bigstack=(struct usc_bigstack_t *)
526 malloc(sizeof(struct usc_bigstack_t));
527 if ( Debug )
528 printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
529 STD_LP_recfun);
533 if ( (ptr=getenv("USC_LD_RECFUN")) != NULL ) {
534 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
535 STD_LD_recfun=k;
536 if ( STD_bigstack != (struct usc_bigstack_t *)NULL )
537 STD_bigstack=(struct usc_bigstack_t *)
538 malloc(sizeof(struct usc_bigstack_t));
539 if ( Debug )
540 printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
541 STD_LD_recfun);
545 #if UNIT_TEST
546 printf("The following variables after option and env parsing:\n");
547 printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST);
548 printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION);
549 printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY);
550 printf("STD_COPIES = %d\n", STD_COPIES);
551 printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT);
552 printf("STD_INFINITE = %d\n", STD_INFINITE);
553 printf("STD_TIMING_ON = %d\n", STD_TIMING_ON);
554 printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG);
555 printf("STD_PAUSE = %d\n", STD_PAUSE);
556 #endif
558 return((char *) NULL);
560 } /* end of parse_opts */
562 /*********************************************************************
563 * print_help() - print help message and user help message
564 *********************************************************************/
565 void print_help(void (*user_help)())
567 STD_opts_help();
569 if (user_help) user_help();
572 /*********************************************************************
573 * STD_opts_help() - return a help string for the STD_OPTIONS.
574 *********************************************************************/
575 void
576 STD_opts_help()
578 int i;
580 for(i = 0; std_options[i].optstr; ++i) {
581 if (std_options[i].help)
582 printf("%s", std_options[i].help);
587 * routine to goto when we get the SIGUSR1 for STD_PAUSE
589 void STD_go(int sig)
591 return;
594 /***********************************************************************
595 * This function will do desired end of global setup test
596 * hooks.
597 * Currently it will only do a pause waiting for sigusr1 if
598 * STD_PAUSE is set.
600 ***********************************************************************/
602 usc_global_setup_hook()
604 #ifndef UCLINUX
605 int cnt;
606 /* temp variable to store old signal action to be restored after pause */
607 int (*_TMP_FUNC)(void);
610 * Fork STD_COPIES-1 copies.
612 for(cnt=1;cnt<STD_COPIES;cnt++) {
613 switch(fork() ) {
614 case -1:
615 fprintf(stderr, "%s: fork() failed, errno:%d %s\n",
616 __FILE__, errno, strerror(errno));
617 break;
618 case 0: /* child */
619 cnt=STD_COPIES; /* to stop the forking */
620 break;
622 default: /* parent */
623 break;
628 * pause waiting for sigusr1.
630 if ( STD_PAUSE ) {
631 _TMP_FUNC = (int (*)())signal(SIGUSR1, STD_go);
632 pause();
633 signal(SIGUSR1, (void (*)())_TMP_FUNC);
636 #if !defined(UCLINUX)
638 if ( STD_TP_sbrk || STD_LP_sbrk) {
639 STD_start_break=sbrk(0); /* get original sbreak size */
642 if ( STD_TP_sbrk ) {
643 sbrk(STD_TP_sbrk);
644 if ( Debug )
645 printf("after sbrk(%d)\n", STD_TP_sbrk);
648 #endif /* if !defined(UCLINUX) */
649 #endif
650 return 0;
653 #define USECS_PER_SEC 1000000 /* microseconds per second */
655 /***********************************************************************
656 * This function returns the number of get_current_time()'s return
657 * per second.
658 ***********************************************************************/
660 static int
661 get_timepersec()
663 return USECS_PER_SEC; /* microseconds per second */
667 /***********************************************************************
668 * this function will get current time in microseconds since 1970.
669 ***********************************************************************/
670 static int
671 get_current_time()
673 struct timeval curtime;
675 gettimeofday(&curtime, NULL);
677 /* microseconds since 1970 */
678 return (curtime.tv_sec*USECS_PER_SEC) + curtime.tv_usec;
683 /***********************************************************************
685 * This function will determine if test should continue iterating
686 * If the STD_INFINITE flag is set, return 1.
687 * If the STD_LOOP_COUNT variable is set, compare it against
688 * the counter.
689 * If the STD_LOOP_DURATION variable is set, compare current time against
690 * calculated stop_time.
691 * This function will return 1 until all desired looping methods
692 * have been met.
694 * counter integer is supplied by the user program.
695 ***********************************************************************/
697 usc_test_looping(counter)
698 int counter;
700 static int first_time = 1;
701 static int stop_time = 0; /* stop time in rtc or usecs */
702 static int delay; /* delay in clocks or usecs */
703 int hertz=0; /* clocks per second or usecs per second */
704 int ct, end; /* current time, end delay time */
705 int keepgoing=0; /* used to determine return value */
708 * If this is the first iteration and we are looping for
709 * duration of STD_LOOP_DURATION seconds (fractional) or
710 * doing loop delays, get the clocks per second.
712 if ( first_time ) {
714 first_time=0;
715 if ( STD_LOOP_DELAY || STD_LOOP_DURATION ) {
716 hertz = get_timepersec();
720 * If looping for duration, calculate stop time in
721 * clocks.
724 if ( STD_LOOP_DURATION) {
725 ct=get_current_time();
726 stop_time=(int)((float)hertz * STD_LOOP_DURATION) + ct;
730 * If doing delay each iteration, calcuate the number
731 * of clocks for each delay.
733 if ( STD_LOOP_DELAY ) {
734 delay=(int)((float)hertz * STD_LOOP_DELAY);
740 * if delay each iteration, loop for delay clocks.
741 * This will not be done on first iteration.
742 * The delay will happen before determining if
743 * there will be another iteration.
745 else if ( STD_LOOP_DELAY ) {
746 ct=get_current_time();
747 end=ct+delay;
748 while ( ct < end ) {
750 * The following are special test hooks in the delay loop.
752 if ( STD_LD_recfun ) {
753 if ( Debug )
754 printf("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
755 STD_LD_recfun);
756 usc_recressive_func(0, STD_LD_recfun, *STD_bigstack);
759 ct=get_current_time();
763 if ( STD_INFINITE ) {
764 keepgoing++;
767 if ( STD_LOOP_COUNT && counter < STD_LOOP_COUNT ) {
768 keepgoing++;
771 if ( STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time ) {
772 keepgoing++;
775 if ( keepgoing == 0 )
776 return 0;
779 * The following code allows special system testing hooks.
782 if ( STD_LP_recfun ) {
783 if ( Debug )
784 printf("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
785 STD_LP_recfun);
786 usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
789 #if !defined(UCLINUX)
791 if ( STD_LP_sbrk ) {
792 if ( Debug )
793 printf("about to do sbrk(%d)\n", STD_LP_sbrk);
794 sbrk(STD_LP_sbrk);
796 #endif
799 if ( keepgoing )
800 return 1;
801 else
802 return 0; /* done - stop iterating */
807 * This function recressively calls itself max times.
809 static void
810 usc_recressive_func(cnt, max, bstack)
811 int cnt;
812 int max;
813 struct usc_bigstack_t bstack;
815 if ( cnt < max )
816 usc_recressive_func(cnt+1, max, bstack);
820 #if UNIT_TEST
821 /******************************************************************************
822 * UNIT TEST CODE
823 * UNIT TEST CODE
825 * this following code is provide so that unit testing can
826 * be done fairly easily.
827 ******************************************************************************/
829 int Help = 0;
830 int Help2 = 0;
831 char *ptr;
834 * Code from usctest.h that not part of this file since we are the library.
837 struct usc_errno_t TEST_VALID_ENO[USC_MAX_ERRNO];
839 /***********************************************************************
840 * Globals for returning the return code and errno from the system call
841 * test macros.
842 ***********************************************************************/
843 int TEST_RETURN;
844 int TEST_ERRNO;
846 /* for test specific parse_opts options */
847 option_t Options[] = {
848 { "help", &Help2, NULL }, /* -help option */
849 { "h", &Help, NULL }, /* -h option */
850 { TIMING, NULL, NULL}, /* disable -timing option */
852 #if INVALID_TEST_CASES
853 { "missingflag", NULL, &ptr }, /* error */
854 { "missingarg:", &Help, NULL }, /* error */
855 #endif /* INVALID_TEST_CASES */
857 { NULL, NULL, NULL }
861 main(argc, argv)
862 int argc;
863 char **argv;
865 int lc;
866 char *msg;
867 struct timeval t;
868 int cnt;
870 if ( (msg=parse_opts(argc, argv,
871 (option_t *) Options)) != (char *) NULL ) {
872 printf("ERROR : %s\n", msg);
873 exit(1);
876 TEST_PAUSE;
878 for (lc=0; TEST_LOOPING(lc); lc++) {
880 TEST( gettimeofday(&t, NULL) );
881 printf("iter=%d: sec:%d, usec:%6.6d %s", lc+1, t.tv_sec,
882 t.tv_usec, ctime(&t.tv_sec));
886 TEST_CLEANUP;
888 exit(0);
891 #endif /* UNIT_TEST */