1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It is one of the source files comprising zwgc, the Zephyr WindowGram
5 * Created by: Marc Horowitz <marc@athena.mit.edu>
7 * $Id: file.c,v 1.5 1999/01/22 23:20:19 ghudson Exp $
9 * Copyright (c) 1989 by the Massachusetts Institute of Technology.
10 * For copying and distribution information, see the file
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_file_c[] = "$Id: file.c,v 1.5 1999/01/22 23:20:19 ghudson Exp $";
20 #include <zephyr/mit-copyright.h>
23 #include "new_memory.h"
24 #include "new_string.h"
28 * char *get_home_directory()
30 * Effects: Attempts to locate the user's (by user, the owner of this
31 * process is meant) home directory & return its pathname.
32 * Returns NULL if unable to do so. Does so by first checking
33 * the environment variable HOME. If it is unset, falls back
34 * on the user's password entry.
35 * Note: The returned pointer may point to a static buffer & hence
36 * go away on further calls to getenv, get_home_directory,
37 * getpwuid, or related calls. The caller should copy it
41 char *get_home_directory()
45 struct passwd *passwd_entry;
47 if (result = getenv("HOME"))
50 if (!(passwd_entry = getpwuid(getuid())))
53 return(passwd_entry->pw_dir);
60 FILE *locate_file(override_filename, home_dir_filename, fallback_filename)
61 char *override_filename;
62 char *home_dir_filename;
63 char *fallback_filename;
70 if (override_filename) {
71 if (string_Eq(override_filename, "-"))
74 result = fopen(override_filename, "r");
75 if (!(result = fopen(override_filename, "r"))) {
77 fprintf(stderr, "zwgc: error while opening %s for reading: ",
84 if (home_dir_filename) {
85 if (filename = get_home_directory()) {
86 filename = string_Concat(filename, "/");
87 filename = string_Concat2(filename, home_dir_filename);
88 result = fopen(filename, "r");
93 if (errno != ENOENT) {
95 fprintf(stderr, "zwgc: error while opening %s for reading: ",
103 ERROR("unable to find your home directory.\n");
106 if (fallback_filename) {
107 if (!(result = fopen(fallback_filename, "r"))) {
109 fprintf(stderr, "zwgc: error while opening %s for reading: ",