~ubuntu-branches/ubuntu/breezy/garlic/breezy

« back to all changes in this revision

Viewing changes to open_cfg_file.c

  • Committer: Bazaar Package Importer
  • Author(s): zhaoway
  • Date: 2001-04-24 07:09:13 UTC
  • Revision ID: james.westby@ubuntu.com-20010424070913-uzpupnwdfhmliebz
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 Damir Zucic */
 
2
 
 
3
/*=============================================================================
 
4
 
 
5
                                open_cfg_file.c
 
6
 
 
7
Purpose:
 
8
        Open configuration file. Up to five directories are searched.
 
9
        If all attempts fail, return NULL.
 
10
 
 
11
Input:
 
12
        (1) Environment variable HOME used.
 
13
 
 
14
Output:
 
15
        (1) Return value. 
 
16
 
 
17
Return value:
 
18
        (1) File pointer, if file found.
 
19
        (2) NULL, if all attempts to find file failed.
 
20
 
 
21
========includes:============================================================*/
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include "defines.h"
 
27
 
 
28
/*======try to find and open .garlicrc file:=================================*/
 
29
 
 
30
FILE *OpenConfigFile_ (void)
 
31
{
 
32
static FILE     *fileP;
 
33
char            tildaA[10] = "~";
 
34
char            *home_dirP;
 
35
int             n;
 
36
char            config_file_nameA[STRINGSIZE];
 
37
 
 
38
/* The first attempt - try to find .garlicrc file in current directory: */
 
39
if ((fileP = fopen (".garlicrc", "r")) != NULL) return fileP;
 
40
 
 
41
/* The second attempt - try to find .garlicrc file in users home directory: */
 
42
/** Prepare file name: **/
 
43
do
 
44
        {
 
45
        if ((home_dirP = getenv ("HOME")) != NULL) break;
 
46
        if ((home_dirP = getenv ("home")) != NULL) break;
 
47
        home_dirP = tildaA;
 
48
        } while (0);
 
49
n = STRINGSIZE - 100;
 
50
strncpy (config_file_nameA, home_dirP, n);
 
51
config_file_nameA[n] = '\0';
 
52
strncat (config_file_nameA, "/.garlicrc", 11);
 
53
config_file_nameA[STRINGSIZE - 1] = '\0';
 
54
 
 
55
/** Try to open file: **/
 
56
if ((fileP = fopen (config_file_nameA, "r")) != NULL) return fileP;
 
57
 
 
58
/* The third attempt - try to find personal file in $HOME/garlic directory: */
 
59
n = STRINGSIZE - 100;
 
60
strncpy (config_file_nameA, home_dirP, n);
 
61
config_file_nameA[n] = '\0';
 
62
strncat (config_file_nameA, "/garlic/.garlicrc", 18);
 
63
config_file_nameA[STRINGSIZE - 1] = '\0';
 
64
 
 
65
/** Try to open file: **/
 
66
if ((fileP = fopen (config_file_nameA, "r")) != NULL) return fileP;
 
67
 
 
68
/* The fourth attempt - try with public .garlicrc file: */
 
69
if ((fileP = fopen ("/usr/local/lib/garlic/.garlicrc", "r")) != NULL)
 
70
        {
 
71
        return fileP;
 
72
        }
 
73
 
 
74
/* The last (fifth) attempt (try to find public .garlicrc file): */
 
75
if ((fileP = fopen ("/usr/lib/garlic/.garlicrc", "r")) != NULL) return fileP;
 
76
 
 
77
/* If this point is reached, all five attempts to open file failed: */
 
78
return NULL;
 
79
}
 
80
 
 
81
/*===========================================================================*/
 
82
 
 
83