📄 config.c
字号:
/* * =========================================================================== * PRODUCTION $Log: config.c,v $ * PRODUCTION Revision 1000.0 2003/10/29 20:34:34 gouriano * PRODUCTION PRODUCTION: IMPORTED [ORIGINAL] Dev-tree R1.5 * PRODUCTION * =========================================================================== *//* FreeTDS - Library of routines accessing Sybase and Microsoft databases * Copyright (C) 1998-1999 Brian Bruns * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */#include <tds_config.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <time.h>#include <limits.h>#include <assert.h>#include <ctype.h>#ifdef __DGUX__#include <paths.h>#endif#ifdef __FreeBSD__#include <sys/time.h>#endif#ifdef WIN32#include <windows.h>#include <stdio.h>#define PATH_MAX 255#endif#ifndef WIN32#include <netdb.h>#include <sys/types.h>#include <netinet/in.h>#include <arpa/inet.h>#endif#include "tds.h"#include "tdsutil.h"#ifdef DMALLOC#include <dmalloc.h>#endifstatic char software_version[] = "$Id: config.c,v 1000.0 2003/10/29 20:34:34 gouriano Exp $";static void *no_unused_var_warn[] = {software_version, no_unused_var_warn};static void tds_config_login(TDSCONFIGINFO *config, TDSLOGIN *login);static void tds_config_env_dsquery(TDSCONFIGINFO *config);static void tds_config_env_tdsdump(TDSCONFIGINFO *config);static void tds_config_env_tdsver(TDSCONFIGINFO *config);static void tds_config_env_tdsport(TDSCONFIGINFO *config);static void tds_config_env_tdshost(TDSCONFIGINFO *config);static int tds_read_conf_file(char *server, TDSCONFIGINFO *config);static int tds_read_conf_sections(FILE *in, char *server, TDSCONFIGINFO *config);static int tds_read_conf_section(FILE *in, char *section, TDSCONFIGINFO *config);static void tds_read_interfaces(char *server, TDSCONFIGINFO *config);static void tds_config_verstr(char *tdsver, TDSCONFIGINFO *config);static int tds_config_boolean(char *value);static void lookup_host(const char *servername, const char *portname, char *ip, char *port);static int parse_server_name_for_port( TDSCONFIGINFO *config, TDSLOGIN *login );extern int g_append_mode;static char interf_file[MAXPATH];/*** tds_get_config() will fill the tds config structure based on configuration ** information gathered in the following order:** 1) Program specified in TDSLOGIN structure** 2) The environment variables TDSVER, TDSDUMP, TDSPORT, TDSQUERY, TDSHOST** 3) A config file with the following search order:** a) a readable file specified by environment variable FREETDSCONF** b) a readable file in ~/.freetds.conf** c) a readable file in $prefix/etc/freetds.conf** 3) ~/.interfaces if exists** 4) $SYBASE/interfaces if exists** 5) TDS_DEF_* default values**** .tdsrc and freetds.conf have been added to make the package easier to ** integration with various Linux and *BSD distributions.*/ TDSCONFIGINFO *tds_get_config(TDSSOCKET *tds, TDSLOGIN *login, TDSLOCINFO *locale){TDSCONFIGINFO *config;char *s;char path[MAXPATH];pid_t pid; /* allocate a new structure with hard coded and build-time defaults */ config = tds_alloc_config(locale); s=getenv("TDSDUMPCONFIG"); if (s) { if ( !*s) { pid = getpid(); sprintf(path,"/tmp/tdsconfig.log.%d",pid); s = path; } tdsdump_open(s); } tdsdump_log(TDS_DBG_INFO1, "%L Attempting to read conf files.\n"); if (! tds_read_conf_file(login->server_name, config)) { /* fallback to interfaces file */ tdsdump_log(TDS_DBG_INFO1, "%L Failed in reading conf file. Trying interface files.\n"); tds_read_interfaces(login->server_name, config); } if( parse_server_name_for_port( config, login ) ) { tdsdump_log(TDS_DBG_INFO1, "Parsed servername, now %s on %d.\n", config->server_name, login->port); } /* Now check the environment variables */ tds_config_env_tdsver(config); tds_config_env_tdsdump(config); tds_config_env_tdsport(config); tds_config_env_dsquery(config); tds_config_env_tdshost(config); /* And finally the login structure */ tds_config_login(config, login); if ( s && *s) tdsdump_close(); return config;} static int tds_try_conf_file(char *path, char *how, char *server, TDSCONFIGINFO *config){int found = 0;FILE *in; if ((in = fopen (path, "r")) != NULL) { tdsdump_log(TDS_DBG_INFO1, "%L Found conf file in %s %s. Reading section %s.\n",interf_file,how, server); found = tds_read_conf_sections (in, server, config); if(found) tdsdump_log(TDS_DBG_INFO1, "%L ...Success.\n"); fclose (in); } return found;}static int tds_read_conf_file(char *server, TDSCONFIGINFO *config){#ifndef NCBI_FTDSFILE *in;#endifchar *home, *path;int found = 0; if (interf_file[0]!='\0') { found = tds_try_conf_file(interf_file,"set programmatically", server, config); } /* FREETDSCONF env var, pkleef@openlinksw.com 01/21/02 */ if (!found) { path = getenv ("FREETDSCONF"); if (path) { found = tds_try_conf_file(path, "(from $FREETDSCONF)", server, config); } } if (!found) { /* FIXME use getpwent for security */ home = getenv("HOME"); if (home!=NULL && home[0]!='\0') { /* FIXME check buffer */ path = malloc(strlen(home) + 14 + 1); /* strlen("/.freetds.conf")=14 */ sprintf(path,"%s/.freetds.conf",home); found = tds_try_conf_file(path, "(.freetds.conf)", server, config); free(path); } } if (!found) { found = tds_try_conf_file(FREETDS_SYSCONFFILE, "(default)", server, config); } return found;}static int tds_read_conf_sections(FILE *in, char *server, TDSCONFIGINFO *config){char *section;int i, found = 0; tds_read_conf_section(in, "global", config); rewind(in); section = strdup(server); for (i=0;i<strlen(section);i++) section[i]=tolower(section[i]); found = tds_read_conf_section(in, section, config); free(section); return found;}static int tds_config_boolean(char *value) { if (!strcmp(value, "yes") || !strcmp(value, "on") || !strcmp(value, "true") || !strcmp(value, "1")) { tdsdump_log(TDS_DBG_INFO1, "%L %s is a 'yes/on/true'.\n",value); return 1; } else { tdsdump_log(TDS_DBG_INFO1, "%L %s is a 'no/off/false'.\n",value); return 0; }}static int tds_read_conf_section(FILE *in, char *section, TDSCONFIGINFO *config){char line[256], option[256], value[256], *s;int i;char p;int insection = 0;char tmp[256];int found = 0; tdsdump_log(TDS_DBG_INFO1, "%L Looking for section %s.\n", section); while (fgets(line, 256, in)) { s = line; /* skip leading whitespace */ while (*s && isspace(*s)) s++; /* skip it if it's a comment line */ if (*s==';' || *s=='#') continue; /* read up to the = ignoring duplicate spaces */ p = 0; i = 0; while (*s && *s!='=') { if (!isspace(*s) && isspace(p)) option[i++]=' '; if (!isspace(*s)) option[i++]=tolower(*s); p = *s; s++; } option[i]='\0'; /* skip the = */ if(*s) s++; /* skip leading whitespace */ while (*s && isspace(*s)) s++; /* read up to a # ; or null ignoring duplicate spaces */ p = 0; i = 0; while (*s && *s!=';' && *s!='#') { if (!isspace(*s) && isspace(p)) value[i++]=' '; if (!isspace(*s)) value[i++]=*s; p = *s; s++; } value[i]='\0'; if (!strlen(option)) continue; if (option[0]=='[') { s = &option[1]; while (*s) { if (*s==']') *s='\0'; *s = tolower(*s); s++; } tdsdump_log(TDS_DBG_INFO1, "%L ... Found section %s.\n", &option[1]); if (!strcmp(section, &option[1])) { tdsdump_log(TDS_DBG_INFO1, "%L Got a match.\n"); insection=1; found=1; } else { insection=0; } } else if (insection) { /* fprintf(stderr,"option = '%s' value = '%s'\n", option, value); */ tdsdump_log(TDS_DBG_INFO1, "%L option = '%s' value = '%s'.\n", option, value); if (!strcmp(option,TDS_STR_VERSION)) { tds_config_verstr(value, config); } else if (!strcmp(option,TDS_STR_BLKSZ)) { if (atoi(value)) config->block_size = atoi(value); } else if (!strcmp(option,TDS_STR_SWAPDT)) { config->broken_dates = tds_config_boolean(value); } else if (!strcmp(option,TDS_STR_SWAPMNY)) { config->broken_money = tds_config_boolean(value); } else if (!strcmp(option,TDS_STR_TRYSVR)) { config->try_server_login = tds_config_boolean(value); } else if (!strcmp(option,TDS_STR_TRYDOM)) { config->try_domain_login = tds_config_boolean(value); } else if (!strcmp(option,TDS_STR_DOMAIN)) { if (config->default_domain) free(config->default_domain); config->default_domain = strdup(value); } else if (!strcmp(option,TDS_STR_XDOMAUTH)) { config->xdomain_auth = tds_config_boolean(value); } else if (!strcmp(option,TDS_STR_DUMPFILE)) { if (config->dump_file) free(config->dump_file); config->dump_file = strdup(value); } else if (!strcmp(option,TDS_STR_DEBUGLVL)) { if (atoi(value)) config->debug_level = atoi(value); } else if (!strcmp(option,TDS_STR_TIMEOUT )) { if (atoi(value)) config->timeout = atoi(value); } else if (!strcmp(option,TDS_STR_CONNTMOUT)) { if (atoi(value)) config->connect_timeout = atoi(value); } else if (!strcmp(option,TDS_STR_HOST)) { tdsdump_log(TDS_DBG_INFO1, "%L Found host entry %s.\n",value); lookup_host(value, NULL, tmp, NULL);#ifdef NCBI_FTDS if (config->ip_addr[0]) free(config->ip_addr[0]); config->ip_addr[0] = strdup(tmp); tdsdump_log(TDS_DBG_INFO1, "%L IP addr is %s.\n",config->ip_addr[0]);#else if (config->ip_addr) free(config->ip_addr); config->ip_addr = strdup(tmp); tdsdump_log(TDS_DBG_INFO1, "%L IP addr is %s.\n",config->ip_addr);#endif } else if (!strcmp(option,TDS_STR_PORT)) { if (atoi(value)) #ifdef NCBI_FTDS config->port[0] = atoi(value);#else config->port = atoi(value);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -