1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/openfile.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,113 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * This test calls PR_OpenFile to create a bunch of files 1.11 + * with various file modes. 1.12 + */ 1.13 + 1.14 +#include "prio.h" 1.15 +#include "prerror.h" 1.16 +#include "prinit.h" 1.17 + 1.18 +#include <stdio.h> 1.19 +#include <stdlib.h> 1.20 + 1.21 +#define TEMPLATE_FILE_NAME "template.txt" 1.22 + 1.23 +int main(int argc, char **argv) 1.24 +{ 1.25 + FILE *template; 1.26 + char buf[32]; 1.27 + PRInt32 nbytes; 1.28 + PRFileDesc *fd; 1.29 + 1.30 + 1.31 + /* Write in text mode. Let stdio deal with line endings. */ 1.32 + template = fopen(TEMPLATE_FILE_NAME, "w"); 1.33 + fputs("line 1\nline 2\n", template); 1.34 + fclose(template); 1.35 + 1.36 + /* Read in binary mode */ 1.37 + fd = PR_OpenFile(TEMPLATE_FILE_NAME, PR_RDONLY, 0666); 1.38 + nbytes = PR_Read(fd, buf, sizeof(buf)); 1.39 + PR_Close(fd); 1.40 + PR_Delete(TEMPLATE_FILE_NAME); 1.41 + 1.42 + fd = PR_OpenFile("tfil0700.txt", PR_RDWR | PR_CREATE_FILE, 0700); 1.43 + if (NULL == fd) { 1.44 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.45 + PR_GetError(), PR_GetOSError()); 1.46 + exit(1); 1.47 + } 1.48 + PR_Write(fd, buf, nbytes); 1.49 + PR_Close(fd); 1.50 + 1.51 + fd = PR_OpenFile("tfil0500.txt", PR_RDWR | PR_CREATE_FILE, 0500); 1.52 + if (NULL == fd) { 1.53 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.54 + PR_GetError(), PR_GetOSError()); 1.55 + exit(1); 1.56 + } 1.57 + PR_Write(fd, buf, nbytes); 1.58 + PR_Close(fd); 1.59 + 1.60 + fd = PR_OpenFile("tfil0400.txt", PR_RDWR | PR_CREATE_FILE, 0400); 1.61 + if (NULL == fd) { 1.62 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.63 + PR_GetError(), PR_GetOSError()); 1.64 + exit(1); 1.65 + } 1.66 + PR_Write(fd, buf, nbytes); 1.67 + PR_Close(fd); 1.68 + 1.69 + fd = PR_OpenFile("tfil0644.txt", PR_RDWR | PR_CREATE_FILE, 0644); 1.70 + if (NULL == fd) { 1.71 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.72 + PR_GetError(), PR_GetOSError()); 1.73 + exit(1); 1.74 + } 1.75 + PR_Write(fd, buf, nbytes); 1.76 + PR_Close(fd); 1.77 + 1.78 + fd = PR_OpenFile("tfil0664.txt", PR_RDWR | PR_CREATE_FILE, 0664); 1.79 + if (NULL == fd) { 1.80 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.81 + PR_GetError(), PR_GetOSError()); 1.82 + exit(1); 1.83 + } 1.84 + PR_Write(fd, buf, nbytes); 1.85 + PR_Close(fd); 1.86 + 1.87 + fd = PR_OpenFile("tfil0660.txt", PR_RDWR | PR_CREATE_FILE, 0660); 1.88 + if (NULL == fd) { 1.89 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.90 + PR_GetError(), PR_GetOSError()); 1.91 + exit(1); 1.92 + } 1.93 + PR_Write(fd, buf, nbytes); 1.94 + PR_Close(fd); 1.95 + 1.96 + fd = PR_OpenFile("tfil0666.txt", PR_RDWR | PR_CREATE_FILE, 0666); 1.97 + if (NULL == fd) { 1.98 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.99 + PR_GetError(), PR_GetOSError()); 1.100 + exit(1); 1.101 + } 1.102 + PR_Write(fd, buf, nbytes); 1.103 + PR_Close(fd); 1.104 + 1.105 + fd = PR_OpenFile("tfil0640.txt", PR_RDWR | PR_CREATE_FILE, 0640); 1.106 + if (NULL == fd) { 1.107 + fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", 1.108 + PR_GetError(), PR_GetOSError()); 1.109 + exit(1); 1.110 + } 1.111 + PR_Write(fd, buf, nbytes); 1.112 + PR_Close(fd); 1.113 + 1.114 + PR_Cleanup(); 1.115 + return 0; 1.116 +}