Commit 233bddd4 authored by Steven Murray's avatar Steven Murray
Browse files

Merge branch 'getconfent.c_for_CTA_master'

Adding C implementation of getconfent from 2012.
parents d124b2fd 49fc9bfd
/*
* Copyright (C) 1991-1999 by CERN/IT/PDP/DM
* All rights reserved
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Cglobals.h"
#include "serrno.h"
#include "getconfent.h"
#include "Csnprintf.h"
#include "Castor_limits.h"
#ifndef PATH_CONFIG
#define PATH_CONFIG "/etc/castor/castor.conf"
#endif /* PATH_CONFIG */
#define strtok(X,Y) strtok_r(X,Y,&last)
static char *getconfent_r (const char *, const char *, const char *, int, char *, int);
static char *getconfent_r(const char *filename,
const char *category,
const char *name,
int flags,
char *buffer,
int bufsiz)
{
FILE *fp;
char *p, *cp;
char *getenv();
int found = 0;
char path_config[CA_MAXPATHLEN+1];
char *separator;
char *last = NULL;
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) {
filename=p;
}
}
strncpy (path_config, filename, CA_MAXPATHLEN);
/* Who knows */
path_config[CA_MAXPATHLEN] = '\0';
if ((fp = fopen(path_config,"r")) == NULL) {
serrno = SENOCONFIG;
return (NULL);
}
for (;;) {
p = fgets(buffer, bufsiz-1, fp);
if (p == NULL) {
break;
}
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";
}
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);
}
static int value_key = -1;
char *getconfent(const char *category,
const char *name,
int flags)
{
char *value = NULL;
Cglobals_get(&value_key,(void **) &value,BUFSIZ+1);
if ( value == NULL ) {
return(NULL);
}
return(getconfent_r(NULL,category,name,flags,value,BUFSIZ+1));
}
char *getconfent_fromfile(const char *filename,
const char *category,
const char *name,
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));
}
int getconfent_parser(char **conf_val,
char ***result,
int *count)
{
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++;
free(p);
/* Saving the index information to pass on later */
*count = i;
/* 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); }
free(p);
return 0;
}
int getconfent_multi_fromfile(const char *filename,
const char *category,
const char *name,
int flags,
char ***result,
int *count)
{
char *conf_val;
if((conf_val = getconfent_fromfile(filename,category,name,flags)) == NULL){
*result = NULL;
*count = 0;
return 0;
}
if ( getconfent_parser(&conf_val, result, count) == -1 ) {return -1;}
return 0;
}
int getconfent_multi(const char *category,
const char *name,
int flags,
char ***result,
int *count)
{
char *conf_val;
if((conf_val = getconfent(category,name,flags)) == NULL) {
*result = NULL;
*count = 0;
return 0;
}
if( getconfent_parser(&conf_val, result, count) == -1 ) {return -1;}
return 0;
}
.\" @(#)@(#)$RCSfile: getconfent.man,v $ $Revision: 1.9 $ $Date: 2009/07/23 12:22:02 $ CERN IT-PDP/DC Frederic Hemmer
.\" Copyright (C) 1990-2000 by CERN/IT/PDP/DC
.\" All rights reserved
.\"
.TH GETCONFENT 3 "$Date: 2009/07/23 12:22:02 $" CASTOR "Common Library Functions"
.SH NAME
getconfent, getconfent_r \- Get configuration entry
.SH SYNOPSIS
.nf
.LP
.BI "char * getconfent ("
.br
.BI " const char *" category ,
.br
.BI " const char *" name ,
.br
.BI " int " flags );
.PP
.BI "int getconfent_multi ("
.br
.BI " const char *" category ,
.br
.BI " const char *" name ,
.br
.BI " int " flags ,
.br
.BI " char ***" result ,
.br
.BI " int *" count );
.PP
.BI "char * getconfent_fromfile ("
.br
.BI " const char *" filename ,
.br
.BI " const char *" category ,
.br
.BI " const char *" name ,
.br
.BI " int " flags );
.PP
.BI "int getconfent_multi_fromfile ("
.br
.BI " const char *" filename ,
.br
.BI " const char *" category ,
.br
.BI " const char *" name ,
.br
.BI " int " flags ,
.br
.BI " char ***" result ,
.br
.BI " int *" count );
.PP
.fi
.IX "getconfent function" "" "\fLget\fP \(em configuration entry"
.SH DESCRIPTION
.LP
.B getconfent(\|)
gets the
.I name
entry from the configuration file.
Every entry in the configuration file is categorized. The
.I category
parameter is its specifier.
.PP
.B getconfent(\|)
is thread-safe if threads are created using the
.B Cthread_create(\|)
interface. In this case
.B getconfent(\|)
calls the re-entrant version
.B getconfent_r(\|)
internally using a buffer allocated in thread local storage.
.PP
.B getconfent_multi(\|)
returns the
.I result
in the form of an array associated with a
.I count
of the number of results.
.PP
.B getconfent_fromfile(\|)
and
.B getconfent_multi_fromfile(\|)
preform the same operation as
.B getconfent(\|)
and
.B getconfent_multi(\|)
but reading the information from a file.
.PP
.SH "RETURN VALUE"
.B getconfent(\|)
,
.B getconfent_r(\|)
and
.B getconfent_fromfile(|\)
return NULL if the entry is not found. Otherwise,
it returns the matched entry.
If
.B flags
is not null, the return value is a pointer to the full string matched
in the configuration file. Otherwise, only the pointer to the first
blank separated token is returned.
.PP
.B getconfent_multi(\|)
and
.B getconfent_multi_fromfile(\|)
return 0 if successful, -1 if failed.
.SH "ERRORS"
.PP
If the
.B getconfent(\|), getconfent_r(\|)
function fail,
.B serrno
may be set to one of the following values:
.TP
.B SENOCONFIG
can't open configuration file.
.SH "SEE ALSO"
.BR Cthread_create(3)
.SH AUTHOR
\fBCASTOR\fP Team <castor.support@cern.ch>
/*
* $Id: getconfent.h,v 1.5 2007/12/07 11:40:53 sponcec3 Exp $
*/
#ifndef __getconfent_h
#define __getconfent_h
#include "osdep.h"
EXTERN_C char *getconfent (const char *, const char *, int);
EXTERN_C char *getconfent_fromfile (const char *, const char *, const char *, int);
EXTERN_C int getconfent_multi (const char *, const char *, int, char ***, int *);
EXTERN_C int getconfent_multi_fromfile (const char *, const char *, const char *, int, char ***, int *);
EXTERN_C int getconfent_parser (char **, char ***, int *);
#endif /* __getconfent_h */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment