"Fossies" - the Fresh Open Source Software Archive

Member "libgcgi.a-0.9.5/src/common.c" (22 Jun 2002, 2644 Bytes) of package /linux/www/old/gcgi-0.9.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.c" see the Fossies "Dox" file reference documentation.

    1 /* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
    2 /*
    3  * GCGI Library, implementing NCSA'a Common Gateway Interface and RFC2338.
    4  * Copyright (C) 2001-2002 Julian Catchen, julian@catchen.org
    5  *
    6  * This library is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU Lesser General Public
    8  * License as published by the Free Software Foundation; either
    9  * version 2.1 of the License, or (at your option) any later version.
   10  *
   11  * This library is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14  * Lesser General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU Lesser General Public
   17  * License along with this library; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   19  */
   20 
   21 #include "common.h"
   22 
   23 FILE * 
   24 tempFile(char *prefix, char **filename, int *fd) 
   25 {
   26   FILE        *retFile;
   27   char        *tmpdir  = NULL;
   28   static char *pattern = NULL;
   29   mode_t       old_mode;
   30 
   31   if (! (tmpdir = getenv("TMPDIR"))) 
   32     tmpdir = getenv("TMP");
   33 
   34   if (! tmpdir) tmpdir = "/tmp";
   35   
   36   pattern = XMALLOC(char, (strlen(tmpdir)+strlen(prefix)+8));
   37 
   38   strcpy(pattern,tmpdir);
   39   strcat(pattern,"/");
   40   strcat(pattern, prefix);
   41   strcat(pattern, "XXXXXX");
   42   
   43   old_mode = umask(077);
   44   *fd = mkstemp(pattern);
   45   umask(old_mode);
   46 
   47   if (*fd < 0) {
   48     fprintf(stderr,"Unable to open temporary file");
   49     return NULL;
   50   }
   51 
   52   if (! (retFile = fdopen(*fd, "w"))) {
   53     fprintf(stderr,"Unable to get temporary file's descriptor");
   54     return NULL;
   55   }
   56   
   57   *filename = pattern;
   58 
   59   return retFile;
   60 }
   61 
   62 
   63 char *
   64 readQueryFromStream(FILE *input, int clen)
   65 {
   66     size_t       read;
   67     off_t        offset;
   68     static char *querystring;
   69 
   70     read           = 0;
   71     offset         = 0;
   72     querystring    = XMALLOC(char, clen+1);
   73     memset(querystring, 0, clen + 1);
   74 
   75     /* Read in the query from STDIN */
   76     do {
   77     read = fread(querystring + offset, sizeof(char), clen - offset, input);
   78     if (ferror(input)) 
   79         return NULL;
   80     else if (read <= 0)
   81         continue;
   82     offset += read;
   83     } while (offset < clen && read > 0);
   84 
   85     return querystring;
   86 }
   87 
   88 
   89 void *
   90 xmalloc (size_t num)
   91 {
   92   void *new = malloc (num);
   93   if (!new) {
   94     fprintf(stderr,"Memory exhausted.\n");
   95     exit(1);
   96   }
   97   return new;
   98 }
   99 
  100 
  101 void *
  102 xrealloc (void *p, size_t num)
  103 {
  104   void *new;
  105 
  106   if (!p)
  107     return xmalloc (num);
  108 
  109   new = realloc (p, num);
  110   if (!new) {
  111     fprintf(stderr,"Memory exhausted.\n");
  112     exit(1);
  113   }
  114 
  115   return new;
  116 }