static HODBCINSTPROPERTY definePropertyHidden(HODBCINSTPROPERTY hLastProperty, const char *name, const char *value, const char *comment) { hLastProperty = addProperty(hLastProperty); hLastProperty->nPromptType = ODBCINST_PROMPTTYPE_HIDDEN; tds_strlcpy(hLastProperty->szName, name, INI_MAX_PROPERTY_NAME); tds_strlcpy(hLastProperty->szValue, value, INI_MAX_PROPERTY_VALUE); hLastProperty->pszHelp = (char *) strdup(comment); return hLastProperty; }
static HODBCINSTPROPERTY definePropertyList(HODBCINSTPROPERTY hLastProperty, const char *name, const char *value, const void *list, int size, const char *comment) { hLastProperty = addProperty(hLastProperty); hLastProperty->nPromptType = ODBCINST_PROMPTTYPE_LISTBOX; hLastProperty->aPromptData = malloc(size); memcpy(hLastProperty->aPromptData, list, size); tds_strlcpy(hLastProperty->szName, name, INI_MAX_PROPERTY_NAME); tds_strlcpy(hLastProperty->szValue, value, INI_MAX_PROPERTY_VALUE); hLastProperty->pszHelp = (char *) strdup(comment); return hLastProperty; }
int main(void) { char *buf = (char *) malloc(10); /* test tds_strlcpy */ memset(buf, 0xff, 10); tds_strlcpy(buf, "test", 10); assert(strcmp(buf, "test") == 0); memset(buf, 0xff, 10); tds_strlcpy(buf, "TESTTEST", 10); assert(strcmp(buf, "TESTTEST") == 0); memset(buf, 0xff, 10); tds_strlcpy(buf, "abcdefghi", 10); assert(strcmp(buf, "abcdefghi") == 0); memset(buf, 0xff, 10); tds_strlcpy(buf, "1234567890", 10); assert(strcmp(buf, "123456789") == 0); memset(buf, 0xff, 10); tds_strlcpy(buf, "xyzabc1234567890", 10); assert(strcmp(buf, "xyzabc123") == 0); /* test tds_strlcat */ strcpy(buf, "xyz"); tds_strlcat(buf, "test", 10); assert(strcmp(buf, "xyztest") == 0); strcpy(buf, "xyz"); tds_strlcat(buf, "TESTAB", 10); assert(strcmp(buf, "xyzTESTAB") == 0); strcpy(buf, "xyz"); tds_strlcat(buf, "TESTabc", 10); assert(strcmp(buf, "xyzTESTab") == 0); strcpy(buf, "xyz"); tds_strlcat(buf, "123456789012345", 10); assert(strcmp(buf, "xyz123456") == 0); strcpy(buf, "123456789"); tds_strlcat(buf, "test", 4); assert(strcmp(buf, "123456789") == 0); free(buf); return 0; }
static void odbc_get_v2state(const char *sqlstate, char *dest_state) { const struct s_v3to2map *pmap = v3to2map; while (pmap->v3[0]) { if (!strcasecmp(pmap->v3, sqlstate)) { tds_strlcpy(dest_state, pmap->v2, 6); return; } ++pmap; } /* return the original if a v2 state is not found */ tds_strlcpy(dest_state, sqlstate, 6); }
/** * Get locale information. * @return allocated structure with all information or NULL if error */ TDSLOCALE * tds_get_locale(void) { TDSLOCALE *locale; char *s; FILE *in; /* allocate a new structure with hard coded and build-time defaults */ locale = tds_alloc_locale(); if (!locale) return NULL; tdsdump_log(TDS_DBG_INFO1, "Attempting to read locales.conf file\n"); in = fopen(FREETDS_LOCALECONFFILE, "r"); if (in) { tds_read_conf_section(in, "default", tds_parse_locale, locale); #if HAVE_LOCALE_H s = setlocale(LC_ALL, NULL); #else s = getenv("LANG"); #endif if (s && s[0]) { int found; char buf[128]; const char *strip = "@._"; /* do not change environment !!! */ tds_strlcpy(buf, s, sizeof(buf)); /* search full name */ rewind(in); found = tds_read_conf_section(in, buf, tds_parse_locale, locale); /* * Here we try to strip some part of language in order to * catch similar language * LANG is composed by * language[_sublanguage][.charset][@modified] * ie it_IT@euro or it_IT.UTF-8 so we strip in order * modifier, charset and sublanguage * ie it_IT@euro -> it_IT -> it */ for (;!found && *strip; ++strip) { s = strrchr(buf, *strip); if (!s) continue; *s = 0; rewind(in); found = tds_read_conf_section(in, buf, tds_parse_locale, locale); } } fclose(in); } return locale; }
/* TODO check if TDS_UINT is correct for native error */ void odbc_errs_add_rdbms(struct _sql_errors *errs, TDS_UINT native, const char *sqlstate, const char *msg, int linenum, int msgstate, const char *server, int row) { struct _sql_error *p; int n = errs->num_errors; if (errs->errs) p = (struct _sql_error *) realloc(errs->errs, sizeof(struct _sql_error) * (n + 1)); else p = (struct _sql_error *) malloc(sizeof(struct _sql_error)); if (!p) return; errs->errs = p; memset(&errs->errs[n], 0, sizeof(struct _sql_error)); errs->errs[n].row = row; errs->errs[n].native = native; if (sqlstate) tds_strlcpy(errs->errs[n].state2, sqlstate, 6); else errs->errs[n].state2[0] = '\0'; strcpy(errs->errs[n].state3, errs->errs[n].state2); sqlstate2to3(errs->errs[n].state3); /* TODO why driver ?? -- freddy77 */ errs->errs[n].server = (server) ? strdup(server) : strdup("DRIVER"); errs->errs[n].msg = msg ? strdup(msg) : odbc_get_msg(errs->errs[n].state3); errs->errs[n].linenum = linenum; errs->errs[n].msgstate = msgstate; ++errs->num_errors; }
/* * pool_mbr_login open a single pool login, to be call at init time or * to reconnect. */ static TDSSOCKET * pool_mbr_login(TDS_POOL * pool) { TDSCONTEXT *context; TDSLOGIN *login; TDSSOCKET *tds; TDSCONNECTION *connection; int rc; char *query; char hostname[MAXHOSTNAMELEN]; login = tds_alloc_login(); tds_set_passwd(login, pool->password); tds_set_user(login, pool->user); tds_set_app(login, "tdspool"); #if HAVE_GETHOSTNAME if (gethostname(hostname, MAXHOSTNAMELEN) < 0) #endif tds_strlcpy(hostname, "tdspool", MAXHOSTNAMELEN); tds_set_host(login, hostname); tds_set_library(login, "TDS-Library"); tds_set_server(login, pool->server); tds_set_client_charset(login, "iso_1"); tds_set_language(login, "us_english"); tds_set_packet(login, 512); context = tds_alloc_context(NULL); tds = tds_alloc_socket(context, 512); connection = tds_read_config_info(tds, login, context->locale); if (!connection || tds_connect_and_login(tds, connection) != TDS_SUCCEED) { tds_free_socket(tds); tds_free_connection(connection); /* what to do? */ fprintf(stderr, "Could not open connection to server %s\n", pool->server); return NULL; } tds_free_connection(connection); /* * FIXME -- tds_connect_and_login no longer preallocates the in_buf need to * do something like what tds_read_packet does */ tds->in_buf = (unsigned char *) calloc(BLOCKSIZ, 1); if (pool->database && strlen(pool->database)) { query = (char *) malloc(strlen(pool->database) + 5); sprintf(query, "use %s", pool->database); rc = tds_submit_query(tds, query); free(query); if (rc != TDS_SUCCEED) { fprintf(stderr, "changing database failed\n"); return NULL; } if (tds_process_simple_query(tds) != TDS_SUCCEED) return NULL; } return tds; }
static void parse_argument(const char argument[], PROCEDURE* procedure) { const char *s = strchr(argument, '.'); if (s) { size_t len = s - argument; if (len > sizeof(procedure->owner) - 1) len = sizeof(procedure->owner) - 1; memcpy(procedure->owner, argument, len); procedure->owner[len] = '\0'; tds_strlcpy(procedure->name, s+1, sizeof(procedure->name)); } else { strcpy(procedure->owner, "dbo"); tds_strlcpy(procedure->name, argument, sizeof(procedure->name)); } }
int INSTAPI #else static int #endif SQLGetPrivateProfileString(LPCSTR pszSection, LPCSTR pszEntry, LPCSTR pszDefault, LPSTR pRetBuffer, int nRetBuffer, LPCSTR pszFileName) { FILE *hFile; ProfileParam param; tdsdump_log(TDS_DBG_FUNC, "SQLGetPrivateProfileString(%p, %p, %p, %p, %d, %p)\n", pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName); if (!pszSection) { /* spec says return list of all section names - but we will just return nothing */ tdsdump_log(TDS_DBG_WARN, "WARNING: Functionality for NULL pszSection not implemented.\n"); return 0; } if (!pszEntry) { /* spec says return list of all key names in section - but we will just return nothing */ tdsdump_log(TDS_DBG_WARN, "WARNING: Functionality for NULL pszEntry not implemented.\n"); return 0; } if (nRetBuffer < 1) tdsdump_log(TDS_DBG_WARN, "WARNING: No space to return a value because nRetBuffer < 1.\n"); if (pszFileName && *pszFileName == '/') hFile = fopen(pszFileName, "r"); else hFile = tdoGetIniFileName(); if (hFile == NULL) { tdsdump_log(TDS_DBG_ERROR, "ERROR: Could not open configuration file\n"); return 0; } param.entry = pszEntry; param.buffer = pRetBuffer; param.buffer_len = nRetBuffer; param.ret_val = 0; param.found = 0; pRetBuffer[0] = 0; tds_read_conf_section(hFile, pszSection, tdoParseProfile, ¶m); if (pszDefault && !param.found) { tds_strlcpy(pRetBuffer, pszDefault, nRetBuffer); param.ret_val = (int)strlen(pRetBuffer); } fclose(hFile); return param.ret_val; }
const char * tds_inet_ntoa_r(struct in_addr iaddr, char *ip, size_t len) { #if defined(AF_INET) && HAVE_INET_NTOP inet_ntop(AF_INET, &iaddr, ip, len); #elif HAVE_INET_NTOA_R inet_ntoa_r(iaddr, ip, len); #else tds_strlcpy(ip, inet_ntoa(iaddr), len); #endif return ip; }
static void tdoParseProfile(const char *option, const char *value, void *param) { ProfileParam *p = (ProfileParam *) param; if (strcasecmp(p->entry, option) == 0) { tds_strlcpy(p->buffer, value, p->buffer_len); p->ret_val = (int) strlen(p->buffer); p->found = 1; } }
int main(void) { char *buf = (char *) malloc(10); /* test tds_strlcpy */ memset(buf, 0xff, 10); assert(tds_strlcpy(buf, "test", 10) == 4); assert(strcmp(buf, "test") == 0); memset(buf, 0xff, 10); assert(tds_strlcpy(buf, "TESTTEST", 10) == 8); assert(strcmp(buf, "TESTTEST") == 0); memset(buf, 0xff, 10); assert(tds_strlcpy(buf, "abcdefghi", 10) == 9); assert(strcmp(buf, "abcdefghi") == 0); memset(buf, 0xff, 10); assert(tds_strlcpy(buf, "1234567890", 10) == 10); assert(strcmp(buf, "123456789") == 0); memset(buf, 0xff, 10); assert(tds_strlcpy(buf, "xyzabc1234567890", 10) == 16); assert(strcmp(buf, "xyzabc123") == 0); /* test tds_strlcat */ strcpy(buf, "xyz"); assert(tds_strlcat(buf, "test", 10) == 7); assert(strcmp(buf, "xyztest") == 0); strcpy(buf, "xyz"); assert(tds_strlcat(buf, "TESTAB", 10) == 9); assert(strcmp(buf, "xyzTESTAB") == 0); strcpy(buf, "xyz"); assert(tds_strlcat(buf, "TESTabc", 10) == 10); assert(strcmp(buf, "xyzTESTab") == 0); strcpy(buf, "xyz"); assert(tds_strlcat(buf, "123456789012345", 10) == 18); assert(strcmp(buf, "xyz123456") == 0); strcpy(buf, "123456789"); assert(tds_strlcat(buf, "test", 4) == 13); assert(strcmp(buf, "123456789") == 0); /* test length == 0 */ assert(tds_strlcpy(buf + 10, "test", 0) == 4); strcpy(buf, "123"); assert(tds_strlcat(buf, "456", 0) == 6); assert(strcmp(buf, "123") == 0); free(buf); return 0; }
void odbc_errs_add(struct _sql_errors *errs, const char *sqlstate, const char *msg) { struct _sql_error *p; int n; assert(sqlstate); if (!errs) return; n = errs->num_errors; if (errs->errs) p = (struct _sql_error *) realloc(errs->errs, sizeof(struct _sql_error) * (n + 1)); else p = (struct _sql_error *) malloc(sizeof(struct _sql_error)); if (!p) return; errs->errs = p; memset(&errs->errs[n], 0, sizeof(struct _sql_error)); errs->errs[n].native = 0; tds_strlcpy(errs->errs[n].state3, sqlstate, 6); odbc_get_v2state(errs->errs[n].state3, errs->errs[n].state2); /* TODO why driver ?? -- freddy77 */ errs->errs[n].server = strdup("DRIVER"); errs->errs[n].msg = msg ? strdup(msg) : odbc_get_msg(errs->errs[n].state3); ++errs->num_errors; /* updated last error */ if (!strcmp(sqlstate, "01004") || !strcmp(sqlstate, "01S02")) { if (errs->lastrc != SQL_ERROR) errs->lastrc = SQL_SUCCESS_WITH_INFO; } else { errs->lastrc = SQL_ERROR; } tdsdump_log(TDS_DBG_FUNC, "odbc_errs_add: \"%s\"\n", errs->errs[n].msg); }
/** * Allocate memory and copy the rpc information into a TDSPARAMINFO structure. */ static TDSPARAMINFO * param_info_alloc(TDSSOCKET * tds, DBREMOTE_PROC * rpc) { int i; DBREMOTE_PROC_PARAM *p; TDSCOLUMN *pcol; TDSPARAMINFO *params = NULL, *new_params; BYTE *temp_value; int temp_datalen; int temp_type; int param_is_null; /* sanity */ if (rpc == NULL) return NULL; /* see v 1.10 2002/11/23 for first broken attempt */ for (i = 0, p = rpc->param_list; p != NULL; p = p->next, i++) { const unsigned char *prow; if (!(new_params = tds_alloc_param_result(params))) { tds_free_param_results(params); tdsdump_log(TDS_DBG_ERROR, "out of rpc memory!"); return NULL; } params = new_params; /* * Determine whether an input parameter is NULL * or not. */ param_is_null = 0; temp_type = p->type; temp_value = p->value; temp_datalen = p->datalen; if (p->datalen == 0) param_is_null = 1; tdsdump_log(TDS_DBG_INFO1, "parm_info_alloc(): parameter null-ness = %d\n", param_is_null); if (param_is_null || (p->status & DBRPCRETURN)) { if (param_is_null) { temp_datalen = 0; temp_value = NULL; } else if (is_fixed_type(temp_type)) { temp_datalen = tds_get_size_by_type(temp_type); } temp_type = tds_get_null_type(temp_type); } else if (is_fixed_type(temp_type)) { temp_datalen = tds_get_size_by_type(temp_type); } pcol = params->columns[i]; /* meta data */ if (p->name) { tds_strlcpy(pcol->column_name, p->name, sizeof(pcol->column_name)); pcol->column_namelen = strlen(pcol->column_name); } tds_set_param_type(tds, pcol, temp_type); if (p->maxlen > 0) pcol->column_size = p->maxlen; else { if (is_fixed_type(p->type)) { pcol->column_size = tds_get_size_by_type(p->type); } else { pcol->column_size = p->datalen; } } pcol->on_server.column_size = pcol->column_size; pcol->column_output = p->status; pcol->column_cur_size = temp_datalen; prow = param_row_alloc(params, pcol, i, temp_value, temp_datalen); if (!prow) { tds_free_param_results(params); tdsdump_log(TDS_DBG_ERROR, "out of memory for rpc row!"); return NULL; } } return params; }