getconfent.c 4.82 KB
Newer Older
Olof Barring's avatar
Olof Barring committed
1
/*
2
 * Copyright (C) 1991-1999 by CERN/IT/PDP/DM
Olof Barring's avatar
Olof Barring committed
3
4
5
 * All rights reserved
 */

6
#include <stdio.h>
7
#include <stdlib.h>
8
#include <string.h>
9
10
11
#include "Cglobals.h"
#include "serrno.h"
#include "getconfent.h"
Jean-Damien Durand's avatar
Jean-Damien Durand committed
12
13
#include "Csnprintf.h"
#include "Castor_limits.h"
Jean-Damien Durand's avatar
Jean-Damien Durand committed
14
15

#ifndef PATH_CONFIG
16
#define PATH_CONFIG "/etc/castor/castor.conf"
Jean-Damien Durand's avatar
Jean-Damien Durand committed
17
18
#endif /* PATH_CONFIG */

19
#if (defined(_REENTRANT) || defined(_THREAD_SAFE))
20
21
#define strtok(X,Y) strtok_r(X,Y,&last)
#endif /* _REENTRANT || _THREAD_SAFE */
Jean-Damien Durand's avatar
Jean-Damien Durand committed
22
23


Sebastien Ponce's avatar
Sebastien Ponce committed
24
static char *getconfent_r _PROTO((const char *, const char *, const char *, int, char *, int));
25
26

static char *getconfent_r(filename,category, name, flags, buffer, bufsiz)
Sebastien Ponce's avatar
Sebastien Ponce committed
27
28
29
     const char *filename;
     const char *category;
     const char *name;
30
31
32
33
     int flags;
     char *buffer;
     int bufsiz;
{
34
35
36
37
    FILE    *fp;
    char    *p, *cp;
    char    *getenv();
    int     found = 0;
Jean-Damien Durand's avatar
Jean-Damien Durand committed
38
    char    path_config[CA_MAXPATHLEN+1];
39
40
41
42
43
    char    *separator;
#if defined(_REENTRANT) || defined(_THREAD_SAFE)
    char *last = NULL;
#endif /* _REENTRANT || _THREAD_SAFE */

44
45
46
47
48
    if (filename == NULL) {
      /* Use default config file is not in the parameters */
      filename = PATH_CONFIG;
      /* But give precedence to $PATH_CONFIG environment variable */
      if ((p = getenv("PATH_CONFIG")) != NULL) {
49
        filename=p;
50
      }
51
    }
52

53
    strncpy (path_config, filename, CA_MAXPATHLEN);
Jean-Damien Durand's avatar
Jean-Damien Durand committed
54
55
    /* Who knows */
    path_config[CA_MAXPATHLEN] = '\0';
Jean-Damien Durand's avatar
Jean-Damien Durand committed
56

57
58
59
60
61
62
    if ((fp = fopen(path_config,"r")) == NULL) {
        serrno = SENOCONFIG;
        return (NULL);
    }

    for (;;) {
63
        p = fgets(buffer, bufsiz-1, fp);
64
65
        if (p == NULL) {
            break;
Jean-Damien Durand's avatar
Jean-Damien Durand committed
66
67
        }

68
69
70
71
72
73
74
75
76
77
78
        if ( (cp = strtok(p," \t")) == NULL) continue; /* empty line */
        if (*cp == '#') continue; /* comment */
        if (strcmp(cp,category) == 0) { /* A category match */
            if ( (cp = strtok(NULL," \t")) == NULL ) continue;
            if (*cp == '#') continue; /* comment, invalid */
            if ( strcmp(cp,name) == 0) { /* A match */
                if (flags != 0) {
                    /* Don't tokenize next arg */
                    separator = "#\n";
                } else {
                    separator = "#\t \n";
Jean-Damien Durand's avatar
Jean-Damien Durand committed
79
                }
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
                if ( (cp = strtok(NULL, separator)) == NULL ) continue;
                if (*cp == '#') continue;
                found++;
                break;
            }
            else {
                continue;
            }
        } else {
            continue;
        }
    }
    if (fclose(fp)) return(NULL);
    if (found == 0) return(NULL);
    else return(cp);
96
97
98
99
}

static int value_key = -1;

100
char DLL_DECL *getconfent(category, name, flags)
Sebastien Ponce's avatar
Sebastien Ponce committed
101
102
     const char *category;
     const char *name;
103
104
     int flags;
{
105
    char *value = NULL;
106

107
108
109
110
    Cglobals_get(&value_key,(void **) &value,BUFSIZ+1);
    if ( value == NULL ) {
        return(NULL);
    }
111

112
113
114
115
    return(getconfent_r(NULL,category,name,flags,value,BUFSIZ+1));
}

char DLL_DECL *getconfent_fromfile(filename,category, name, flags)
Sebastien Ponce's avatar
Sebastien Ponce committed
116
117
118
     const char *filename;
     const char *category;
     const char *name;
119
120
121
122
123
124
125
126
127
128
     int flags;
{
    char *value = NULL;

    Cglobals_get(&value_key,(void **) &value,BUFSIZ+1);
    if ( value == NULL ) {
        return(NULL);
    }

    return(getconfent_r(filename,category,name,flags,value,BUFSIZ+1));
Jean-Damien Durand's avatar
Jean-Damien Durand committed
129
}
130
131
132
133

int DLL_DECL getconfent_parser(conf_val, result, count)
     char **conf_val;
     char ***result;
134
     int *count;
135
136
137
138
139
140
141
{
  char *p,*q,*last;
  int i=0;

  /* Counting the number of strings for the array */
  if ((p = strdup(*conf_val)) == NULL) { return -1; }
  for (q = strtok(p," \t"); q != NULL; q = strtok(NULL," \t")) i++;
142
143
  free(p);

144
  /* Saving the index information to pass on later */
145
  *count = i;
146
147
148
149
150
151
152
153
  
  /* Allocating the necessary space and parsing the string */
  if ((p = strdup(*conf_val)) == NULL) { return -1; }
  (*result) = (char **)calloc((i+1), sizeof(char *));
  if (result == NULL) { return -1; }
 
  i = 0 ;
  for (q = strtok(p," \t");q != NULL; q = strtok(NULL," \t")) { (*result)[i++] = strdup(q); }
154
155
  free(p);

156
157
158
159
  return 0;
}

int DLL_DECL getconfent_multi_fromfile(filename, category, name, flags, result, count)
Sebastien Ponce's avatar
Sebastien Ponce committed
160
161
162
     const char *filename;
     const char *category;
     const char *name;
163
164
     int flags;
     char ***result;
165
     int *count;
166
167
168
{
  char *conf_val;

169
170
171
172
173
  if((conf_val = getconfent_fromfile(filename,category,name,flags)) == NULL){ 
    *result = NULL;
    *count = 0;
    return 0; 
  }
174
175
176
177
178
179
180
181
182
 
  if ( getconfent_parser(&conf_val, result, count) == -1 ) {return -1;}

  return 0;
}



int DLL_DECL getconfent_multi(category, name, flags, result, count)
Sebastien Ponce's avatar
Sebastien Ponce committed
183
184
     const char *category;
     const char *name;
185
186
     int flags;
     char ***result;
187
     int *count;
188
189
190
{
  char *conf_val;
  
191
192
193
194
195
  if((conf_val = getconfent(category,name,flags)) == NULL) { 
    *result = NULL;
    *count = 0;
    return 0; 
  }
196
197
198
199
200
  
  if( getconfent_parser(&conf_val, result, count) == -1 ) {return -1;}

  return 0;
}