tmpfile alternative

From: zoppiat_private
Date: Wed Jan 02 2002 - 11:22:20 PST

  • Next message: Glynn Clements: "Re: tmpfile alternative"

    Hi, i've implemented this function as part of my bondlog package:
    
    #include <string.h>
    #include <ctype.h>
    #include <stdio.h>
    #include <stdarg.h>
    #include <stdlib.h>
    #include <errno.h>
    #include "bondlog.h"
    #include <time.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <pwd.h>
    
    
    #define PATH_MAX		1024
    
    
    FILE *
    mytmpfile ()
    {
    
      char appname[] = "/var/spool/bondlog/tmp-";
      char filename[PATH_MAX];
      base64buf b;
      unsigned int num = 0;
      unsigned int count = 0;
      mode_t mask;
      FILE *urand_file;
      FILE *tmp;
      unsigned char *stream;
    
      if ((stream = (char *) malloc (11)) == NULL)
        {
          return NULL;
        }
      memset (stream, '\0', 11);
    
      urand_file = fopen (RANDOMFILE, "r");
    
      if (urand_file == NULL)
        {
          free (stream);
          return NULL;
        }
    
      while (count < 9)
        {
          fread (&num, sizeof (unsigned int), 1, urand_file);
          stream[count] = (char) num;
          count++;
        }
      fclose (urand_file);
      base64_encode (&b, stream, 9);
      memset (filename, '\0', sizeof (filename));
      snprintf (filename, sizeof (filename), "%s%s", appname, b.data);
      mask = umask (0066);
      if ((tmp = fopen (filename, "wb")) == NULL)
        {
          fprintf (stderr, "Cannot create filename:%s", filename);
          free (stream);
          return NULL;
        }
      unlink (filename);		// the file will be not delete until closed;
      umask (mask);
      free (stream);
      buffer_delete (&b);
      return (tmp);
    
    }
    
    
    Could be function this considered a valid safe tempfile alternative ?
    
    Cheers,
    gz
    
    ----------------------------
    Giorgio Zoppi
    denebat_private
    



    This archive was generated by hypermail 2b30 : Wed Jan 02 2002 - 21:16:16 PST