nsprpub/pr/tests/openfile.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial