config.c | config.c | |||
---|---|---|---|---|
/* SASL Config file API | /* SASL Config file API | |||
* Rob Siemborski | * Rob Siemborski | |||
* Tim Martin (originally in Cyrus distribution) | * Tim Martin (originally in Cyrus distribution) | |||
* $Id: config.c,v 1.18 2009/02/14 14:01:24 mel Exp $ | * $Id: config.c,v 1.19 2011/11/08 17:22:40 murch Exp $ | |||
*/ | */ | |||
/* | /* | |||
* Copyright (c) 1998-2009 Carnegie Mellon University. All rights reserved . | * Copyright (c) 1998-2009 Carnegie Mellon University. All rights reserved . | |||
* | * | |||
* Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | |||
* modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | |||
* are met: | * are met: | |||
* | * | |||
* 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | |||
* notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | |||
skipping to change at line 58 | skipping to change at line 58 | |||
#include <ctype.h> | #include <ctype.h> | |||
#include "sasl.h" | #include "sasl.h" | |||
#include "saslint.h" | #include "saslint.h" | |||
struct configlist { | struct configlist { | |||
char *key; | char *key; | |||
char *value; | char *value; | |||
}; | }; | |||
static struct configlist *configlist; | static struct configlist *configlist = NULL; | |||
static int nconfiglist; | static int nconfiglist = 0; | |||
#define CONFIGLISTGROWSIZE 100 | #define CONFIGLISTGROWSIZE 100 | |||
int sasl_config_init(const char *filename) | int sasl_config_init(const char *filename) | |||
{ | { | |||
FILE *infile; | FILE *infile; | |||
int lineno = 0; | int lineno = 0; | |||
int alloced = 0; | int alloced = 0; | |||
char buf[4096]; | char buf[4096]; | |||
char *p, *key; | char *p, *key; | |||
skipping to change at line 93 | skipping to change at line 93 | |||
if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0'; | if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0'; | |||
for (p = buf; *p && isspace((int) *p); p++); | for (p = buf; *p && isspace((int) *p); p++); | |||
if (!*p || *p == '#') continue; | if (!*p || *p == '#') continue; | |||
key = p; | key = p; | |||
while (*p && (isalnum((int) *p) || *p == '-' || *p == '_')) { | while (*p && (isalnum((int) *p) || *p == '-' || *p == '_')) { | |||
if (isupper((int) *p)) *p = (char) tolower(*p); | if (isupper((int) *p)) *p = (char) tolower(*p); | |||
p++; | p++; | |||
} | } | |||
if (*p != ':') { | if (*p != ':') { | |||
fclose(infile); | ||||
return SASL_FAIL; | return SASL_FAIL; | |||
} | } | |||
*p++ = '\0'; | *p++ = '\0'; | |||
while (*p && isspace((int) *p)) p++; | while (*p && isspace((int) *p)) p++; | |||
if (!*p) { | if (!*p) { | |||
fclose(infile); | ||||
return SASL_FAIL; | return SASL_FAIL; | |||
} | } | |||
/* Now strip trailing spaces, if any */ | /* Now strip trailing spaces, if any */ | |||
tail = p + strlen(p) - 1; | tail = p + strlen(p) - 1; | |||
while (tail > p && isspace((int) *tail)) { | while (tail > p && isspace((int) *tail)) { | |||
*tail = '\0'; | *tail = '\0'; | |||
tail--; | tail--; | |||
} | } | |||
if (nconfiglist == alloced) { | if (nconfiglist == alloced) { | |||
alloced += CONFIGLISTGROWSIZE; | alloced += CONFIGLISTGROWSIZE; | |||
configlist=sasl_REALLOC((char *)configlist, | configlist=sasl_REALLOC((char *)configlist, | |||
alloced * sizeof(struct configlist)); | alloced * sizeof(struct configlist)); | |||
if (configlist==NULL) return SASL_NOMEM; | if (configlist == NULL) { | |||
fclose(infile); | ||||
return SASL_NOMEM; | ||||
} | ||||
} | } | |||
result = _sasl_strdup(key, | result = _sasl_strdup(key, | |||
&(configlist[nconfiglist].key), | &(configlist[nconfiglist].key), | |||
NULL); | NULL); | |||
if (result!=SASL_OK) return result; | if (result != SASL_OK) { | |||
fclose(infile); | ||||
return result; | ||||
} | ||||
result = _sasl_strdup(p, | result = _sasl_strdup(p, | |||
&(configlist[nconfiglist].value), | &(configlist[nconfiglist].value), | |||
NULL); | NULL); | |||
if (result!=SASL_OK) return result; | if (result != SASL_OK) { | |||
fclose(infile); | ||||
return result; | ||||
} | ||||
nconfiglist++; | nconfiglist++; | |||
} | } | |||
fclose(infile); | fclose(infile); | |||
return SASL_OK; | return SASL_OK; | |||
} | } | |||
const char *sasl_config_getstring(const char *key,const char *def) | const char *sasl_config_getstring(const char *key,const char *def) | |||
{ | { | |||
int opt; | int opt; | |||
for (opt = 0; opt < nconfiglist; opt++) { | for (opt = 0; opt < nconfiglist; opt++) { | |||
if (*key == configlist[opt].key[0] && | if (*key == configlist[opt].key[0] && | |||
!strcmp(key, configlist[opt].key)) | !strcmp(key, configlist[opt].key)) | |||
return configlist[opt].value; | return configlist[opt].value; | |||
} | } | |||
return def; | return def; | |||
} | } | |||
void sasl_config_done(void) | ||||
{ | ||||
int opt; | ||||
for (opt = 0; opt < nconfiglist; opt++) { | ||||
if (configlist[opt].key) sasl_FREE(configlist[opt].key); | ||||
if (configlist[opt].value) sasl_FREE(configlist[opt].value); | ||||
} | ||||
sasl_FREE(configlist); | ||||
configlist = NULL; | ||||
nconfiglist = 0; | ||||
} | ||||
End of changes. 8 change blocks. | ||||
6 lines changed or deleted | 17 lines changed or added | |||
This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/ |