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 /*
7  * This test calls PR_OpenFile to create a bunch of files
8  * with various file modes.
9  */
10 
11 #include "prio.h"
12 #include "prerror.h"
13 #include "prinit.h"
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 
18 #define TEMPLATE_FILE_NAME "template.txt"
19 
main(int argc,char ** argv)20 int main(int argc, char **argv)
21 {
22     FILE *template;
23     char buf[32];
24     PRInt32 nbytes;
25     PRFileDesc *fd;
26 
27 
28     /* Write in text mode.  Let stdio deal with line endings. */
29     template = fopen(TEMPLATE_FILE_NAME, "w");
30     fputs("line 1\nline 2\n", template);
31     fclose(template);
32 
33     /* Read in binary mode */
34     fd = PR_OpenFile(TEMPLATE_FILE_NAME, PR_RDONLY, 0666);
35     nbytes = PR_Read(fd, buf, sizeof(buf));
36     PR_Close(fd);
37     PR_Delete(TEMPLATE_FILE_NAME);
38 
39     fd = PR_OpenFile("tfil0700.txt", PR_RDWR | PR_CREATE_FILE, 0700);
40     if (NULL == fd) {
41         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
42                 PR_GetError(), PR_GetOSError());
43         exit(1);
44     }
45     PR_Write(fd, buf, nbytes);
46     PR_Close(fd);
47 
48     fd = PR_OpenFile("tfil0500.txt", PR_RDWR | PR_CREATE_FILE, 0500);
49     if (NULL == fd) {
50         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
51                 PR_GetError(), PR_GetOSError());
52         exit(1);
53     }
54     PR_Write(fd, buf, nbytes);
55     PR_Close(fd);
56 
57     fd = PR_OpenFile("tfil0400.txt", PR_RDWR | PR_CREATE_FILE, 0400);
58     if (NULL == fd) {
59         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
60                 PR_GetError(), PR_GetOSError());
61         exit(1);
62     }
63     PR_Write(fd, buf, nbytes);
64     PR_Close(fd);
65 
66     fd = PR_OpenFile("tfil0644.txt", PR_RDWR | PR_CREATE_FILE, 0644);
67     if (NULL == fd) {
68         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
69                 PR_GetError(), PR_GetOSError());
70         exit(1);
71     }
72     PR_Write(fd, buf, nbytes);
73     PR_Close(fd);
74 
75     fd = PR_OpenFile("tfil0664.txt", PR_RDWR | PR_CREATE_FILE, 0664);
76     if (NULL == fd) {
77         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
78                 PR_GetError(), PR_GetOSError());
79         exit(1);
80     }
81     PR_Write(fd, buf, nbytes);
82     PR_Close(fd);
83 
84     fd = PR_OpenFile("tfil0660.txt", PR_RDWR | PR_CREATE_FILE, 0660);
85     if (NULL == fd) {
86         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
87                 PR_GetError(), PR_GetOSError());
88         exit(1);
89     }
90     PR_Write(fd, buf, nbytes);
91     PR_Close(fd);
92 
93     fd = PR_OpenFile("tfil0666.txt", PR_RDWR | PR_CREATE_FILE, 0666);
94     if (NULL == fd) {
95         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
96                 PR_GetError(), PR_GetOSError());
97         exit(1);
98     }
99     PR_Write(fd, buf, nbytes);
100     PR_Close(fd);
101 
102     fd = PR_OpenFile("tfil0640.txt", PR_RDWR | PR_CREATE_FILE, 0640);
103     if (NULL == fd) {
104         fprintf(stderr, "PR_OpenFile failed (%d, %d)\n",
105                 PR_GetError(), PR_GetOSError());
106         exit(1);
107     }
108     PR_Write(fd, buf, nbytes);
109     PR_Close(fd);
110 
111     PR_Cleanup();
112     return 0;
113 }
114