1433d6423SLionel Sambuc /* test 3 - library routines rather than system calls */
2433d6423SLionel Sambuc
3433d6423SLionel Sambuc #include <sys/types.h>
4433d6423SLionel Sambuc #include <sys/utsname.h>
5433d6423SLionel Sambuc #include <errno.h>
6433d6423SLionel Sambuc #include <fcntl.h>
7433d6423SLionel Sambuc #include <limits.h>
8433d6423SLionel Sambuc #include <signal.h>
9433d6423SLionel Sambuc #include <stdlib.h>
10433d6423SLionel Sambuc #include <string.h>
11433d6423SLionel Sambuc #include <unistd.h>
12433d6423SLionel Sambuc #include <stdio.h>
13433d6423SLionel Sambuc
14433d6423SLionel Sambuc #define ITERATIONS 10
15433d6423SLionel Sambuc int max_error = 0;
16433d6423SLionel Sambuc #include "common.h"
17433d6423SLionel Sambuc
18433d6423SLionel Sambuc #define SIZE 64
19433d6423SLionel Sambuc
20433d6423SLionel Sambuc int subtest;
21433d6423SLionel Sambuc char el_weirdo[] = "\n\t\\\e@@!!##\e\e\n\n";
22433d6423SLionel Sambuc
23433d6423SLionel Sambuc
24433d6423SLionel Sambuc int main(int argc, char *argv []);
25433d6423SLionel Sambuc void test3a(void);
26433d6423SLionel Sambuc void test3c(void);
27433d6423SLionel Sambuc void test3d(void);
28433d6423SLionel Sambuc void test3e(void);
29433d6423SLionel Sambuc
main(argc,argv)30433d6423SLionel Sambuc int main(argc, argv)
31433d6423SLionel Sambuc int argc;
32433d6423SLionel Sambuc char *argv[];
33433d6423SLionel Sambuc {
34433d6423SLionel Sambuc int i, m = 0xFFFF;
35433d6423SLionel Sambuc
36433d6423SLionel Sambuc start(3);
37433d6423SLionel Sambuc if (argc == 2) m = atoi(argv[1]);
38433d6423SLionel Sambuc
39433d6423SLionel Sambuc for (i = 0; i < ITERATIONS; i++) {
40433d6423SLionel Sambuc if (m & 0001) test3a();
41433d6423SLionel Sambuc if (m & 0004) test3c();
42433d6423SLionel Sambuc if (m & 0010) test3d();
43433d6423SLionel Sambuc if (m & 0020) test3e();
44433d6423SLionel Sambuc }
45433d6423SLionel Sambuc quit();
46433d6423SLionel Sambuc return(-1); /* impossible */
47433d6423SLionel Sambuc
48433d6423SLionel Sambuc }
49433d6423SLionel Sambuc
test3a()50433d6423SLionel Sambuc void test3a()
51433d6423SLionel Sambuc {
52433d6423SLionel Sambuc /* Signal set manipulation. */
53433d6423SLionel Sambuc
54433d6423SLionel Sambuc sigset_t s, s1;
55433d6423SLionel Sambuc
56433d6423SLionel Sambuc subtest = 1;
57433d6423SLionel Sambuc errno = -1000; /* None of these calls set errno. */
58433d6423SLionel Sambuc if (sigemptyset(&s) != 0) e(1);
59433d6423SLionel Sambuc if (sigemptyset(&s1) != 0) e(2);
60433d6423SLionel Sambuc if (sigaddset(&s, SIGABRT) != 0) e(3);
61433d6423SLionel Sambuc if (sigaddset(&s, SIGALRM) != 0) e(4);
62433d6423SLionel Sambuc if (sigaddset(&s, SIGFPE ) != 0) e(5);
63433d6423SLionel Sambuc if (sigaddset(&s, SIGHUP ) != 0) e(6);
64433d6423SLionel Sambuc if (sigaddset(&s, SIGILL ) != 0) e(7);
65433d6423SLionel Sambuc if (sigaddset(&s, SIGINT ) != 0) e(8);
66433d6423SLionel Sambuc if (sigaddset(&s, SIGKILL) != 0) e(9);
67433d6423SLionel Sambuc if (sigaddset(&s, SIGPIPE) != 0) e(10);
68433d6423SLionel Sambuc if (sigaddset(&s, SIGQUIT) != 0) e(11);
69433d6423SLionel Sambuc if (sigaddset(&s, SIGSEGV) != 0) e(12);
70433d6423SLionel Sambuc if (sigaddset(&s, SIGTERM) != 0) e(13);
71433d6423SLionel Sambuc if (sigaddset(&s, SIGUSR1) != 0) e(14);
72433d6423SLionel Sambuc if (sigaddset(&s, SIGUSR2) != 0) e(15);
73433d6423SLionel Sambuc
74433d6423SLionel Sambuc if (sigismember(&s, SIGABRT) != 1) e(16);
75433d6423SLionel Sambuc if (sigismember(&s, SIGALRM) != 1) e(17);
76433d6423SLionel Sambuc if (sigismember(&s, SIGFPE ) != 1) e(18);
77433d6423SLionel Sambuc if (sigismember(&s, SIGHUP ) != 1) e(19);
78433d6423SLionel Sambuc if (sigismember(&s, SIGILL ) != 1) e(20);
79433d6423SLionel Sambuc if (sigismember(&s, SIGINT ) != 1) e(21);
80433d6423SLionel Sambuc if (sigismember(&s, SIGKILL) != 1) e(22);
81433d6423SLionel Sambuc if (sigismember(&s, SIGPIPE) != 1) e(23);
82433d6423SLionel Sambuc if (sigismember(&s, SIGQUIT) != 1) e(24);
83433d6423SLionel Sambuc if (sigismember(&s, SIGSEGV) != 1) e(25);
84433d6423SLionel Sambuc if (sigismember(&s, SIGTERM) != 1) e(26);
85433d6423SLionel Sambuc if (sigismember(&s, SIGUSR1) != 1) e(27);
86433d6423SLionel Sambuc if (sigismember(&s, SIGUSR2) != 1) e(28);
87433d6423SLionel Sambuc
88433d6423SLionel Sambuc if (sigdelset(&s, SIGABRT) != 0) e(29);
89433d6423SLionel Sambuc if (sigdelset(&s, SIGALRM) != 0) e(30);
90433d6423SLionel Sambuc if (sigdelset(&s, SIGFPE ) != 0) e(31);
91433d6423SLionel Sambuc if (sigdelset(&s, SIGHUP ) != 0) e(32);
92433d6423SLionel Sambuc if (sigdelset(&s, SIGILL ) != 0) e(33);
93433d6423SLionel Sambuc if (sigdelset(&s, SIGINT ) != 0) e(34);
94433d6423SLionel Sambuc if (sigdelset(&s, SIGKILL) != 0) e(35);
95433d6423SLionel Sambuc if (sigdelset(&s, SIGPIPE) != 0) e(36);
96433d6423SLionel Sambuc if (sigdelset(&s, SIGQUIT) != 0) e(37);
97433d6423SLionel Sambuc if (sigdelset(&s, SIGSEGV) != 0) e(38);
98433d6423SLionel Sambuc if (sigdelset(&s, SIGTERM) != 0) e(39);
99433d6423SLionel Sambuc if (sigdelset(&s, SIGUSR1) != 0) e(40);
100433d6423SLionel Sambuc if (sigdelset(&s, SIGUSR2) != 0) e(41);
101433d6423SLionel Sambuc
102433d6423SLionel Sambuc if (memcmp(&s, &s1, sizeof(s))) e(42);
103433d6423SLionel Sambuc
104433d6423SLionel Sambuc if (sigaddset(&s, SIGILL) != 0) e(43);
105433d6423SLionel Sambuc if (!memcmp(&s, &s1, sizeof(s))) e(42);
106433d6423SLionel Sambuc
107433d6423SLionel Sambuc if (sigfillset(&s) != 0) e(45);
108433d6423SLionel Sambuc if (sigismember(&s, SIGABRT) != 1) e(46);
109433d6423SLionel Sambuc if (sigismember(&s, SIGALRM) != 1) e(47);
110433d6423SLionel Sambuc if (sigismember(&s, SIGFPE ) != 1) e(48);
111433d6423SLionel Sambuc if (sigismember(&s, SIGHUP ) != 1) e(49);
112433d6423SLionel Sambuc if (sigismember(&s, SIGILL ) != 1) e(50);
113433d6423SLionel Sambuc if (sigismember(&s, SIGINT ) != 1) e(51);
114433d6423SLionel Sambuc if (sigismember(&s, SIGKILL) != 1) e(52);
115433d6423SLionel Sambuc if (sigismember(&s, SIGPIPE) != 1) e(53);
116433d6423SLionel Sambuc if (sigismember(&s, SIGQUIT) != 1) e(54);
117433d6423SLionel Sambuc if (sigismember(&s, SIGSEGV) != 1) e(55);
118433d6423SLionel Sambuc if (sigismember(&s, SIGTERM) != 1) e(56);
119433d6423SLionel Sambuc if (sigismember(&s, SIGUSR1) != 1) e(57);
120433d6423SLionel Sambuc if (sigismember(&s, SIGUSR2) != 1) e(58);
121433d6423SLionel Sambuc
122433d6423SLionel Sambuc /* Test error returns. */
123433d6423SLionel Sambuc if (sigaddset(&s, -1) != -1) e(59);
124433d6423SLionel Sambuc if (sigaddset(&s, -1) != -1) e(60);
125433d6423SLionel Sambuc if (sigismember(&s, -1) != -1) e(61);
126433d6423SLionel Sambuc if (sigaddset(&s, 10000) != -1) e(62);
127433d6423SLionel Sambuc if (sigaddset(&s, 10000) != -1) e(63);
128433d6423SLionel Sambuc if (sigismember(&s, 10000) != -1) e(64);
129433d6423SLionel Sambuc
130433d6423SLionel Sambuc }
131433d6423SLionel Sambuc
test3c()132433d6423SLionel Sambuc void test3c()
133433d6423SLionel Sambuc {
134433d6423SLionel Sambuc /* Test getenv. Asume HOME, PATH, and LOGNAME exist (not strictly required).*/
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc char *p, name[SIZE];
137433d6423SLionel Sambuc
138433d6423SLionel Sambuc subtest = 3;
139433d6423SLionel Sambuc errno = -3000; /* None of these calls set errno. */
140433d6423SLionel Sambuc if ( (p = getenv("HOME")) == NULL) { fprintf(stderr, "Please have $HOME set properly.\n"); e(1); }
141433d6423SLionel Sambuc if (*p != '/') { fprintf(stderr, "Please have $HOME point to an abolute path.\n"); e(2); } /* path must be absolute */
142433d6423SLionel Sambuc if ( (p = getenv("PATH")) == NULL) { fprintf(stderr, "Please have $PATH set properly.\n"); e(3); }
143433d6423SLionel Sambuc if ( (p = getenv("LOGNAME")) == NULL) { fprintf(stderr, "Please have $LOGNAME set properly.\n"); e(5); }
144433d6423SLionel Sambuc strcpy(name, p); /* save it, since getlogin might wipe it out */
145433d6423SLionel Sambuc p = getlogin();
146433d6423SLionel Sambuc if (strcmp(p, name) != 0) { fprintf(stderr, "Please have $LOGNAME set to your real username. (su - instead of su?)\n"); e(6); }
147433d6423SLionel Sambuc
148433d6423SLionel Sambuc /* The following test could fail in a legal POSIX system. However, if it
149433d6423SLionel Sambuc * does, you deserve it to fail.
150433d6423SLionel Sambuc */
151433d6423SLionel Sambuc if (getenv(el_weirdo) != NULL) e(7);
152433d6423SLionel Sambuc }
153433d6423SLionel Sambuc
test3d()154433d6423SLionel Sambuc void test3d()
155433d6423SLionel Sambuc {
156433d6423SLionel Sambuc /* Test ctermid, ttyname, and isatty. */
157433d6423SLionel Sambuc
158433d6423SLionel Sambuc int fd;
159433d6423SLionel Sambuc char *p, name[L_ctermid];
160433d6423SLionel Sambuc
161433d6423SLionel Sambuc subtest = 4;
162433d6423SLionel Sambuc errno = -4000; /* None of these calls set errno. */
163433d6423SLionel Sambuc
164433d6423SLionel Sambuc /* Test ctermid first. */
165433d6423SLionel Sambuc if ( (p = ctermid(name)) == NULL) e(1);
166433d6423SLionel Sambuc if (strcmp(p, name) != 0) e(2);
167433d6423SLionel Sambuc if (strncmp(p, "/dev/tty", 8) != 0) e(3); /* MINIX convention */
168433d6423SLionel Sambuc
169433d6423SLionel Sambuc if ( (p = ttyname(0)) == NULL) e(4);
170*99e8768dSDavid van Moolenbroek if (strncmp(p, "/dev/tty", 8) != 0 && strcmp(p, "/dev/console") != 0 &&
171*99e8768dSDavid van Moolenbroek strncmp(p, "/dev/pts/", 9) != 0) e(5);
172433d6423SLionel Sambuc if ( (p = ttyname(3)) != NULL) e(6);
173433d6423SLionel Sambuc if (ttyname(5000) != NULL) e(7);
174433d6423SLionel Sambuc if ( (fd = creat("T3a", 0777)) < 0) e(8);
175433d6423SLionel Sambuc if (ttyname(fd) != NULL) e(9);
176433d6423SLionel Sambuc
177433d6423SLionel Sambuc if (isatty(0) != 1) e(10);
178433d6423SLionel Sambuc if (isatty(3) != 0) e(11);
179433d6423SLionel Sambuc if (isatty(fd) != 0) e(12);
180433d6423SLionel Sambuc if (close(fd) != 0) e(13);
181433d6423SLionel Sambuc if (ttyname(fd) != NULL) e(14);
182433d6423SLionel Sambuc }
183433d6423SLionel Sambuc
test3e()184433d6423SLionel Sambuc void test3e()
185433d6423SLionel Sambuc {
186433d6423SLionel Sambuc /* Test ctermid, ttyname, and isatty. */
187433d6423SLionel Sambuc
188433d6423SLionel Sambuc subtest = 5;
189433d6423SLionel Sambuc errno = -5000; /* None of these calls set errno. */
190433d6423SLionel Sambuc
191433d6423SLionel Sambuc if (sysconf(_SC_ARG_MAX) < _POSIX_ARG_MAX) e(1);
192433d6423SLionel Sambuc if (sysconf(_SC_CHILD_MAX) < _POSIX_CHILD_MAX) e(2);
193433d6423SLionel Sambuc if (sysconf(_SC_NGROUPS_MAX) < 0) e(3);
194433d6423SLionel Sambuc if (sysconf(_SC_OPEN_MAX) < _POSIX_OPEN_MAX) e(4);
195433d6423SLionel Sambuc
196433d6423SLionel Sambuc /* The rest are MINIX specific */
197433d6423SLionel Sambuc if (sysconf(_SC_JOB_CONTROL) >= 0) e(5); /* no job control! */
198433d6423SLionel Sambuc }
199433d6423SLionel Sambuc
200