00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "mit-copyright.h"
00010
00011 #include <pwd.h>
00012 #include "main.h"
00013 #include "new_string.h"
00014 #include "error.h"
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 char *
00031 get_home_directory()
00032 {
00033 char *result;
00034 char *getenv();
00035 struct passwd *passwd_entry;
00036
00037 if ((result = getenv("HOME")))
00038 return (result);
00039
00040 if (!(passwd_entry = getpwuid(getuid())))
00041 return (NULL);
00042
00043 return (passwd_entry->pw_dir);
00044 }
00045
00046
00047
00048
00049
00050 FILE *
00051 locate_file(override_filename, home_dir_filename, fallback_filename)
00052 char *override_filename;
00053 char *home_dir_filename;
00054 char *fallback_filename;
00055 {
00056 char *filename;
00057 FILE *result;
00058
00059 errno = 0;
00060
00061 if (override_filename) {
00062 if (string_Eq(override_filename, "-"))
00063 return (stdin);
00064
00065 result = fopen(override_filename, "r");
00066 if (!(result = fopen(override_filename, "r"))) {
00067
00068 fprintf(stderr, "jwgc: error while opening %s for reading: ",
00069 override_filename);
00070 perror("");
00071 }
00072 return (result);
00073 }
00074
00075 if (home_dir_filename) {
00076 if ((filename = get_home_directory())) {
00077 filename = string_Concat(filename, "/");
00078 filename = string_Concat2(filename, home_dir_filename);
00079 result = fopen(filename, "r");
00080 if (result) {
00081 free(filename);
00082 return (result);
00083 }
00084 if (errno != ENOENT) {
00085 fprintf(stderr, "jwgc: error while opening %s for reading: ",
00086 filename);
00087 perror("");
00088 free(filename);
00089 return (result);
00090 }
00091 free(filename);
00092 }
00093 else
00094 ERROR("unable to find your home directory.\n");
00095 }
00096
00097 if (fallback_filename) {
00098 if (!(result = fopen(fallback_filename, "r"))) {
00099
00100 fprintf(stderr, "jwgc: error while opening %s for reading: ",
00101 fallback_filename);
00102 perror("");
00103 }
00104 return (result);
00105 }
00106
00107 return (NULL);
00108 }