Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
4.5 KiB

  1. //
  2. // Usage comments:
  3. // Current working directory default is d:/psx - put test files there
  4. // Needs tstf.one tstf.two out.dat
  5. // 'file tstf.one tstf.two'
  6. //
  7. #include <nt.h>
  8. #include <ntrtl.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include <sys/wait.h>
  13. #include <unistd.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include "tsttmp.h" // defines DbgPrint as printf
  17. extern int errno;
  18. VOID file0(char *);
  19. VOID file1(char *);
  20. VOID file2(char *, char *);
  21. VOID file3(char *);
  22. VOID file4(char *, char *);
  23. int
  24. main(int argc, char *argv[])
  25. {
  26. if (argc != 3) {
  27. DbgPrint("Usage: 'tstfile tstf.one tstf.two'\n");
  28. return 1;
  29. }
  30. file0(argv[1]);
  31. file1(argv[1]);
  32. file2(argv[1],argv[2]);
  33. file3(argv[1]);
  34. file4(argv[1],argv[2]);
  35. return 1;
  36. }
  37. VOID
  38. file0(char *f)
  39. {
  40. int rc,fd;
  41. char buf[512];
  42. DbgPrint("file0:++ %s\n",f);
  43. fd = open(f,O_RDONLY);
  44. ASSERT(fd != -1);
  45. rc = read(fd,buf,512);
  46. ASSERT(rc != -1);
  47. rc = close(fd);
  48. ASSERT(rc != -1);
  49. DbgPrint("file0:--\n");
  50. }
  51. VOID
  52. file1(char *f)
  53. {
  54. int rcb,wcb,ifd,ofd;
  55. char buf[512], testbuf[128];
  56. struct stat statbuf;
  57. struct stat *ps;
  58. int i;
  59. DbgPrint("file1:++ %s\n",f);
  60. ps = &statbuf;
  61. // stat always fails with ENOENT for now - see comment in psx/fdapi.c
  62. rcb = stat(f, ps);
  63. if (rcb == -1)
  64. DbgPrint("FAIL on stat: errno = %d\n", errno);
  65. else {
  66. DbgPrint("Mode = %lx, ino = %lx, dev = %ld, nlink = %ld, uid = %lx\n",
  67. ps->st_mode, ps->st_ino, ps->st_dev, ps->st_nlink, ps->st_uid);
  68. DbgPrint("gid = %lx, size = %ld, atime = %lx, mtime = %lx, ctime = %lx\n",
  69. ps->st_gid, ps->st_size, ps->st_atime, ps->st_mtime, ps->st_ctime);
  70. }
  71. ifd = open(f,O_RDONLY);
  72. ASSERT(ifd != -1);
  73. ofd = open("out.dat",O_WRONLY | O_TRUNC);
  74. ASSERT(ofd != -1);
  75. do {
  76. rcb = read(ifd,buf,512);
  77. ASSERT(rcb != -1);
  78. wcb = write(ofd,buf,rcb);
  79. ASSERT(wcb != -1);
  80. } while (rcb == 512);
  81. rcb = close(ofd);
  82. ASSERT(rcb != -1);
  83. ofd = open("out.dat",O_RDWR);
  84. ASSERT(ofd != -1);
  85. for (i = 0; i < 128; i++) {
  86. testbuf[i] = (char) i;
  87. buf[i] = 0;
  88. }
  89. wcb = write(ofd,testbuf,128);
  90. ASSERT(wcb != -1);
  91. lseek(ofd, 0L, SEEK_SET);
  92. rcb = read(ofd,buf,128);
  93. ASSERT(rcb != -1);
  94. if (rcb == -1)
  95. DbgPrint("errno = %d\n", errno);
  96. for (i = 0; i < 128; i++) {
  97. if (buf[i] != testbuf[i]) {
  98. DbgPrint("FAIL buffer contents check at %d\n", i);
  99. for (i = 0; i < 128; i++) {
  100. DbgPrint("%d ", buf[i]);
  101. }
  102. DbgPrint("\n");
  103. break;
  104. }
  105. }
  106. DbgPrint("Testing fstat on %s\n", f);
  107. rcb = fstat(ifd, ps);
  108. if (rcb == -1)
  109. DbgPrint("FAIL on fstat: errno = %d\n", errno);
  110. else
  111. DbgPrint("Mode = %lx, ino = %lx, dev = %ld, nlink = %ld, uid = %lx\n",
  112. ps->st_mode, ps->st_ino, ps->st_dev, ps->st_nlink, ps->st_uid);
  113. DbgPrint("gid = %lx, size = %ld, atime = %lx, mtime = %lx, ctime = %lx\n",
  114. ps->st_gid, ps->st_size, ps->st_atime, ps->st_mtime, ps->st_ctime);
  115. rcb = close(ifd);
  116. ASSERT(rcb != -1);
  117. rcb = close(ofd);
  118. ASSERT(rcb != -1);
  119. DbgPrint("file1:--\n");
  120. }
  121. VOID
  122. file2(char *f1,char *f2)
  123. {
  124. int fd;
  125. DbgPrint("file2:++ %s %s\n",f1,f2);
  126. fd = open(f1, O_RDONLY | O_CREAT | O_EXCL, 0);
  127. ASSERT(fd == -1 && errno == EEXIST);
  128. if (fd == -1 && errno != EEXIST)
  129. DbgPrint("FAIL: errno = %d\n", errno);
  130. DbgPrint("file2:--\n");
  131. }
  132. VOID
  133. file3(char *f)
  134. {
  135. int rc, fd, fd2;
  136. char buf[512];
  137. DbgPrint("file3:++ %s - Testing dup\n",f);
  138. fd = open(f,O_RDONLY);
  139. ASSERT(fd != -1);
  140. rc = read(fd,buf,512);
  141. ASSERT(rc != -1);
  142. fd2 = dup(fd);
  143. ASSERT(fd2 != -1);
  144. rc = close(fd);
  145. ASSERT(rc != -1);
  146. rc = read(fd2,buf,512);
  147. ASSERT(rc != -1);
  148. rc = close(fd2);
  149. ASSERT(rc != -1);
  150. DbgPrint("file3:--\n");
  151. }
  152. VOID
  153. file4(char *f1,char *f2)
  154. {
  155. int rc, fd, fd2, fd3;
  156. char buf[512];
  157. DbgPrint("file4:++ %s %s - Testing dup2\n",f1, f2);
  158. fd = open(f1,O_RDONLY);
  159. fd2 = open(f2,O_RDONLY);
  160. fd3 = open(f2,O_RDONLY);
  161. ASSERT(fd != -1 && fd2 != -1 && fd3 != -1);
  162. rc = read(fd,buf,512);
  163. ASSERT(rc != -1);
  164. rc = read(fd2,buf,512);
  165. ASSERT(rc != -1);
  166. fd2 = dup2(fd, fd2);
  167. ASSERT(fd2 != -1);
  168. rc = close(fd);
  169. ASSERT(rc != -1);
  170. rc = close(fd3);
  171. ASSERT(rc != -1);
  172. rc = read(fd2,buf,512);
  173. ASSERT(rc != -1);
  174. rc = close(fd2);
  175. ASSERT(rc != -1);
  176. DbgPrint("file4:--\n");
  177. }