| 1 | /* $Id: iterate.c 405 2003-07-17 01:19:17Z bird $ | 
|---|
| 2 | * | 
|---|
| 3 | * Iterate a program for a number of seconds. | 
|---|
| 4 | * | 
|---|
| 5 | * InnoTek Systemberatung GmbH confidential | 
|---|
| 6 | * | 
|---|
| 7 | * Copyright (c) 2003 InnoTek Systemberatung GmbH | 
|---|
| 8 | * Author: knut st. osmundsen <bird-srcspam@anduin.net> | 
|---|
| 9 | * | 
|---|
| 10 | * All Rights Reserved | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | #define INCL_BASE | 
|---|
| 15 | #include <os2.h> | 
|---|
| 16 | #include <string.h> | 
|---|
| 17 | #include <stdio.h> | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | static int syntax(const char *argv0) | 
|---|
| 21 | { | 
|---|
| 22 | fprintf(stderr, | 
|---|
| 23 | "syntax: %s <seconds> <prog> [arg]\n" | 
|---|
| 24 | "\n" | 
|---|
| 25 | "Iterated execution of <prog> for <seconds> reporting the number of iterations.\n", | 
|---|
| 26 | argv0); | 
|---|
| 27 | return 8; | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 | int main(int argc, char const * const *argv) | 
|---|
| 32 | { | 
|---|
| 33 | APIRET      rc; | 
|---|
| 34 | unsigned    cSeconds; | 
|---|
| 35 | PPIB        ppib; | 
|---|
| 36 | PTIB        ptib; | 
|---|
| 37 | char *      psz; | 
|---|
| 38 | char *      pszEnd; | 
|---|
| 39 | char        szArgs[8192]; | 
|---|
| 40 | ULONG       ulEnd; | 
|---|
| 41 | ULONG       ul; | 
|---|
| 42 | unsigned    cIterations; | 
|---|
| 43 |  | 
|---|
| 44 | /* Enough arguments? */ | 
|---|
| 45 | if (argc <= 2) | 
|---|
| 46 | return syntax(argv[0]); | 
|---|
| 47 |  | 
|---|
| 48 | /* It's easier to use the OS/2 facilities. */ | 
|---|
| 49 | DosGetInfoBlocks(&ptib, &ppib); | 
|---|
| 50 | psz = ppib->pib_pchcmd; | 
|---|
| 51 |  | 
|---|
| 52 | /* Skip the prog name and traditinal blanks before first arg */ | 
|---|
| 53 | psz += strlen(psz) + 1; | 
|---|
| 54 | while (*psz == ' ' || *psz == '\t') | 
|---|
| 55 | psz++; | 
|---|
| 56 |  | 
|---|
| 57 | /* Seconds. */ | 
|---|
| 58 | cSeconds = strtoul(psz, &pszEnd, 0); | 
|---|
| 59 | if (pszEnd <= psz || cSeconds <= 0) | 
|---|
| 60 | { | 
|---|
| 61 | fprintf(stderr, "error: would you be so good as give me a decent argument?\n"); | 
|---|
| 62 | return 8; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | /* Skip the spaces. */ | 
|---|
| 66 | psz = pszEnd; | 
|---|
| 67 | while (*psz == ' ' || *psz == '\t') | 
|---|
| 68 | psz++; | 
|---|
| 69 |  | 
|---|
| 70 | /* Find the program name to execute - first word. (forget quoting) */ | 
|---|
| 71 | psz = strcpy(szArgs, psz); | 
|---|
| 72 | while (*psz != ' ' && *psz != '\0') | 
|---|
| 73 | psz++; | 
|---|
| 74 | *psz++ = '\0'; | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /* | 
|---|
| 78 | * Now iterate! | 
|---|
| 79 | */ | 
|---|
| 80 | printf("Iterating '%s' for %d seconds...\n", szArgs, cSeconds); | 
|---|
| 81 | fflush(stdout); | 
|---|
| 82 |  | 
|---|
| 83 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulEnd, sizeof(ulEnd)); | 
|---|
| 84 | ul = ulEnd; | 
|---|
| 85 | ulEnd += 1000 * cSeconds; | 
|---|
| 86 | while (ul < ulEnd) | 
|---|
| 87 | { | 
|---|
| 88 | RESULTCODES res; | 
|---|
| 89 | rc = DosExecPgm(NULL, 0, EXEC_SYNC, szArgs, NULL, &res, szArgs); | 
|---|
| 90 | if (rc) | 
|---|
| 91 | break; | 
|---|
| 92 | cIterations++; | 
|---|
| 93 | /* check the time */ | 
|---|
| 94 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ul, sizeof(ul)); | 
|---|
| 95 | } | 
|---|
| 96 | if (rc) | 
|---|
| 97 | printf("error: DosExecPgm failed with rc=%d.\n", rc); | 
|---|
| 98 | printf("Executed %d iterations.\n", cIterations); | 
|---|
| 99 |  | 
|---|
| 100 | return rc; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|