some coverity fixes.
[minix.git] / test / test61.c
blob42c3746e4e85ad6ebaa2b5a9b124a09c4f9df923
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <sys/wait.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <unistd.h>
8 #define MAX_ERROR 5
9 #include "common.c"
11 void dangling_slink(int sub_test, char const slink_to[PATH_MAX]);
13 void dangling_slink(int sub_test, char const slink_to[PATH_MAX])
15 pid_t child;
17 subtest = sub_test;
19 child = fork();
20 if (child == (pid_t) -1) {
21 e(1);
22 return;
23 } else if (child == (pid_t) 0) {
24 /* I'm the child. Create a dangling symlink with an absolute path */
25 int fd;
26 char buf[4];
29 /* We don't want to actually write to '/', so instead we pretend */
30 if (chroot(".") != 0) e(2);
32 /* Create file 'slink_to' with contents "bar" */
33 if ((fd = open(slink_to, O_CREAT|O_WRONLY)) == -1) e(3);
34 if (write(fd, "bar", strlen("bar")) != strlen("bar")) e(4);
35 close(fd);
37 if (symlink(slink_to, "a") == -1) e(5); /* Create the symlink */
38 if (rename(slink_to, "c") == -1) e(6); /* Make it a dangling symlink */
40 /* Write "foo" to symlink; this should recreate file 'slink_to' with
41 * contents "foo" */
42 if ((fd = open("a", O_CREAT|O_WRONLY)) == -1) e(7);
43 if (write(fd, "foo", strlen("foo")) != strlen("foo")) e(8);
44 close(fd);
46 /* Verify 'a' and 'slink_to' contain "foo" */
47 memset(buf, '\0', sizeof(buf));
48 if ((fd = open("a", O_RDONLY)) == -1) e(9);
49 if (read(fd, buf, 3) != 3) e(10);
50 if (strncmp(buf, "foo", strlen("foo"))) e(11);
51 close(fd);
52 memset(buf, '\0', sizeof(buf));
53 if ((fd = open(slink_to, O_RDONLY)) == -1) e(12);
54 if (read(fd, buf, 3) != 3) e(13);
55 if (strncmp(buf, "foo", strlen("foo"))) e(14);
56 close(fd);
58 /* Verify 'c' contains 'bar' */
59 memset(buf, '\0', sizeof(buf));
60 if ((fd = open("c", O_RDONLY)) == -1) e(15);
61 if (read(fd, buf, 3) != 3) e(16);
62 if (strncmp(buf, "bar", strlen("bar"))) e(17);
63 close(fd);
65 /* Cleanup created files */
66 if (unlink(slink_to) == -1) e(18);
67 if (unlink("a") == -1) e(19);
68 if (unlink("c") == -1) e(20);
70 exit(EXIT_SUCCESS);
71 } else {
72 int status;
73 if (wait(&status) == -1) e(7);
79 int main(int argc, char *argv[])
81 start(61);
82 dangling_slink(1, "/abs"); /* Create dangling symlink with absolute path */
83 dangling_slink(2, "rel"); /* Create dangling symlink with relative path */
84 quit();
85 return(-1); /* Unreachable */