michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * This test calls PR_OpenFile to create a bunch of files michael@0: * with various file modes. michael@0: */ michael@0: michael@0: #include "prio.h" michael@0: #include "prerror.h" michael@0: #include "prinit.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #define TEMPLATE_FILE_NAME "template.txt" michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: FILE *template; michael@0: char buf[32]; michael@0: PRInt32 nbytes; michael@0: PRFileDesc *fd; michael@0: michael@0: michael@0: /* Write in text mode. Let stdio deal with line endings. */ michael@0: template = fopen(TEMPLATE_FILE_NAME, "w"); michael@0: fputs("line 1\nline 2\n", template); michael@0: fclose(template); michael@0: michael@0: /* Read in binary mode */ michael@0: fd = PR_OpenFile(TEMPLATE_FILE_NAME, PR_RDONLY, 0666); michael@0: nbytes = PR_Read(fd, buf, sizeof(buf)); michael@0: PR_Close(fd); michael@0: PR_Delete(TEMPLATE_FILE_NAME); michael@0: michael@0: fd = PR_OpenFile("tfil0700.txt", PR_RDWR | PR_CREATE_FILE, 0700); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0500.txt", PR_RDWR | PR_CREATE_FILE, 0500); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0400.txt", PR_RDWR | PR_CREATE_FILE, 0400); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0644.txt", PR_RDWR | PR_CREATE_FILE, 0644); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0664.txt", PR_RDWR | PR_CREATE_FILE, 0664); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0660.txt", PR_RDWR | PR_CREATE_FILE, 0660); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0666.txt", PR_RDWR | PR_CREATE_FILE, 0666); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: fd = PR_OpenFile("tfil0640.txt", PR_RDWR | PR_CREATE_FILE, 0640); michael@0: if (NULL == fd) { michael@0: fprintf(stderr, "PR_OpenFile failed (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: PR_Write(fd, buf, nbytes); michael@0: PR_Close(fd); michael@0: michael@0: PR_Cleanup(); michael@0: return 0; michael@0: }