1/* $OpenBSD: stdio.c,v 1.3 2004/02/27 19:58:08 deraadt Exp $ */ 2/* 3 * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors, 4 * proven@mit.edu All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Chris Provenzano, 17 * the University of California, Berkeley, and contributors. 18 * 4. Neither the name of Chris Provenzano, the University, nor the names of 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR 26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35#include <pthread.h> 36#include <sys/types.h> 37#include <sys/stat.h> 38#include <string.h> 39#include <stdlib.h> 40#include <stdio.h> 41#include "test.h" 42 43char * base_name = "stdio.c"; 44char * dir_name = SRCDIR; 45char * fullname; 46 47/* Test fopen()/ftell()/getc() */ 48static void 49test_1(void) 50{ 51 struct stat statbuf; 52 FILE * fp; 53 int i; 54 55 CHECKe(stat(fullname, &statbuf)); 56 57 CHECKn((fp = fopen(fullname, "r"))); 58 59 /* Get the entire file */ 60 while ((i = getc(fp)) != EOF) 61 ; 62 63 ASSERT(ftell(fp) == statbuf.st_size); 64 65 CHECKe(fclose(fp)); 66} 67 68/* Test fopen()/fclose() */ 69static void 70test_2(void) 71{ 72 FILE *fp1, *fp2; 73 74 CHECKn(fp1 = fopen(fullname, "r")); 75 CHECKe(fclose(fp1)); 76 77 CHECKn(fp2 = fopen(fullname, "r")); 78 CHECKe(fclose(fp2)); 79 80 ASSERT(fp1 == fp2); 81} 82 83/* Test sscanf()/sprintf() */ 84static void 85test_3(void) 86{ 87 char * str = "10 4.53"; 88 char buf[64]; 89 double d; 90 int i; 91 92 ASSERT(sscanf(str, "%d %lf", &i, &d) == 2); 93 94 /* Should have a check */ 95 snprintf(buf, sizeof buf, "%d %2.2f", i, d); 96 ASSERT(strcmp(buf, str) == 0); 97} 98 99int 100main(int argc, char *argv[]) 101{ 102 103 int len = strlen (dir_name) + strlen (base_name) + 2; 104 105 CHECKn(fullname = malloc (len)); 106 snprintf(fullname, len, "%s/%s", dir_name, base_name); 107 108 test_1(); 109 test_2(); 110 test_3(); 111 112 SUCCEED; 113} 114