From 29f4970e2f868b3225f69dee7a78a8bd142d4ec1 Mon Sep 17 00:00:00 2001 From: Robert Moore Date: Thu, 8 Nov 2012 08:45:29 -0800 Subject: Tools: Replace low-level I/O with stream I/O functions. Replace open/read/write/close with the stream I/O equivalents fopen/fread/fwrite/fclose for portability and performance Lv Zheng, Bob Moore. --- source/tools/acpibin/abcompare.c | 26 +++++++++++--------- source/tools/acpibin/acpibin.h | 15 ------------ source/tools/acpisrc/acpisrc.h | 30 ++++++++--------------- source/tools/acpisrc/asfile.c | 52 +++++++++++++++++++++++----------------- 4 files changed, 55 insertions(+), 68 deletions(-) diff --git a/source/tools/acpibin/abcompare.c b/source/tools/acpibin/abcompare.c index d448d3fd0..954133b12 100644 --- a/source/tools/acpibin/abcompare.c +++ b/source/tools/acpibin/abcompare.c @@ -120,7 +120,6 @@ FILE *File1; FILE *File2; ACPI_TABLE_HEADER Header1; ACPI_TABLE_HEADER Header2; -struct stat Gbl_StatBuf; #define BUFFER_SIZE 256 char Buffer[BUFFER_SIZE]; @@ -612,15 +611,18 @@ AbGetFile ( char *Filename, UINT32 *FileSize) { - int FileHandle; + FILE *File; UINT32 Size; char *Buffer = NULL; + int Seek1; + int Seek2; + size_t Actual; /* Binary mode does not alter CR/LF pairs */ - FileHandle = open (Filename, O_BINARY | O_RDONLY); - if (!FileHandle) + File = fopen (Filename, "rb"); + if (!File) { printf ("Could not open %s\n", Filename); return (NULL); @@ -628,15 +630,18 @@ AbGetFile ( /* Need file size to allocate a buffer */ - if (fstat (FileHandle, &Gbl_StatBuf)) + Seek1 = fseek (File, 0L, SEEK_END); + Size = ftell (File); + Seek2 = fseek (File, 0L, SEEK_SET); + + if (Seek1 || Seek2 || (Size == -1)) { - printf ("Could not get file status for %s\n", Filename); + printf ("Could not get file size (seek) for %s\n", Filename); goto ErrorExit; } /* Allocate a buffer for the entire file */ - Size = Gbl_StatBuf.st_size; Buffer = calloc (Size, 1); if (!Buffer) { @@ -646,8 +651,8 @@ AbGetFile ( /* Read the entire file */ - Size = read (FileHandle, Buffer, Size); - if (Size == -1) + Actual = fread (Buffer, 1, Size, File); + if (Actual != Size) { printf ("Could not read the input file %s\n", Filename); free (Buffer); @@ -658,8 +663,7 @@ AbGetFile ( *FileSize = Size; ErrorExit: - close (FileHandle); - + fclose (File); return (Buffer); } diff --git a/source/tools/acpibin/acpibin.h b/source/tools/acpibin/acpibin.h index 6769c8978..594f65077 100644 --- a/source/tools/acpibin/acpibin.h +++ b/source/tools/acpibin/acpibin.h @@ -117,30 +117,15 @@ #include "accommon.h" #include -#include -#include #include -#include #include -#ifdef WIN32 -#include -#include -#include -#endif - -/* O_BINARY is not always defined */ -#ifndef O_BINARY -#define O_BINARY 0x0 -#endif - #define DB_CONSOLE_OUTPUT 0x02 #define ACPI_DB_REDIRECTABLE_OUTPUT 0x01 /* * Global variables. Defined in main.c only, externed in all other files */ - #ifdef _DECLARE_GLOBALS #define EXTERN #define INIT_GLOBAL(a,b) a=b diff --git a/source/tools/acpisrc/acpisrc.h b/source/tools/acpisrc/acpisrc.h index af46c8da9..b54fbc988 100644 --- a/source/tools/acpisrc/acpisrc.h +++ b/source/tools/acpisrc/acpisrc.h @@ -113,35 +113,19 @@ * *****************************************************************************/ - -#define LINES_IN_LEGAL_HEADER 105 /* See above */ -#define LEGAL_HEADER_SIGNATURE " * 2.1. This is your license from Intel Corp. under its intellectual property" -#define LINES_IN_LINUX_HEADER 34 -#define LINUX_HEADER_SIGNATURE " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS" -#define LINES_IN_ASL_HEADER 29 /* Header as output from disassembler */ - #include "acpi.h" #include "accommon.h" #include #include -#include -#include -#include -#ifdef WIN32 -#include -#include -#endif #include +/* mkdir/strlwr support */ -/* O_BINARY is not always defined */ -#ifndef O_BINARY -#define O_BINARY 0x0 -#endif +#ifdef WIN32 +#include -/* Fixups for non-Win32 compilation */ -#ifndef WIN32 +#else #define mkdir(x) mkdir(x, 0770) char * strlwr(char* str); #endif @@ -149,6 +133,12 @@ char * strlwr(char* str); /* Constants */ +#define LINES_IN_LEGAL_HEADER 105 /* See above */ +#define LEGAL_HEADER_SIGNATURE " * 2.1. This is your license from Intel Corp. under its intellectual property" +#define LINES_IN_LINUX_HEADER 34 +#define LINUX_HEADER_SIGNATURE " * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS" +#define LINES_IN_ASL_HEADER 29 /* Header as output from disassembler */ + #define ASRC_MAX_FILE_SIZE (1024 * 100) #define FILE_TYPE_SOURCE 1 diff --git a/source/tools/acpisrc/asfile.c b/source/tools/acpisrc/asfile.c index 504b9a616..885a1e417 100644 --- a/source/tools/acpisrc/asfile.c +++ b/source/tools/acpisrc/asfile.c @@ -770,24 +770,32 @@ AsGetFile ( char **FileBuffer, UINT32 *FileSize) { - - int FileHandle; + FILE *File; UINT32 Size; char *Buffer; + int Seek1; + int Seek2; + size_t Actual; /* Binary mode leaves CR/LF pairs */ - FileHandle = open (Filename, O_BINARY | O_RDONLY); - if (!FileHandle) + File = fopen (Filename, "rb"); + if (!File) { printf ("Could not open %s\n", Filename); return (-1); } - if (fstat (FileHandle, &Gbl_StatBuf)) + /* Need file size to allocate a buffer */ + + Seek1 = fseek (File, 0L, SEEK_END); + Size = ftell (File); + Seek2 = fseek (File, 0L, SEEK_SET); + + if (Seek1 || Seek2 || (Size == -1)) { - printf ("Could not get file status for %s\n", Filename); + printf ("Could not get file size for %s\n", Filename); goto ErrorExit; } @@ -795,7 +803,6 @@ AsGetFile ( * Create a buffer for the entire file * Add plenty extra buffer to accommodate string replacements */ - Size = Gbl_StatBuf.st_size; Gbl_TotalSize += Size; Buffer = calloc (Size * 2, 1); @@ -807,15 +814,16 @@ AsGetFile ( /* Read the entire file */ - Size = read (FileHandle, Buffer, Size); - if (Size == -1) + Actual = fread (Buffer, 1, Size, File); + if (Actual != Size) { - printf ("Could not read the input file %s\n", Filename); + printf ("Could not read the input file %s (%u bytes)\n", + Filename, Size); goto ErrorExit; } Buffer [Size] = 0; /* Null terminate the buffer */ - close (FileHandle); + fclose (File); /* Check for unix contamination */ @@ -829,13 +837,12 @@ AsGetFile ( *FileBuffer = Buffer; *FileSize = Size; - return (0); ErrorExit: - close (FileHandle); + fclose (File); return (-1); } @@ -855,15 +862,14 @@ AsPutFile ( char *FileBuffer, UINT32 SystemFlags) { + FILE *File; UINT32 FileSize; - int DestHandle; - int OpenFlags; + size_t Actual; + int Status = 0; /* Create the target file */ - OpenFlags = O_TRUNC | O_CREAT | O_WRONLY | O_BINARY; - if (!(SystemFlags & FLG_NO_CARRIAGE_RETURNS)) { /* Put back the CR before each LF */ @@ -871,8 +877,8 @@ AsPutFile ( AsInsertCarriageReturns (FileBuffer); } - DestHandle = open (Pathname, OpenFlags, S_IREAD | S_IWRITE); - if (DestHandle == -1) + File = fopen (Pathname, "w+b"); + if (!File) { perror ("Could not create destination file"); printf ("Could not create destination file \"%s\"\n", Pathname); @@ -882,11 +888,13 @@ AsPutFile ( /* Write the buffer to the file */ FileSize = strlen (FileBuffer); - if (write (DestHandle, FileBuffer, FileSize) == -1) + Actual = fwrite (FileBuffer, 1, FileSize, File); + if (Actual != FileSize) { printf ("Error writing output file \"%s\"\n", Pathname); + Status = -1; } - close (DestHandle); - return (0); + fclose (File); + return (Status); } -- cgit v1.2.1