openfile.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * This test calls PR_OpenFile to create a bunch of files
  7. * with various file modes.
  8. */
  9. #include "prio.h"
  10. #include "prerror.h"
  11. #include "prinit.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #define TEMPLATE_FILE_NAME "template.txt"
  15. int main(int argc, char **argv)
  16. {
  17. FILE *template;
  18. char buf[32];
  19. PRInt32 nbytes;
  20. PRFileDesc *fd;
  21. /* Write in text mode. Let stdio deal with line endings. */
  22. template = fopen(TEMPLATE_FILE_NAME, "w");
  23. fputs("line 1\nline 2\n", template);
  24. fclose(template);
  25. /* Read in binary mode */
  26. fd = PR_OpenFile(TEMPLATE_FILE_NAME, PR_RDONLY, 0666);
  27. nbytes = PR_Read(fd, buf, sizeof(buf));
  28. PR_Close(fd);
  29. PR_Delete(TEMPLATE_FILE_NAME);
  30. fd = PR_OpenFile("tfil0700.txt", PR_RDWR | PR_CREATE_FILE, 0700);
  31. if (NULL == fd) {
  32. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  33. PR_GetError(), PR_GetOSError());
  34. exit(1);
  35. }
  36. PR_Write(fd, buf, nbytes);
  37. PR_Close(fd);
  38. fd = PR_OpenFile("tfil0500.txt", PR_RDWR | PR_CREATE_FILE, 0500);
  39. if (NULL == fd) {
  40. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  41. PR_GetError(), PR_GetOSError());
  42. exit(1);
  43. }
  44. PR_Write(fd, buf, nbytes);
  45. PR_Close(fd);
  46. fd = PR_OpenFile("tfil0400.txt", PR_RDWR | PR_CREATE_FILE, 0400);
  47. if (NULL == fd) {
  48. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  49. PR_GetError(), PR_GetOSError());
  50. exit(1);
  51. }
  52. PR_Write(fd, buf, nbytes);
  53. PR_Close(fd);
  54. fd = PR_OpenFile("tfil0644.txt", PR_RDWR | PR_CREATE_FILE, 0644);
  55. if (NULL == fd) {
  56. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  57. PR_GetError(), PR_GetOSError());
  58. exit(1);
  59. }
  60. PR_Write(fd, buf, nbytes);
  61. PR_Close(fd);
  62. fd = PR_OpenFile("tfil0664.txt", PR_RDWR | PR_CREATE_FILE, 0664);
  63. if (NULL == fd) {
  64. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  65. PR_GetError(), PR_GetOSError());
  66. exit(1);
  67. }
  68. PR_Write(fd, buf, nbytes);
  69. PR_Close(fd);
  70. fd = PR_OpenFile("tfil0660.txt", PR_RDWR | PR_CREATE_FILE, 0660);
  71. if (NULL == fd) {
  72. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  73. PR_GetError(), PR_GetOSError());
  74. exit(1);
  75. }
  76. PR_Write(fd, buf, nbytes);
  77. PR_Close(fd);
  78. fd = PR_OpenFile("tfil0666.txt", PR_RDWR | PR_CREATE_FILE, 0666);
  79. if (NULL == fd) {
  80. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  81. PR_GetError(), PR_GetOSError());
  82. exit(1);
  83. }
  84. PR_Write(fd, buf, nbytes);
  85. PR_Close(fd);
  86. fd = PR_OpenFile("tfil0640.txt", PR_RDWR | PR_CREATE_FILE, 0640);
  87. if (NULL == fd) {
  88. fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
  89. PR_GetError(), PR_GetOSError());
  90. exit(1);
  91. }
  92. PR_Write(fd, buf, nbytes);
  93. PR_Close(fd);
  94. PR_Cleanup();
  95. return 0;
  96. }