00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 #include <stdio.h>
00037 #include <stdlib.h>
00038 #include <string.h>
00039 #include <unistd.h>
00040 #include <limits.h>
00041 #include <errno.h>
00042 #include <err.h>
00043
00044 #include "config.h"
00045 #include "test.h"
00046
00047 static
00048 void
00049 dup2_fd2(int fd, const char *desc)
00050 {
00051 int rv;
00052
00053 rv = dup2(STDIN_FILENO, fd);
00054 report_test(rv, errno, EBADF, desc);
00055
00056 if (rv != -1) {
00057 close(fd);
00058 }
00059 }
00060
00061 static
00062 void
00063 dup2_self(void)
00064 {
00065 struct stat sb;
00066 int rv;
00067 int testfd;
00068
00069
00070 testfd = CLOSED_FD;
00071
00072 rv = dup2(STDIN_FILENO, testfd);
00073 if (rv == -1) {
00074 warn("UH-OH: couldn't copy stdin");
00075 return;
00076 }
00077
00078 rv = dup2(testfd, testfd);
00079 if (rv == testfd) {
00080 warnx("passed: dup2 to same fd");
00081 }
00082 else if (rv<0) {
00083 warn("FAILURE: dup2 to same fd: error");
00084 }
00085 else {
00086 warnx("FAILURE: dup2 to same fd: returned %d instead", rv);
00087 }
00088
00089 rv = fstat(testfd, &sb);
00090 if (rv==0) {
00091 warnx("passed: fstat fd after dup2 to itself");
00092 }
00093 else if (errno!=EUNIMP && errno!=ENOSYS) {
00094 warn("FAILURE: fstat fd after dup2 to itself");
00095 }
00096 else {
00097
00098 rv = lseek(testfd, 0, SEEK_CUR);
00099 if (rv==0 || (rv==-1 && errno==ESPIPE)) {
00100 warnx("passed: lseek fd after dup2 to itself");
00101 }
00102 else {
00103 warn("FAILURE: lseek fd after dup2 to itself");
00104 }
00105 }
00106
00107 close(testfd);
00108 }
00109
00110 void
00111 test_dup2(void)
00112 {
00113
00114 test_dup2_fd();
00115
00116
00117 dup2_fd2(-1, "dup2 to -1");
00118 dup2_fd2(-5, "dup2 to -5");
00119 dup2_fd2(IMPOSSIBLE_FD, "dup2 to impossible fd");
00120 #ifdef OPEN_MAX
00121 dup2_fd2(OPEN_MAX, "dup2 to OPEN_MAX");
00122 #else
00123 warnx("Warning: OPEN_MAX not defined - test skipped");
00124 #endif
00125
00126 dup2_self();
00127 }