/*  -*- c -*-
mzbase.c, part of:

muttzilla  v0.40

Copyright (C) 1999-2000 Brian D. Winters

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
*/


#include "muttzilla.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>


/* create a linked list of tempfiles to clean up on exit */
tempfile_info *head;

/* helper function for open_tempfile() */
int compare_stat(struct stat *osb, struct stat *nsb) {
  if(osb->st_dev != nsb->st_dev || osb->st_ino != nsb->st_ino ||
     osb->st_rdev != nsb->st_rdev)
    return -1;
  else
    return 0;
}

/* this function creates a tempfile
   it should not choose a name predictably or follow symlinks
   it should create the file with 600 permissions
   and ideally we should also clean up after ourselves...

   I have patterned this function after what mutt appears to do, so
   hopefully this is safe.  If you see any flaws please report them to
   me asap.

   Hmm, mktemp() is turning out to be rather predictable.  I'm not sure
   that this is a problem though, so I won't worry about it for now.

   At some point I might want to automatically retry tempfile creation
   a few times if it fails.  I want to think about this a bit more
   before I do it though.
*/
tempfile_info *open_tempfile() {
  char fn[sizeof(TEMPDIR "/muttzilla-XXXXXXXX") / sizeof(char)]
    = TEMPDIR "/muttzilla-XXXXXXXX";
  struct stat osb, nsb;
  tempfile_info *tf;

  tf = malloc(sizeof(tempfile_info));
  tf->next = NULL;
  tf->fp = NULL;
  tf->fd = -1;
  tf->filename = NULL;

  if(!mktemp(fn)) {
    if(mz_debug)
      fprintf(stderr, "muttzilla: open_tempfile(): mktemp(\"%s\") failed!\n", fn);
    return NULL;
  }

  if((tf->fd = open(fn, O_CREAT | O_EXCL | O_WRONLY, 0600)) < 0)
    return NULL;
  if(lstat(fn, &osb) < 0 || fstat(tf->fd, &nsb) < 0 ||
     compare_stat(&osb, &nsb) == -1) {
    fprintf(stderr, "muttzilla: open_tempfile(): %s is a symlink!\n", fn);
    close(tf->fd);
  } else {
    tf->fp = fdopen(tf->fd, "w");
    if(tf->fp) {
      tf->filename = malloc((strlen(fn) + 1) * sizeof(char));
      if(tf->filename)
	strcpy(tf->filename, fn);
    }
  }

  if(tf->filename) {
    tf->next = head;
    head = tf->next;
  } else {
    free(tf);
    tf = NULL;
  }

  return tf;
}



/* generic function for forking and execing a new process */
int mzspawn(char *const argv[]) {
  pid_t p;
  int i;

  if(mz_debug) {
    fprintf(stderr, "Spawning \"");
    for(i = 0; argv[i] != NULL; i++)
      fprintf(stderr, "%s ", argv[i]);
    fprintf(stderr, "\"\n");
  }

  p = fork();
  if(p == -1) {   /* error forking */
    if(mz_debug)
      fprintf(stderr, "Error forking.\n");
    return -1;
  } else if(p)    /* fork successful */
    return 0;
  else {        /* in child */
    execvp(argv[0], argv);
    exit(-1);
  }
}



syntax highlighted by Code2HTML, v. 0.9.1