First part of program argument and environment variable support.
[planlOS.git] / programs / test1 / test1.c
blob48a68fe78c0fcaf3b37b8e2315c0a6e9870e1abd
2 #include <signal.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <sys/ioctl.h>
6 #include <termios.h>
7 #include <unistd.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <sys/wait.h>
11 #include <time.h>
12 #include <sys/time.h>
14 void size_changed(int sig)
16 struct winsize size;
17 int status = ioctl(0, TIOCGWINSZ, &size);
18 if (status != -1)
20 char tmp[30];
21 snprintf(tmp, 30, "\033[%d;%dH", size.ws_row, size.ws_col);
22 write(1, tmp, strlen(tmp));
23 write(1, "*", 1);
27 int main(int argc, char **argv)
29 signal(SIGWINCH, size_changed);
30 // Initialize screen
31 write(1, "\033[2J", 4);
32 write(1, "\033[0;0H", 6);
33 write(1, "\033[30;41m", 8);
34 write(1, "Hello ", 6);
35 write(1, "\033[33;40m", 8);
36 write(1, "World!\n", 7);
38 struct winsize size;
39 int status = ioctl(0, TIOCGWINSZ, &size);
40 if (status != -1)
42 write(1, "\033[37;40m", 8);
43 printf("Size: %d/%d.\n", size.ws_row, size.ws_col);
44 write(1, "\033[53;180H", 9);
45 write(1, "*", 1);
48 write(1, "\033[4;0H", 6);
49 int i;
50 for (i = 0; i < argc; i++)
52 printf("Arg. %d: %s\n", i, argv[i]);
55 int time1 = time(0);
56 struct timeval tv1;
57 gettimeofday(&tv1, 0);
58 write(1, "\033[30;41m", 8);
59 write(1, "fork()! \n", 15);
60 for (i = 0; i < 1000; i++)
62 int pid = fork();
63 if (!pid)
65 exit(0);
67 waitpid(pid, 0, 0);
69 write(1, "DONE! \n", 15);
70 int time2 = time(0);
71 struct timeval tv2;
72 gettimeofday(&tv2, 0);
73 printf("%d seconds (%d).\n", time2 - time1, time2);
75 if (tv1.tv_usec > tv2.tv_usec)
77 tv2.tv_usec += 1000000;
78 tv2.tv_sec -= 1;
80 int difference = tv2.tv_usec / 1000 - tv1.tv_usec / 1000 + tv2.tv_sec * 1000 - tv1.tv_sec * 1000;
81 write(1, "\033[7;0H", 6);
82 printf("%d seconds.\n", difference);
84 char c;
85 while ((c = getchar()) != 'q')
87 putchar(c);
89 return 0;