[PATCH] Add -i and -o switches for freebcp

"Craig A. Berry" <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
The attached patch adds switches to freebcp for input and output  
redirection just like the Microsoft and Sybase clients have.  The  
input is not particularly useful at the moment since we haven't  
implemented prompting for any kind of input, but it seemed like it  
ought to go in while I was in the vicinity.

The -o option is especially convenient on a system like mine where the  
shell does not do redirection.

Two files are affected:

% lsdiff -s --strip=1 002_freebcpio.patch
! src/apps/freebcp.c
! src/apps/freebcp.h

________________________________________
Craig A. Berry
mailto:[email protected]

"... getting out of a sonnet is much more
  difficult than getting in."
                  Brad Leithauser

_______________________________________________
FreeTDS mailing list
[email protected]
http://lists.ibiblio.org/mailman/listinfo/freetds
002_freebcpio.patch (application/octet-stream, 2.9 KB)
# HG changeset patch
# User BERRYC
# Date 1273682444 18000
# Node ID 6d37105fefd2d23a5ba86fb940d13ca57c89b5ee
# Parent  d240602fb78ba6c6ab0cd035c1b205914582bb86
Add -i and -o options to freebcp.

Input redirection is really just a placeholder for now since we
don't prompt for input when neither -n nor -c is specified.

diff -r d240602fb78b -r 6d37105fefd2 src/apps/freebcp.c
--- a/src/apps/freebcp.c	Wed May 12 11:29:49 2010 -0500
+++ b/src/apps/freebcp.c	Wed May 12 11:40:44 2010 -0500
@@ -24,6 +24,10 @@
 #include <stdio.h>
 #include <ctype.h>
 
+#if HAVE_ERRNO_H
+#include <errno.h>
+#endif /* HAVE_ERRNO_H */
+
 #if HAVE_STDLIB_H
 #include <stdlib.h>
 #endif /* HAVE_STDLIB_H */
@@ -192,7 +196,7 @@
 	 * Get the rest of the arguments 
 	 */
 	optind = 4; /* start processing options after table, direction, & filename */
-	while ((ch = getopt(argc, argv, "m:f:e:F:L:b:t:r:U:P:I:S:h:T:A:O:0:C:ncEdvV")) != -1) {
+	while ((ch = getopt(argc, argv, "m:f:e:F:L:b:t:r:U:P:i:I:S:h:T:A:o:O:0:C:ncEdvV")) != -1) {
 		switch (ch) {
 		case 'v':
 		case 'V':
@@ -263,6 +267,10 @@
 				pdata->pass = strdup(optarg);
 			}
 			break;
+		case 'i':
+			free(pdata->inputfile);
+			pdata->inputfile = strdup(optarg);
+			break;
 		case 'I':
 			pdata->Iflag++;
 			free(pdata->interfacesfile);
@@ -275,6 +283,10 @@
 		case 'h':
 			pdata->hint = strdup(optarg);
 			break;
+		case 'o':
+			free(pdata->outputfile);
+			pdata->outputfile = strdup(optarg);
+			break;
 		case 'O':
 		case '0':
 			pdata->options = strdup(optarg);
@@ -326,6 +338,27 @@
 		}
 	}
 
+	/* 
+	 * Override stdin and/or stdout if requested.
+	 */
+
+	/* FIXME -- Since we don't implement prompting for field data types when neither -c nor -n
+	 * is specified, redirecting stdin doesn't do much yet.
+	 */
+	if (pdata->inputfile) {
+		if (freopen(pdata->inputfile, "rb", stdout) == NULL) {
+			fprintf(stderr, "%s: unable to open %s: %s\n", "freebcp", pdata->inputfile, strerror(errno));
+			exit(1);
+		}
+	}
+	
+	if (pdata->outputfile) {
+		if (freopen(pdata->outputfile, "wb", stdout) == NULL) {
+			fprintf(stderr, "%s: unable to open %s: %s\n", "freebcp", pdata->outputfile, strerror(errno));
+			exit(1);
+		}
+	}
+	
 	return (TRUE);
 
 }
@@ -700,6 +733,7 @@
 	fprintf(stderr, "        [-U username] [-P password] [-I interfaces_file] [-S server]\n");
 	fprintf(stderr, "        [-v] [-d] [-h \"hint [,...]\" [-O \"set connection_option on|off, ...]\"\n");
 	fprintf(stderr, "        [-A packet size] [-T text or image size] [-E]\n");
+	fprintf(stderr, "        [-i input_file] [-o output_file]\n");
 	fprintf(stderr, "        \n");
 	fprintf(stderr, "example: freebcp testdb.dbo.inserttest in inserttest.txt -S mssql -U guest -P password -c\n");
 }
diff -r d240602fb78b -r 6d37105fefd2 src/apps/freebcp.h
--- a/src/apps/freebcp.h	Wed May 12 11:29:49 2010 -0500
+++ b/src/apps/freebcp.h	Wed May 12 11:40:44 2010 -0500
@@ -70,5 +70,7 @@
 	int Tflag;
 	int Aflag;
 	int Eflag;
+        char *inputfile;
+        char *outputfile;
 }
 BCPPARAMDATA;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.