/*-------------------------------------------------------------------------*/ /** @file gzip_i.c @author N. Devillard @date Aug 1999 @version $Revision: 1.6 $ @brief Interfaces to gzip through popen(). This module implements a simplistic interface to gzipped files, using Unix's popen() and a locally installed version of the gzip program to do the job. */ /*--------------------------------------------------------------------------*/ /* $Id: gzip_i.c,v 1.6 2002/01/15 10:05:06 ndevilla Exp $ $Author: ndevilla $ $Date: 2002/01/15 10:05:06 $ $Revision: 1.6 $ */ /*--------------------------------------------------------------------------- Includes ---------------------------------------------------------------------------*/ #include "gzip_i.h" /*--------------------------------------------------------------------------- Defines ---------------------------------------------------------------------------*/ /* * The magic number for a gzipped file is defined by the first two * bytes, which should be equal to resp. 1f and 8b. */ #define GZIP_MAGICNUM_1 0x1f #define GZIP_MAGICNUM_2 0x8b /*--------------------------------------------------------------------------- Function codes ---------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------*/ /** @brief Find out if a given file name corresponds to a gzipped file. @param filename Name of the file to check out. @return 1 if the file is gzipped, 0 if not, -1 if error. This function finds out if a given character string is the name of a valid gzipped file. Be careful that the returned value is more than a true/false flag, it can also be -1 if an error occurred (e.g. the given file name does not correspond to any existing file). A gzipped file is recognized by the fact that the first two bytes in the files are respectively 0x1f and 0x8b (magic number, according to gzip documentation). */ /*--------------------------------------------------------------------------*/ int is_gzipped_file(char * filename) { FILE * f ; unsigned char c[2] ; int ret ; if ((f=fopen(filename, "r"))==NULL) return -1 ; ret=0 ; if (fread(&c[0], sizeof(char), 2, f)==2) if ((c[0]==GZIP_MAGICNUM_1) && (c[1]==GZIP_MAGICNUM_2)) ret=1 ; fclose(f) ; return ret ; } /*-------------------------------------------------------------------------*/ /** @brief Open a gzipped file in read-only mode as if uncompressed. @param name Name of the gzipped file to open. @return FILE pointer to the opened file. This function calls gzip through a named pipe to get the uncompressed bytes. This means that the 'gzip' command must be installed on the local system and available in the user's path. The received bytes are read-only and sequential. The returned FILE pointer corresponds to a non-seekable file because it is created by a popen() call. The returned FILE pointer must be closed using gzclose(). */ /*--------------------------------------------------------------------------*/ FILE * gzopen(char * name) { FILE * p ; char * cmd ; cmd = malloc((int)strlen(name) + 20) ; sprintf(cmd, "gzip -cd %s", name) ; p = popen(cmd, "r") ; free(cmd) ; if (p==NULL) { fprintf(stderr, "cannot open gzipped file: is gzip installed?\n") ; } return p ; } /*-------------------------------------------------------------------------*/ /** @brief Close out named pipe associated to gzipped open file. @param p FILE pointer to close. @return void Calls pclose() on the given FILE pointer. */ /*--------------------------------------------------------------------------*/ void gzclose(FILE * p) { pclose(p) ; } /* vim: set ts=4 et sw=4 tw=75 */