diff options
Diffstat (limited to 'examples/test.c')
| -rw-r--r-- | examples/test.c | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/examples/test.c b/examples/test.c new file mode 100644 index 0000000..44dc307 --- /dev/null +++ b/examples/test.c | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | /* test.c | ||
| 2 | |||
| 3 | Experiments with syscalls | ||
| 4 | argc < 2 Print Hello World | ||
| 5 | argv[1][0] == 'p' print argv[2] | ||
| 6 | == 'e' Exec Test | ||
| 7 | == 'f' File test | ||
| 8 | == 'F' File descriptor stress test | ||
| 9 | == 'h' Halt | ||
| 10 | == '0' Null-Pointer Access | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include <stdio.h> | ||
| 14 | #include <syscall.h> | ||
| 15 | |||
| 16 | #define LARGE_BUF_SIZE 4150 | ||
| 17 | char large_buf[LARGE_BUF_SIZE]; | ||
| 18 | |||
| 19 | #define NUM_EXEC_CHILDS 7 | ||
| 20 | char *execs[NUM_EXEC_CHILDS] = { "test", "test p FOO", "test p BAR", "test f", "test 0", &large_buf[0], "test^" }; | ||
| 21 | |||
| 22 | #define MAX_FD 4097 | ||
| 23 | |||
| 24 | static void init_args(void); | ||
| 25 | static void init_args() | ||
| 26 | { | ||
| 27 | int i = 0; | ||
| 28 | char *t = ""; | ||
| 29 | while(i < LARGE_BUF_SIZE-1) { | ||
| 30 | if(!*t) t = "test "; | ||
| 31 | large_buf[i++] = *t++; | ||
| 32 | } | ||
| 33 | large_buf[LARGE_BUF_SIZE-1]='\0'; | ||
| 34 | } | ||
| 35 | |||
| 36 | int | ||
| 37 | main (int argc, char** argv) | ||
| 38 | { | ||
| 39 | if(argc < 2) { | ||
| 40 | printf("Hello World!\n"); | ||
| 41 | exit(0); | ||
| 42 | } | ||
| 43 | init_args(); | ||
| 44 | if(argv[1][0] == 'e') { | ||
| 45 | int r = 0; | ||
| 46 | int i; | ||
| 47 | int tid[NUM_EXEC_CHILDS]; | ||
| 48 | |||
| 49 | for(i = 0; i < NUM_EXEC_CHILDS; i++) { | ||
| 50 | tid[i] = exec(execs[i]); | ||
| 51 | } | ||
| 52 | for(i = 0; i < NUM_EXEC_CHILDS; i++) { | ||
| 53 | if (tid[i] >= 0) { | ||
| 54 | r = wait(tid[i]); | ||
| 55 | printf("P child %d exited with exit code %d\n",i, r); | ||
| 56 | } else { | ||
| 57 | printf("P child %d failed to start\n", i); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } else if(argv[1][0] == 'f') { | ||
| 61 | char buf[10]; | ||
| 62 | int r; | ||
| 63 | create ("test.txt", 10); | ||
| 64 | int handle = open ("test.txt"); | ||
| 65 | if (handle < 2) | ||
| 66 | printf ("open(test.txt) returned %d", handle); | ||
| 67 | if ((r=write(handle,"987654321",10)) != 10) { | ||
| 68 | printf("write failed: %d not %d\n",r,10); | ||
| 69 | exit(1); | ||
| 70 | } | ||
| 71 | seek(handle,0); | ||
| 72 | if ((r=read(handle, buf, 10)) != 10) { | ||
| 73 | printf("read failed: %d not %d\n",r,10); | ||
| 74 | exit(1); | ||
| 75 | } | ||
| 76 | printf("test.txt: %s\n", buf); | ||
| 77 | } else if(argv[1][0] == 'F') { | ||
| 78 | int j,i; | ||
| 79 | create ("foo.txt", 10); | ||
| 80 | for (j = 0; j < 5; j++) { | ||
| 81 | for (i = 2; i <= MAX_FD; i++) { | ||
| 82 | if (open ("foo.txt") < 0) { | ||
| 83 | printf("Opening the %d's file failed\n",i-2); | ||
| 84 | break; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | while(--i >= 2) { | ||
| 88 | close (i); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } else if(argv[1][0] == '0') { | ||
| 92 | printf("Null pointer value is: %d\n",*((int*)NULL)); | ||
| 93 | } else if(argv[1][0] == 'h') { | ||
| 94 | halt(); | ||
| 95 | } else if(argv[1][0] == 'p' && argc >= 3) { | ||
| 96 | printf("%s\n", argv[2]); | ||
| 97 | } else { | ||
| 98 | printf("ARGV[1] is %s\n", argv[1]); | ||
| 99 | } | ||
| 100 | return 0; | ||
| 101 | } | ||
