]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/file.c
r4275@bucket (orig r265): kcr | 2008-01-21 02:57:32 -0500
[1ts-debian.git] / zephyr / zwgc / file.c
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
3  * client.
4  *
5  *      Created by:     Marc Horowitz <marc@athena.mit.edu>
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h".
12  */
13
14 #include <sysdep.h>
15
16 #if (!defined(lint) && !defined(SABER))
17 static const char rcsid_file_c[] = "$Id$";
18 #endif
19
20 #include <zephyr/mit-copyright.h>
21
22 #include <pwd.h>
23 #include "new_memory.h"
24 #include "new_string.h"
25 #include "error.h"
26
27 /*
28  *    char *get_home_directory()
29  *
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
38  *               if necessary.
39  */
40
41 char *get_home_directory(void)
42 {
43     char *result;
44     struct passwd *passwd_entry;
45
46     result = getenv("HOME");
47     if (result)
48       return(result);
49
50     if (!(passwd_entry = getpwuid(getuid())))
51       return(NULL);
52
53     return(passwd_entry->pw_dir);
54 }
55
56 /*
57  *
58  */
59
60 FILE *locate_file(char *override_filename,
61                   char *home_dir_filename,
62                   char *fallback_filename)
63 {
64     char *filename;
65     FILE *result;
66
67     errno = 0;
68
69     if (override_filename) {
70         if (string_Eq(override_filename, "-"))
71           return(stdin);
72         
73         result = fopen(override_filename, "r");
74         if (!(result = fopen(override_filename, "r"))) {
75             /* <<<>>> */
76             fprintf(stderr, "zwgc: error while opening %s for reading: ",
77                    override_filename);
78             perror("");
79         }
80         return(result);
81     }
82
83     if (home_dir_filename) {
84         filename = get_home_directory();
85         if (filename) {
86             filename = string_Concat(filename, "/");
87             filename = string_Concat2(filename, home_dir_filename);
88             result = fopen(filename, "r");
89             if (result) {
90                 free(filename);
91                 return(result);
92             }
93             if (errno != ENOENT) {
94                 /* <<<>>> */
95                 fprintf(stderr, "zwgc: error while opening %s for reading: ",
96                         filename);
97                 perror("");
98                 free(filename);
99                 return(result);
100             }
101             free(filename);
102         } else
103           ERROR("unable to find your home directory.\n");
104     }
105
106     if (fallback_filename) {
107         if (!(result = fopen(fallback_filename, "r"))) {
108             /* <<<>>> */
109             fprintf(stderr, "zwgc: error while opening %s for reading: ",
110                    fallback_filename);
111             perror("");
112         }
113         return(result);
114     }
115
116     return(NULL);
117 }