/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Source: /mit/zephyr/repository/zephyr/clients/zwgc/file.c,v $
 *      $Author: ghudson $
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_file_c[] = "$Id: file.c,v 1.5 1995/07/07 21:59:34 ghudson Exp $";
#endif

#include <zephyr/mit-copyright.h>

#include <pwd.h>
#include "new_memory.h"
#include "new_string.h"
#include "error.h"

/*
 *    char *get_home_directory()
 *
 *        Effects: Attempts to locate the user's (by user, the owner of this
 *                 process is meant) home directory & return its pathname.
 *                 Returns NULL if unable to do so.  Does so by first checking
 *                 the environment variable HOME.  If it is unset, falls back
 *                 on the user's password entry.
 *         Note: The returned pointer may point to a static buffer & hence
 *               go away on further calls to getenv, get_home_directory,
 *               getpwuid, or related calls.  The caller should copy it
 *               if necessary.
 */

char *get_home_directory()
{
    char *result;
    char *getenv();
    struct passwd *passwd_entry;

    if (result = getenv("HOME"))
      return(result);

    if (!(passwd_entry = getpwuid(getuid())))
      return(NULL);

    return(passwd_entry->pw_dir);
}

/*
 *
 */

FILE *locate_file(override_filename, home_dir_filename, fallback_filename, resultfile)
     char *override_filename;
     char *home_dir_filename;
     char *fallback_filename;
     char *resultfile;

{
    char *filename;
    FILE * result;

    errno = 0;
    if (override_filename) {
	if (string_Eq(override_filename, "-"))
	  return(stdin);
	
	result = fopen(override_filename, "r");
	if (!(result = fopen(override_filename, "r"))) {
	    /* <<<>>> */
	    fprintf(stderr, "zwgc: error while opening %s for reading: ",
		   override_filename);
	    perror("");
	}
	strcpy(resultfile, override_filename);
	return(result);
    }

    if (home_dir_filename) {
	if (filename = get_home_directory()) {
	    filename = string_Concat(filename, "/");
	    filename = string_Concat2(filename, home_dir_filename);
	    result = fopen(filename, "r");
	    strcpy(resultfile, filename);
	    if (result != NULL) {
		return(result);
	    }
	    if (errno != ENOENT) {
		/* <<<>>> */
		fprintf(stderr, "zwgc: error while opening %s for reading: ",
			filename);
		perror("");
		return(result);
	    }
	    free(filename);
	} else
	  ERROR("unable to find your home directory.\n");
    }
    if (fallback_filename != NULL) {
	if (!(result = fopen(fallback_filename, "r"))) {
	    /* <<<>>> */
	    fprintf(stderr, "zwgc: error while opening %s for reading: ",
		   fallback_filename);
	    perror("");
	}
	strcpy(resultfile, fallback_filename);
	return(result);
    }

    return(NULL);
}
