fopen_test.c revision 255303
1/*- 2 * Copyright (c) 2013 Jilles Tjoelker 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-fopen.c 255303 2013-09-06 13:47:16Z jilles $"); 29 30#include <fcntl.h> 31#include <stdio.h> 32#include <string.h> 33 34/* 35 * O_ACCMODE is currently defined incorrectly. This is what it should be. 36 * Various code depends on the incorrect value. 37 */ 38#define CORRECT_O_ACCMODE (O_ACCMODE | O_EXEC) 39 40static int testnum = 1; 41 42static void 43runtest(const char *fname, const char *mode) 44{ 45 FILE *fp; 46 int fd, flags, wantedflags; 47 48 fp = fopen(fname, mode); 49 if (fp == NULL) { 50 printf("not ok %d - fopen(\"%s\", \"%s\") failed\n", 51 testnum++, fname, mode); 52 printf("not ok %d - FD_CLOEXEC # SKIP\n", 53 testnum++); 54 return; 55 } 56 fd = fileno(fp); 57 if (fd < 0) 58 printf("not ok %d - fileno() failed\n", testnum++); 59 else 60 printf("ok %d - fopen(\"%s\", \"%s\") and fileno() succeeded\n", 61 testnum++, fname, mode); 62 if (fcntl(fd, F_GETFD) == (strchr(mode, 'e') != NULL ? FD_CLOEXEC : 0)) 63 printf("ok %d - FD_CLOEXEC flag correct\n", testnum++); 64 else 65 printf("not ok %d - FD_CLOEXEC flag incorrect\n", testnum++); 66 flags = fcntl(fd, F_GETFL); 67 if (strchr(mode, '+')) 68 wantedflags = O_RDWR | (*mode == 'a' ? O_APPEND : 0); 69 else if (*mode == 'r') 70 wantedflags = O_RDONLY; 71 else if (*mode == 'w') 72 wantedflags = O_WRONLY; 73 else if (*mode == 'a') 74 wantedflags = O_WRONLY | O_APPEND; 75 else 76 wantedflags = -1; 77 if (wantedflags == -1) 78 printf("not ok %d - unrecognized mode\n", testnum++); 79 else if ((flags & (CORRECT_O_ACCMODE | O_APPEND)) == wantedflags) 80 printf("ok %d - correct access mode\n", testnum++); 81 else 82 printf("not ok %d - incorrect access mode\n", testnum++); 83 fclose(fp); 84} 85 86/* 87 * Test program for fopen(). 88 */ 89int 90main(int argc, char *argv[]) 91{ 92 printf("1..45\n"); 93 runtest("/dev/null", "r"); 94 runtest("/dev/null", "r+"); 95 runtest("/dev/null", "w"); 96 runtest("/dev/null", "w+"); 97 runtest("/dev/null", "a"); 98 runtest("/dev/null", "a+"); 99 runtest("/dev/null", "re"); 100 runtest("/dev/null", "r+e"); 101 runtest("/dev/null", "we"); 102 runtest("/dev/null", "w+e"); 103 runtest("/dev/null", "ae"); 104 runtest("/dev/null", "a+e"); 105 runtest("/dev/null", "re+"); 106 runtest("/dev/null", "we+"); 107 runtest("/dev/null", "ae+"); 108 109 return 0; 110} 111 112/* vim:ts=8:cin:sw=8 113 * */ 114