1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/* 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$
* $Author$
*
* Copyright (c) 1989 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#if (!defined(lint) && !defined(SABER))
static char rcsid_file_c[] = "$Id$";
#endif
#include <zephyr/mit-copyright.h>
#include <errno.h>
#include <sys/types.h>
#include <stdio.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)
char *override_filename;
char *home_dir_filename;
char *fallback_filename;
{
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("");
}
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");
if (result) {
free(filename);
return(result);
}
if (errno != ENOENT) {
/* <<<>>> */
fprintf(stderr, "zwgc: error while opening %s for reading: ",
filename);
perror("");
free(filename);
return(result);
}
free(filename);
} else
ERROR("unable to find your home directory.\n");
}
if (fallback_filename) {
if (!(result = fopen(fallback_filename, "r"))) {
/* <<<>>> */
fprintf(stderr, "zwgc: error while opening %s for reading: ",
fallback_filename);
perror("");
}
return(result);
}
return(NULL);
}
|