/**************************************************************************** * fileutils.c * * * * LocalGate Local Filesystem -> WWW Gateway * * * * LocalGate allows a user to access HTML files on the local filesystem * * as if they had a web server to access the files. * * * * fileutils.c: file access routines for LocalGate. * * * * Copyright (C) 1997 Michael Chu * * This file is part of the LocalGate local filesystem -> WWW gateway. * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the Free Software * * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * * * Contact information: Michael Chu * * mmchu@pobox.com * ****************************************************************************/ /*********************************** INCLUDES *******************************/ #include #include #include #include #include "fileutils.h" /********************************** FUNCTIONS *******************************/ /**************************************************************************** * readFromFile: * * * * Summary: Reads the given number of bytes from the given file into the * * given buffer. * * * * Input: fileId: file id to read from. * * buffer: buffer to which to write to. * * maxBytes: maximum number of bytes to read from file. * * * * Returns: Actual number of bytes read from the file. * * * * Global State Affected: * * Given number of bytes is read from the given file. * ****************************************************************************/ int readFromFile(int fileId, char *buffer, int maxBytes) { int numBytesRead = 0; /* number of bytes read. */ /* make sure that file id is non-negative. */ assert(fileId >= 0); /* make sure that buffer is not NULL. */ assert(buffer != NULL); /* make sure that number of bytes to read is non-negative. */ assert(maxBytes >= 0); /* try to read bytes. */ numBytesRead = read(fileId, buffer, maxBytes); /* if we had an error reading from the file, show it. */ if (numBytesRead < 0) { /* show error. */ fprintf(stderr, "Error reading from file! (errno = %d)\n", errno); /* return error. */ return(-1); } /* return the number of bytes read. */ return(numBytesRead); } /**************************************************************************** * readFromFileOffset: * * * * Summary: Reads the given number of bytes from the given file at the * * given offset into the given buffer. * * * * Input: fileId: file id to read from. * * fileOffset: file offset to read from. * * buffer: buffer to which to write to. * * maxBytes: maximum number of bytes to read from file. * * * * Returns: Actual number of bytes read from the file. * * * * Global State Affected: * * Given number of bytes is read from the given file. * ****************************************************************************/ int readFromFileOffset(int fileId, long fileOffset, char *buffer, int maxBytes) { int numBytesRead = 0; /* number of bytes read. */ /* make sure that file id is non-negative. */ assert(fileId >= 0); /* make sure that file offset is non-negative. */ assert(fileOffset >= 0); /* make sure that buffer is not NULL. */ assert(buffer != NULL); /* make sure that number of bytes to read is non-negative. */ assert(maxBytes >= 0); /* try to seek to given file offset. */ if ((lseek(fileId, fileOffset, SEEK_SET)) < 0) { /* show error. */ fprintf(stderr, "Error seeking to location %ld in file! (errno = %d)\n", fileOffset, errno); /* return error. */ return(-1); } /* try to read bytes. */ numBytesRead = read(fileId, buffer, maxBytes); /* if we had an error reading from the file, show it. */ if (numBytesRead < 0) { /* show error. */ fprintf(stderr, "Error reading from file! (errno = %d)\n", errno); /* return error. */ return(-1); } /* return the number of bytes read. */ return(numBytesRead); }