]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/zwgc/file.c
r4264@bucket (orig r254): kcr | 2008-01-20 22:11:44 -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     if (result = getenv("HOME"))
47       return(result);
48
49     if (!(passwd_entry = getpwuid(getuid())))
50       return(NULL);
51
52     return(passwd_entry->pw_dir);
53 }
54
55 /*
56  *
57  */
58
59 FILE *locate_file(char *override_filename,
60                   char *home_dir_filename,
61                   char *fallback_filename)
62 {
63     char *filename;
64     FILE *result;
65
66     errno = 0;
67
68     if (override_filename) {
69         if (string_Eq(override_filename, "-"))
70           return(stdin);
71         
72         result = fopen(override_filename, "r");
73         if (!(result = fopen(override_filename, "r"))) {
74             /* <<<>>> */
75             fprintf(stderr, "zwgc: error while opening %s for reading: ",
76                    override_filename);
77             perror("");
78         }
79         return(result);
80     }
81
82     if (home_dir_filename) {
83         if (filename = get_home_directory()) {
84             filename = string_Concat(filename, "/");
85             filename = string_Concat2(filename, home_dir_filename);
86             result = fopen(filename, "r");
87             if (result) {
88                 free(filename);
89                 return(result);
90             }
91             if (errno != ENOENT) {
92                 /* <<<>>> */
93                 fprintf(stderr, "zwgc: error while opening %s for reading: ",
94                         filename);
95                 perror("");
96                 free(filename);
97                 return(result);
98             }
99             free(filename);
100         } else
101           ERROR("unable to find your home directory.\n");
102     }
103
104     if (fallback_filename) {
105         if (!(result = fopen(fallback_filename, "r"))) {
106             /* <<<>>> */
107             fprintf(stderr, "zwgc: error while opening %s for reading: ",
108                    fallback_filename);
109             perror("");
110         }
111         return(result);
112     }
113
114     return(NULL);
115 }