1 /* 2 * Copyright 2007, Axel Dörfler, axeld@pinc-software.de. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Copyright 2002, Manuel J. Petit. All rights reserved. 6 * Distributed under the terms of the NewOS License. 7 */ 8 9 10 #include <image.h> 11 12 #include <string.h> 13 #include <stdlib.h> 14 #include <stdio.h> 15 16 17 static void 18 usage(char const *app) 19 { 20 printf("usage: %s [-s] ###\n", app); 21 exit(-1); 22 } 23 24 25 int 26 main(int argc, char *argv[]) 27 { 28 bool silent = false; 29 int num = 0; 30 int result; 31 32 switch (argc) { 33 case 2: 34 num = atoi(argv[1]); 35 break; 36 case 3: 37 if (!strcmp(argv[1], "-s")) { 38 num = atoi(argv[2]); 39 silent = true; 40 break; 41 } 42 // supposed to fall through 43 44 default: 45 usage(argv[0]); 46 break; 47 } 48 49 if (num < 2) { 50 result = num; 51 } else { 52 char buffer[64]; 53 char* args[]= { argv[0], "-s", buffer, NULL }; 54 int argCount = 3; 55 status_t status; 56 57 snprintf(buffer, sizeof(buffer), "%d", num - 1); 58 thread_id threadA = load_image(argCount, (const char**)args, (const char**)environ); 59 if (threadA < B_OK) { 60 fprintf(stderr, "Could not create thread A: %s\n", strerror(threadA)); 61 return -1; 62 } 63 64 snprintf(buffer, sizeof(buffer), "%d", num - 2); 65 thread_id threadB = load_image(argCount, (const char**)args, (const char**)environ); 66 if (threadB < B_OK) { 67 fprintf(stderr, "Could not create thread B: %s\n", strerror(threadB)); 68 return -1; 69 } 70 71 resume_thread(threadA); 72 resume_thread(threadB); 73 74 status_t returnValue = 0; 75 do { 76 status = wait_for_thread(threadA, &returnValue); 77 } while (status == B_INTERRUPTED); 78 79 if (status == B_OK) 80 result = returnValue; 81 else 82 fprintf(stderr, "wait_for_thread(%ld) A failed: %s\n", threadA, strerror(status)); 83 84 do { 85 status = wait_for_thread(threadB, &returnValue); 86 } while (status == B_INTERRUPTED); 87 88 if (status == B_OK) 89 result += returnValue; 90 else 91 fprintf(stderr, "wait_for_thread(%ld) B failed: %s\n", threadB, strerror(status)); 92 } 93 94 if (silent) { 95 return result; 96 } else { 97 printf("%d\n", result); 98 return 0; 99 } 100 } 101 102