⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tsql.c

📁 在Linux/Unix下面访问WINDOWS SQLSERVER 的ODBC驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
			break;		case 'D':			opt_default_db = strdup(optarg);			break;		case 'o':			opt_flags_str = optarg;			break;		case 'H':			hostname = (char *) malloc(strlen(optarg) + 1);			strcpy(hostname, optarg);			break;		case 'S':			servername = (char *) malloc(strlen(optarg) + 1);			strcpy(servername, optarg);			break;		case 'U':			username = (char *) malloc(strlen(optarg) + 1);			strcpy(username, optarg);			break;		case 'P':			password = (char *) malloc(strlen(optarg) + 1);			strcpy(password, optarg);			break;		case 'I':			confile = (char *) malloc(strlen(optarg) + 1);			strcpy(confile, optarg);			break;		case 'p':			port = atoi(optarg);			break;		case 'C':			settings = tds_get_compiletime_settings();			printf("%s\n%35s: %s\n%35s: %s\n%35s: %s\n%35s: %s\n%35s: %s\n%35s: %s\n%35s: %s\n%35s: %s\n%35s: %s\n",			       "Compile-time settings (established with the \"configure\" script)",			       "Version", settings->freetds_version,			       "freetds.conf directory", settings->sysconfdir, 			       /* settings->last_update */			       "MS db-lib source compatibility", settings->msdblib ? "yes" : "no",			       "Sybase binary compatibility",			       (settings->sybase_compat == -1 ? "unknown" : (settings->sybase_compat ? "yes" : "no")),			       "Thread safety", settings->threadsafe ? "yes" : "no",			       "iconv library", settings->libiconv ? "yes" : "no",			       "TDS version", settings->tdsver,			       "iODBC", settings->iodbc ? "yes" : "no", 			       "unixodbc", settings->unixodbc ? "yes" : "no");			exit(0);			break;		default:			tsql_print_usage(argv[0]);			exit(1);			break;		}	}	if (opt_flags_str != NULL) {		char *minus_flags = malloc(strlen(opt_flags_str) + 5);		if (minus_flags != NULL) {			strcpy(minus_flags, "go -");			strcat(minus_flags, opt_flags_str);			get_opt_flags(minus_flags, &global_opt_flags);			free(minus_flags);		}	}	if (locale)		if (!QUIET) printf("locale is \"%s\"\n", locale);	if (charset) {		if (!QUIET) printf("locale charset is \"%s\"\n", charset);	} else {		charset = "ISO-8859-1";		if (!QUIET) printf("using default charset \"%s\"\n", charset);	}	/* validate parameters */	if (!servername && !hostname) {		fprintf(stderr, "Missing argument -S or -H\n");		tsql_print_usage(argv[0]);		exit(1);	}	if (hostname && !port) {		fprintf(stderr, "Missing argument -p \n");		tsql_print_usage(argv[0]);		exit(1);	}	if (!username) {		fprintf(stderr, "Missing argument -U \n");		tsql_print_usage(argv[0]);		exit(1);	}	if (!servername && !hostname) {		tsql_print_usage(argv[0]);		exit(1);	}	if (!password) {		password = (char*) malloc(128);		readpassphrase("Password: ", password, 128, RPP_ECHO_OFF);	}	if (!opt_col_term) {		fprintf(stderr, "Missing delimiter for -t (check escaping)\n");		tsql_print_usage(argv[0]);		exit(1);	}	if (!opt_row_term) {		fprintf(stderr, "Missing delimiter for -r (check escaping)\n");		tsql_print_usage(argv[0]);		exit(1);	}	/* all validated, let's do it */	/* if it's a servername */	if (servername) {		tds_set_user(login, username);		tds_set_app(login, "TSQL");		tds_set_library(login, "TDS-Library");		tds_set_server(login, servername);		tds_set_client_charset(login, charset);		tds_set_language(login, "us_english");		tds_set_passwd(login, password);		if (confile) {			tds_set_interfaces_file_loc(confile);		}		/* else we specified hostname/port */	} else {		tds_set_user(login, username);		tds_set_app(login, "TSQL");		tds_set_library(login, "TDS-Library");		tds_set_server(login, hostname);		tds_set_port(login, port);		tds_set_client_charset(login, charset);		tds_set_language(login, "us_english");		tds_set_passwd(login, password);	}	/* free up all the memory */	free(hostname);	free(username);	free(password);	free(servername);}static inttsql_handle_message(const TDSCONTEXT * context, TDSSOCKET * tds, TDSMESSAGE * msg){	if (msg->msgno == 0) {		fprintf(stderr, "%s\n", msg->message);		return 0;	}	if (msg->msgno != 5701 && msg->msgno != 5703	    && msg->msgno != 20018) {		fprintf(stderr, "Msg %d, Level %d, State %d, Server %s, Line %d\n%s\n",			msg->msgno, msg->severity, msg->state, msg->server, msg->line_number, msg->message);	}	return TDS_INT_CANCEL;}static voidslurp_input_file(char *fname, char **mybuf, int *bufsz, size_t *buflen, int *line){	FILE *fp = NULL;	register char *n;	char linebuf[1024];	char *s = NULL;	if ((fp = fopen(fname, "r")) == NULL) {		fprintf(stderr, "Unable to open input file '%s': %s\n", fname, strerror(errno));		return;	}	while ((s = fgets(linebuf, sizeof(linebuf), fp)) != NULL) {		while (*buflen + strlen(s) + 2 > *bufsz) {			*bufsz *= 2;			*mybuf = (char *) realloc(*mybuf, *bufsz);		}		strcpy(*mybuf + *buflen, s);		*buflen += strlen(*mybuf + *buflen);		n = strrchr(s, '\n');		if (n != NULL)			*n = '\0';		tsql_add_history(s);		(*line)++;	}}extern const char STD_DATETIME_FMT[];intmain(int argc, char **argv){	char *s = NULL, *s2 = NULL, *cmd = NULL;	char prompt[20];	int line = 0;	char *mybuf;	int bufsz = 4096;	size_t buflen = 0;	TDSSOCKET *tds;	TDSLOGIN *login;	TDSCONTEXT *context;	TDSCONNECTION *connection;	int opt_flags = 0;#if HAVE_FORK	pid_t timer_pid = 0;	int pipes[2];#endif	istty = isatty(0);	if (INITSOCKET()) {		fprintf(stderr, "Unable to initialize sockets\n");		return 1;	}	/* grab a login structure */	login = tds_alloc_login();	context = tds_alloc_context(NULL);	if (context->locale && !context->locale->date_fmt) {		/* set default in case there's no locale file */		context->locale->date_fmt = strdup(STD_DATETIME_FMT);	}	context->msg_handler = tsql_handle_message;	context->err_handler = tsql_handle_message;	/* process all the command line args into the login structure */	populate_login(login, argc, argv);	/* Try to open a connection */	tds = tds_alloc_socket(context, 512);	tds_set_parent(tds, NULL);	connection = tds_read_config_info(NULL, login, context->locale);	if (opt_default_db) {		tds_dstr_copy(&connection->database, opt_default_db);		if (!QUIET) fprintf(stderr, "Default database being set to %s\n", opt_default_db);	}	/* 	 * If we're able to establish an ip address for the server, we'll try to connect to it. 	 * If that machine is currently unreachable	 * show a timer connecting to the server 	 */#if HAVE_FORK	if (connection && !QUIET) {		/*		 * Here we use a pipe so if even main program core the counter stops		 * Note that we do not read or write nothing from this pipe		 */		if (pipe(pipes) < 0) {			perror("tsql: pipe");			return 1;		}		timer_pid = fork();		if (-1 == timer_pid) {			perror("tsql: warning"); /* make a note */		}		if (0 == timer_pid) {			int i=0;			close(pipes[1]);			while(1) {				fd_set set;				struct timeval tv;				FD_ZERO(&set);				FD_SET(pipes[0], &set);				tv.tv_sec = 1;				tv.tv_usec = 0;				if (select(pipes[0] + 1, &set, NULL, &set, &tv) != 0)					return 0;				fprintf(stderr, "\r%2d", ++i);			}		}		close(pipes[0]);	}#endif	if (!connection || tds_connect(tds, connection) == TDS_FAIL) {		tds_free_connection(connection);		tds_free_socket(tds);		tds_free_login(login);		tds_free_context(context);		fprintf(stderr, "There was a problem connecting to the server\n");		exit(1);	}	tds_free_connection(connection);	/* give the buffer an initial size */	bufsz = 4096;	mybuf = (char *) malloc(bufsz);	mybuf[0] = '\0';	buflen = 0;#if defined(HAVE_READLINE) && HAVE_RL_INHIBIT_COMPLETION	rl_inhibit_completion = 1;#endif#if HAVE_FORK	if (timer_pid > 0 ) {		close(pipes[1]);		if (wait(0) == -1 ) {			perror("tsql: warning");		} else {			fprintf(stderr, "\r");		}	}#endif	for (s=NULL, s2=NULL; ; free(s), free(s2), s2=NULL) {		sprintf(prompt, "%d> ", ++line);		s = tsql_readline(QUIET ? NULL : prompt);		if (s == NULL) 			break;		/* 		 * 'GO' is special only at the start of a line		 *  The rest of the line may include options that apply to the batch, 		 *  and perhaps whitespace.  		 */		if (0 == strncasecmp(s, "go", 2) && (strlen(s) == 2 || isspace(s[2]))) {			char *go_line = strdup(s);			assert(go_line);			line = 0;			if (get_opt_flags(go_line, &opt_flags)) {				free(go_line);				opt_flags ^= global_opt_flags;				do_query(tds, mybuf, opt_flags);				mybuf[0] = '\0';				buflen = 0;				continue;			}			free(go_line);		}				/* skip leading whitespace */		if ((s2 = strdup(s)) == NULL) {	/* copy to mangle with strtok() */			perror("tsql: ");			exit(1);		}				if ((cmd = strtok(s2, " \t")) == NULL)			cmd = "";		if (!strcasecmp(cmd, "exit") || !strcasecmp(cmd, "quit") || !strcasecmp(cmd, "bye"))			break;		if (!strcasecmp(cmd, "version")) {			tds_version(tds, mybuf);			printf("using TDS version %s\n", mybuf);			line = 0;			mybuf[0] = '\0';			buflen = 0;			continue;		}		if (!strcasecmp(cmd, "reset")) {			line = 0;			mybuf[0] = '\0';			buflen = 0;		} else if (!strcasecmp(cmd, ":r")) {			slurp_input_file(strtok(NULL, " \t"), &mybuf, &bufsz, &buflen, &line);		} else {			while (buflen + strlen(s) + 2 > bufsz) {				char *newbuf; 				bufsz *= 2;				if ((newbuf = realloc(mybuf, bufsz)) == NULL) {					perror("tsql: ");					exit(1);				}				mybuf = newbuf;			}			tsql_add_history(s);			strcpy(mybuf + buflen, s);			/* preserve line numbering for the parser */			strcat(mybuf + buflen, "\n");			buflen += strlen(mybuf + buflen);		}	}	/* close up shop */	free(mybuf);	tds_free_socket(tds);	tds_free_login(login);	tds_free_context(context);	DONESOCKET();	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -