File: open_config_file.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (117 lines) | stat: -rw-r--r-- 3,101 bytes parent folder | download | duplicates (4)
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
/* Copyright (C) 2000-2006 Damir Zucic */

/*=============================================================================

				open_cfg_file.c

Purpose:
	Open configuration file. Up to seven directories are searched. If all
	attempts fail, return NULL.

Input:
	(1) Environment variable HOME used.

Output:
	(1) Return value. 

Return value:
	(1) File pointer, if file found.
	(2) NULL, if all attempts to find file failed.

Notes:
	(1) The following seven pathnames will be checked, if necessary:

	    (1) ./.garlicrc		    (current working directory)
	    (2) $HOME/.garlicrc		    (home directory)
	    (3) $HOME/garlic/.garlicrc
	    (4) /usr/share/garlic/.garlicrc (recomm.  system-wide default!)
	    (5) /etc/garlicrc               (note: file name is garlicrc !)
	    (6) /usr/local/lib/garlic/.garlicrc
	    (7) /usr/lib/garlic/.garlicrc

========includes:============================================================*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h"

/*======try to find and open .garlicrc file:=================================*/

FILE *OpenConfigFile_ (void)
{
static FILE	*fileP;
static char	tildaA[10] = "~";
char		*home_dirP;
int		n;
char		config_file_nameA[STRINGSIZE];

/* The first attempt - try to find .garlicrc file in current directory: */
if ((fileP = fopen (".garlicrc", "r")) != NULL)
	{
	return fileP;
	}

/* The second attempt - try to find .garlicrc file in users home directory: */
do
	{
	if ((home_dirP = getenv ("HOME")) != NULL) break;
	if ((home_dirP = getenv ("home")) != NULL) break;
	home_dirP = tildaA;
	} while (0);

n = STRINGSIZE - 100;
strncpy (config_file_nameA, home_dirP, n);
config_file_nameA[n] = '\0';
strncat (config_file_nameA, "/.garlicrc", 11);
config_file_nameA[STRINGSIZE - 1] = '\0';
if ((fileP = fopen (config_file_nameA, "r")) != NULL)
	{
	return fileP;
	}

/* The third attempt - try to find personal file in $HOME/garlic directory: */
n = STRINGSIZE - 100;
strncpy (config_file_nameA, home_dirP, n);
config_file_nameA[n] = '\0';
strncat (config_file_nameA, "/garlic/.garlicrc", 18);
config_file_nameA[STRINGSIZE - 1] = '\0';
if ((fileP = fopen (config_file_nameA, "r")) != NULL)
	{
	return fileP;
	}

/* The fourth attempt  -  /usr/share/garlic/.garlicrc . */
/* This pathname is recommended as system-wide default! */
if ((fileP = fopen ("/usr/share/garlic/.garlicrc", "r")) != NULL)
        {
        return fileP;
        }

/* The fifth attempt - /etc/garlicrc, default for */
/* Debian Linux  (the file name  is  garlicrc !): */
if ((fileP = fopen ("/etc/garlicrc", "r")) != NULL)
	{
	return fileP;
	}

/* The sixth attempt - /usr/local/lib/garlic directory: */
if ((fileP = fopen ("/usr/local/lib/garlic/.garlicrc", "r")) != NULL)
	{
	return fileP;
	}

/* The seventh attempt - /usr/lib/garlic directory: */
if ((fileP = fopen ("/usr/lib/garlic/.garlicrc", "r")) != NULL)
	{
	return fileP;
	}

/* If this point is reached, all attempts to */
/* open the configuration file  have failed: */
return NULL;
}

/*===========================================================================*/