LCOV - code coverage report
Current view: top level - src/dblib - bcp.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 445 894 49.8 %
Date: 2025-04-13 14:39:30 Functions: 25 34 73.5 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
       3             :  * Copyright (C) 2010, 2011  Frediano Ziglio
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or
       6             :  * modify it under the terms of the GNU Library General Public
       7             :  * License as published by the Free Software Foundation; either
       8             :  * version 2 of the License, or (at your option) any later version.
       9             :  *
      10             :  * This library is distributed in the hope that it will be useful,
      11             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13             :  * Library General Public License for more details.
      14             :  *
      15             :  * You should have received a copy of the GNU Library General Public
      16             :  * License along with this library; if not, write to the
      17             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      18             :  * Boston, MA 02111-1307, USA.
      19             :  */
      20             : 
      21             : #include <config.h>
      22             : 
      23             : #include <stdarg.h>
      24             : #include <stdio.h>
      25             : #include <assert.h>
      26             : 
      27             : #if HAVE_STRING_H
      28             : #include <string.h>
      29             : #endif /* HAVE_STRING_H */
      30             : 
      31             : #if HAVE_STDLIB_H
      32             : #include <stdlib.h>
      33             : #endif /* HAVE_STDLIB_H */
      34             : 
      35             : #if HAVE_UNISTD_H
      36             : #include <unistd.h>
      37             : #endif /* HAVE_UNISTD_H */
      38             : 
      39             : #ifdef _WIN32
      40             : #include <io.h>
      41             : #endif
      42             : 
      43             : #include <freetds/tds.h>
      44             : #include <freetds/iconv.h>
      45             : #include <freetds/convert.h>
      46             : #include <freetds/bytes.h>
      47             : #include <freetds/utils/string.h>
      48             : #include <freetds/encodings.h>
      49             : #include <freetds/replacements.h>
      50             : #include <sybfront.h>
      51             : #include <sybdb.h>
      52             : #include <syberror.h>
      53             : #include <dblib.h>
      54             : 
      55             : #define HOST_COL_CONV_ERROR 1
      56             : #define HOST_COL_NULL_ERROR 2
      57             : 
      58             : #ifdef HAVE_FSEEKO
      59             : typedef off_t offset_type;
      60             : #elif defined(_WIN32) || defined(_WIN64)
      61             : /* win32 version */
      62             : typedef __int64 offset_type;
      63             : # if defined(HAVE__FSEEKI64) && defined(HAVE__FTELLI64)
      64             : #  define fseeko(f,o,w) _fseeki64((f),o,w)
      65             : #  define ftello(f) _ftelli64((f))
      66             : # else
      67             : #  define fseeko(f,o,w) (_lseeki64(fileno(f),o,w) == -1 ? -1 : 0)
      68             : #  define ftello(f) _telli64(fileno(f))
      69             : # endif
      70             : #else
      71             : /* use old version */
      72             : #define fseeko(f,o,w) fseek(f,o,w)
      73             : #define ftello(f) ftell(f)
      74             : typedef long offset_type;
      75             : #endif
      76             : 
      77             : static void _bcp_free_storage(DBPROCESS * dbproc);
      78             : static void _bcp_free_columns(DBPROCESS * dbproc);
      79             : static void _bcp_null_error(TDSBCPINFO *bcpinfo, int index, int offset);
      80             : static TDSRET _bcp_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset);
      81             : static TDSRET _bcp_no_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset);
      82             : 
      83             : static int rtrim(char *, int);
      84             : static int rtrim_u16(uint16_t *str, int len, uint16_t space);
      85             : static STATUS _bcp_read_hostfile(DBPROCESS * dbproc, FILE * hostfile, bool *row_error, bool skip);
      86             : static int _bcp_readfmt_colinfo(DBPROCESS * dbproc, char *buf, BCP_HOSTCOLINFO * ci);
      87             : static int _bcp_get_term_var(const BYTE * pdata, const BYTE * term, int term_len);
      88             : 
      89             : /*
      90             :  * "If a host file is being used ... the default data formats are as follows:
      91             :  *
      92             :  *  > The order, type, length and number of the columns in the host file are 
      93             :  *    assumed to be identical to the order, type and number of the columns in the database table.
      94             :  *  > If a given database column's data is fixed-length, 
      95             :  *    then the host file's  data column will also be fixed-length. 
      96             :  *  > If a given database column's data is variable-length or may contain null values, 
      97             :  *    the host file's data column will be prefixed by 
      98             :  *      a 4-byte length value for SYBTEXT and SYBIMAGE data types, and 
      99             :  *      a 1-byte length value for all other types.
     100             :  *  > There are no terminators of any kind between host file columns."
     101             :  */
     102             : 
     103             : static void
     104         268 : init_hostfile_columns(DBPROCESS *dbproc)
     105             : {
     106         268 :         const int ncols = dbproc->bcpinfo->bindinfo->num_cols;
     107             :         RETCODE erc;
     108             :         int icol;
     109             :         
     110         268 :         if (ncols == 0)
     111             :                 return;
     112             :                 
     113         268 :         if ((erc = bcp_columns(dbproc, ncols)) != SUCCEED) {
     114           0 :                 assert(erc == SUCCEED);
     115             :                 return;
     116             :         }
     117             :                 
     118        1748 :         for (icol = 0; icol < ncols; icol++) {
     119        1480 :                 const TDSCOLUMN *pcol = dbproc->bcpinfo->bindinfo->columns[icol];
     120        1480 :                 int prefixlen = 0, termlen = 0;
     121             :                 
     122        1480 :                 switch (pcol->column_type) {
     123             :                 case SYBTEXT:
     124             :                 case SYBIMAGE:
     125             :                         prefixlen = 4;
     126             :                         break;
     127        1384 :                 default:
     128        1384 :                         prefixlen = dbvarylen(dbproc, icol+1)? 1 : 0;
     129             :                 }
     130             :                 
     131        1480 :                 erc = bcp_colfmt(dbproc, icol+1, pcol->column_type, prefixlen, pcol->column_size, NULL, termlen, icol+1);
     132             :                 
     133        1480 :                 assert(erc == SUCCEED);
     134             :                 if (erc != SUCCEED)
     135             :                         return;
     136             :         }
     137             : }
     138             : 
     139             : 
     140             : /** 
     141             :  * \ingroup dblib_bcp
     142             :  * \brief Prepare for bulk copy operation on a table
     143             :  *
     144             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     145             :  * \param tblname the name of the table receiving or providing the data.
     146             :  * \param hfile the data file opposite the table, if any.  
     147             :  * \param errfile the "error file" captures messages and, if errors are encountered, 
     148             :  *      copies of any rows that could not be written to the table.
     149             :  * \param direction one of 
     150             :  *              - \b DB_IN writing to the table
     151             :  *              - \b DB_OUT writing to the host file
     152             :  *              .
     153             :  * \remarks bcp_init() sets the host file data format and acquires the table metadata.
     154             :  *      It is called before the other bulk copy functions. 
     155             :  *
     156             :  *      When writing to a table, bcp can use as its data source a data file (\a hfile), 
     157             :  *      or program data in an application's variables.  In the latter case, call bcp_bind() 
     158             :  *      to associate your data with the appropriate table column.  
     159             :  * \return SUCCEED or FAIL.
     160             :  * \sa  BCP_SETL(), bcp_bind(), bcp_done(), bcp_exec()
     161             :  */
     162             : RETCODE
     163         306 : bcp_init(DBPROCESS * dbproc, const char *tblname, const char *hfile, const char *errfile, int direction)
     164             : {
     165         306 :         tdsdump_log(TDS_DBG_FUNC, "bcp_init(%p, %s, %s, %s, %d)\n", 
     166             :                         dbproc, tblname? tblname:"NULL", hfile? hfile:"NULL", errfile? errfile:"NULL", direction);
     167         306 :         CHECK_CONN(FAIL);
     168             : 
     169             :         /* 
     170             :          * Validate other parameters 
     171             :          */
     172         306 :         if (dbproc->tds_socket->conn->tds_version < 0x500) {
     173           0 :                 dbperror(dbproc, SYBETDSVER, 0);
     174           0 :                 return FAIL;
     175             :         }
     176             : 
     177         306 :         if (tblname == NULL) {
     178           0 :                 dbperror(dbproc, SYBEBCITBNM, 0);
     179           0 :                 return FAIL;
     180             :         }
     181             : 
     182         350 :         if (direction != DB_QUERYOUT && !IS_TDS7_PLUS(dbproc->tds_socket->conn) &&
     183          44 :             strlen(tblname) > 92) {  /* 30.30.30 for Sybase */
     184           0 :                 dbperror(dbproc, SYBEBCITBLEN, 0);
     185           0 :                 return FAIL;
     186             :         }
     187             : 
     188         306 :         if (direction != DB_IN && direction != DB_OUT && direction != DB_QUERYOUT) {
     189           0 :                 dbperror(dbproc, SYBEBDIO, 0);
     190           0 :                 return FAIL;
     191             :         }
     192             : 
     193             :         /* Free previously allocated storage in dbproc & initialise flags, etc. */
     194         306 :         _bcp_free_storage(dbproc);
     195             : 
     196             :         /* Allocate storage */
     197         306 :         dbproc->bcpinfo = tds_alloc_bcpinfo();
     198         306 :         if (dbproc->bcpinfo == NULL)
     199             :                 goto memory_error;
     200             : 
     201         306 :         if (!tds_dstr_copy(&dbproc->bcpinfo->tablename, tblname))
     202             :                 goto memory_error;
     203             : 
     204         306 :         dbproc->bcpinfo->direction = direction;
     205             : 
     206         306 :         dbproc->bcpinfo->xfer_init = false;
     207         306 :         dbproc->bcpinfo->bind_count = 0;
     208             : 
     209         306 :         if (TDS_FAILED(tds_bcp_init(dbproc->tds_socket, dbproc->bcpinfo))) {
     210             :                 /* TODO return proper error */
     211             :                 /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */
     212           0 :                 dbperror(dbproc, SYBEBCNT, 0);
     213           0 :                 return FAIL;
     214             :         }
     215             : 
     216             :         /* Prepare default hostfile columns */
     217             :         
     218         306 :         if (hfile == NULL) {
     219          38 :                 dbproc->hostfileinfo = NULL;
     220          38 :                 return SUCCEED;
     221             :         }
     222             : 
     223         268 :         dbproc->hostfileinfo = tds_new0(BCP_HOSTFILEINFO, 1);
     224             : 
     225         268 :         if (dbproc->hostfileinfo == NULL)
     226             :                 goto memory_error;
     227         268 :         dbproc->hostfileinfo->maxerrs = 10;
     228         268 :         dbproc->hostfileinfo->firstrow = 1;
     229         268 :         if ((dbproc->hostfileinfo->hostfile = strdup(hfile)) == NULL)
     230             :                 goto memory_error;
     231             : 
     232         268 :         if (errfile != NULL)
     233         268 :                 if ((dbproc->hostfileinfo->errorfile = strdup(errfile)) == NULL)
     234             :                         goto memory_error;
     235             : 
     236         268 :         init_hostfile_columns(dbproc);
     237             : 
     238         268 :         return SUCCEED;
     239             : 
     240           0 : memory_error:
     241           0 :         _bcp_free_storage(dbproc);
     242           0 :         dbperror(dbproc, SYBEMEM, ENOMEM);
     243           0 :         return FAIL;
     244             : }
     245             : 
     246             : 
     247             : /** 
     248             :  * \ingroup dblib_bcp
     249             :  * \brief Set the length of a host variable to be written to a table.
     250             :  * 
     251             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     252             :  * \param varlen size of the variable, in bytes, or
     253             :  *      - \b 0 indicating NULL
     254             :  *      - \b -1 indicating size is determined by the prefix or terminator.  
     255             :  *              (If both a prefix and a terminator are present, bcp is supposed to use the smaller of the 
     256             :  *               two.  This feature might or might not actually work.)
     257             :  * \param table_column the number of the column in the table (starting with 1, not zero).
     258             :  * 
     259             :  * \return SUCCEED or FAIL.
     260             :  * \sa  bcp_bind(), bcp_colptr(), bcp_sendrow() 
     261             :  */
     262             : RETCODE
     263         130 : bcp_collen(DBPROCESS * dbproc, DBINT varlen, int table_column)
     264             : {
     265             :         TDSCOLUMN *bcpcol;
     266             : 
     267         130 :         tdsdump_log(TDS_DBG_FUNC, "bcp_collen(%p, %d, %d)\n", dbproc, varlen, table_column);
     268             :         
     269         130 :         CHECK_CONN(FAIL);
     270         130 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);            /* not initialized */
     271         130 :         DBPERROR_RETURN(dbproc->bcpinfo->direction != DB_IN, SYBEBCPN)    /* not inbound */
     272         130 :         DBPERROR_RETURN(dbproc->hostfileinfo != NULL, SYBEBCPI)              /* cannot have data file */
     273         130 :         CHECK_PARAMETER(0 < table_column && 
     274             :                             table_column <= dbproc->bcpinfo->bindinfo->num_cols, SYBECNOR, FAIL);
     275             :         
     276         130 :         bcpcol = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
     277             : 
     278             :         /* Sybase library does not check for NULL here, only sending, so don't
     279             :          * check and send SYBEBCNN */
     280         130 :         bcpcol->column_bindlen = varlen;
     281             : 
     282         130 :         return SUCCEED;
     283             : }
     284             : 
     285             : /** 
     286             :  * \ingroup dblib_bcp
     287             :  * \brief Indicate how many columns are to be found in the datafile.
     288             :  * 
     289             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     290             :  * \param host_colcount count of columns in the datafile, irrespective of how many you intend to use. 
     291             :  * \remarks This function describes the file as it is, not how it will be used.  
     292             :  * 
     293             :  * \return SUCCEED or FAIL.  It's hard to see how it could fail.  
     294             :  * \sa  bcp_colfmt()    
     295             :  */
     296             : RETCODE
     297         536 : bcp_columns(DBPROCESS * dbproc, int host_colcount)
     298             : {
     299             :         int i;
     300             : 
     301         536 :         tdsdump_log(TDS_DBG_FUNC, "bcp_columns(%p, %d)\n", dbproc, host_colcount);
     302         536 :         CHECK_CONN(FAIL);
     303         536 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     304         536 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
     305             : 
     306         536 :         if (host_colcount < 1) {
     307           0 :                 dbperror(dbproc, SYBEBCFO, 0);
     308           0 :                 return FAIL;
     309             :         }
     310             : 
     311         536 :         _bcp_free_columns(dbproc);
     312             : 
     313         536 :         dbproc->hostfileinfo->host_columns = tds_new0(BCP_HOSTCOLINFO *, host_colcount);
     314         536 :         if (dbproc->hostfileinfo->host_columns == NULL) {
     315           0 :                 dbperror(dbproc, SYBEMEM, ENOMEM);
     316           0 :                 return FAIL;
     317             :         }
     318             : 
     319         536 :         dbproc->hostfileinfo->host_colcount = host_colcount;
     320             : 
     321        3496 :         for (i = 0; i < host_colcount; i++) {
     322        2960 :                 dbproc->hostfileinfo->host_columns[i] = tds_new0(BCP_HOSTCOLINFO, 1);
     323        2960 :                 if (dbproc->hostfileinfo->host_columns[i] == NULL) {
     324           0 :                         dbproc->hostfileinfo->host_colcount = i;
     325           0 :                         _bcp_free_columns(dbproc);
     326           0 :                         dbperror(dbproc, SYBEMEM, ENOMEM);
     327           0 :                         return FAIL;
     328             :                 }
     329             :         }
     330             : 
     331             :         return SUCCEED;
     332             : }
     333             : 
     334             : /** 
     335             :  * \ingroup dblib_bcp
     336             :  * \brief Specify the format of a datafile prior to writing to a table.
     337             :  * 
     338             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     339             :  * \param host_colnum datafile column number (starting with 1, not zero).
     340             :  * \param host_type dataype token describing the data type in \a host_colnum.  E.g. SYBCHAR for character data.
     341             :  * \param host_prefixlen size of the prefix in the datafile column, if any.  For delimited files: zero.  
     342             :  *                      May be 0, 1, 2, or 4 bytes.  The prefix will be read as an integer (not a character string) from the 
     343             :  *                      data file, and will be interpreted the data size of that column, in bytes.  
     344             :  * \param host_collen maximum size of datafile column, exclusive of any prefix/terminator.  Just the data, ma'am.  
     345             :  *              Special values:
     346             :  *                      - \b 0 indicates NULL.  
     347             :  *                      - \b -1 for fixed-length non-null datatypes
     348             :  *                      - \b -1 for variable-length datatypes (e.g. SYBCHAR) where the length is established 
     349             :  *                              by a prefix/terminator.  
     350             :  * \param host_term the sequence of characters that will serve as a column terminator (delimiter) in the datafile.  
     351             :  *                      Often a tab character, but can be any string of any length.  Zero indicates no terminator.  
     352             :  *                      Special characters:
     353             :  *                              - \b '\\0' terminator is an ASCII NUL.
     354             :  *                              - \b '\\t' terminator is an ASCII TAB.
     355             :  *                              - \b '\\n' terminator is an ASCII NL.
     356             :  * \param host_termlen the length of \a host_term, in bytes. 
     357             :  * \param table_colnum Nth column, starting at 1, in the table that maps to \a host_colnum.  
     358             :  *      If there is a column in the datafile that does not map to a table column, set \a table_colnum to zero.  
     359             :  *
     360             :  *\remarks  bcp_colfmt() is called once for each column in the datafile, as specified with bcp_columns().  
     361             :  * In so doing, you describe to FreeTDS how to parse each line of your datafile, and where to send each field.  
     362             :  *
     363             :  * When a prefix or terminator is used with variable-length data, \a host_collen may have one of three values:
     364             :  *              - \b positive indicating the maximum number of bytes to be used
     365             :  *              - \b 0 indicating NULL
     366             :  *              - \b -1 indicating no maximum; all data, as described by the prefix/terminator will be used.  
     367             :  *              .
     368             :  * \return SUCCEED or FAIL.
     369             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(),
     370             :  *      bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_sendrow
     371             :  */
     372             : RETCODE
     373        2960 : bcp_colfmt(DBPROCESS * dbproc, int host_colnum, int host_type, int host_prefixlen, DBINT host_collen, const BYTE * host_term,
     374             :            int host_termlen, int table_colnum)
     375             : {
     376             :         BCP_HOSTCOLINFO *hostcol;
     377        2960 :         BYTE *terminator = NULL;
     378             : 
     379        2960 :         tdsdump_log(TDS_DBG_FUNC, "bcp_colfmt(%p, %d, %d, %d, %d, %p, %d, %d)\n", 
     380             :                     dbproc, host_colnum, host_type, host_prefixlen, (int) host_collen, host_term, host_termlen, table_colnum);
     381        2960 :         CHECK_CONN(FAIL);
     382        2960 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     383        2960 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
     384             : 
     385             :         /* Microsoft specifies a "file_termlen" of zero if there's no terminator */
     386        2960 :         if (dbproc->msdblib && host_termlen == 0)
     387             :                 host_termlen = -1;
     388             : 
     389        2960 :         if (host_termlen < 0)
     390           0 :                 host_termlen = -1;
     391             : 
     392        2960 :         if (dbproc->hostfileinfo->host_colcount == 0) {
     393           0 :                 dbperror(dbproc, SYBEBCBC, 0);
     394           0 :                 return FAIL;
     395             :         }
     396             : 
     397        2960 :         if (host_colnum < 1) {
     398           0 :                 dbperror(dbproc, SYBEBCFO, 0);
     399           0 :                 return FAIL;
     400             :         }
     401             : 
     402        2960 :         if (host_colnum > dbproc->hostfileinfo->host_colcount) {
     403           0 :                 dbperror(dbproc, SYBECNOR, 0);
     404           0 :                 return FAIL;
     405             :         }
     406             : 
     407        2960 :         if (host_prefixlen != 0 && host_prefixlen != 1 && host_prefixlen != 2 && host_prefixlen != 4 && host_prefixlen != -1) {
     408           0 :                 dbperror(dbproc, SYBEBCPREF, 0);
     409           0 :                 return FAIL;
     410             :         }
     411             : 
     412             :         /* if column is not copied you cannot specify destination type */
     413        2960 :         if (table_colnum <= 0 && host_type == 0) {
     414           0 :                 dbperror(dbproc, SYBEBCPCTYP, 0);
     415           0 :                 return FAIL;
     416             :         }
     417             : 
     418        5920 :         if (table_colnum > 0 && !is_tds_type_valid(host_type)) {
     419           0 :                 dbperror(dbproc, SYBEUDTY, 0);
     420           0 :                 return FAIL;
     421             :         }
     422             : 
     423        2960 :         if (host_type && host_prefixlen == 0 && host_collen == -1 && host_termlen == -1 && !is_fixed_type(host_type)) {
     424           0 :                 dbperror(dbproc, SYBEVDPT, 0);
     425           0 :                 return FAIL;
     426             :         }
     427             : 
     428        2960 :         if (host_collen < -1) {
     429           0 :                 dbperror(dbproc, SYBEBCHLEN, 0);
     430           0 :                 return FAIL;
     431             :         }
     432             : 
     433             :         /* No official error message.  Fix and warn. */
     434        2960 :         if (is_fixed_type(host_type) && (host_collen != -1 && host_collen != 0)) {
     435         620 :                 tdsdump_log(TDS_DBG_FUNC,
     436             :                             "bcp_colfmt: changing host_collen to -1 from %d for fixed type %d.\n", 
     437             :                             host_collen, host_type);
     438             :                 host_collen = -1;
     439             :         }
     440             : 
     441             :         /* 
     442             :          * If there's a positive terminator length, we need a valid terminator pointer.
     443             :          * If the terminator length is 0 or -1, then there's no terminator.
     444             :          */
     445        2960 :         if (host_term == NULL && host_termlen > 0) {
     446           0 :                 dbperror(dbproc, SYBEVDPT, 0);  /* "all variable-length data must have either a length-prefix ..." */
     447           0 :                 return FAIL;
     448             :         }
     449             : 
     450        2960 :         hostcol = dbproc->hostfileinfo->host_columns[host_colnum - 1];
     451             : 
     452             :         /* TODO add precision scale and join with bcp_colfmt_ps */
     453        2960 :         if (host_term && host_termlen > 0) {
     454        1440 :                 if ((terminator = tds_new(BYTE, host_termlen)) == NULL) {
     455           0 :                         dbperror(dbproc, SYBEMEM, errno);
     456           0 :                         return FAIL;
     457             :                 }
     458        1440 :                 memcpy(terminator, host_term, host_termlen);
     459             :         }
     460        2960 :         hostcol->host_column = host_colnum;
     461        2960 :         hostcol->datatype = host_type ? (TDS_SERVER_TYPE) host_type : TDS_INVALID_TYPE;
     462        2960 :         hostcol->prefix_len = host_prefixlen;
     463        2960 :         hostcol->column_len = host_collen;
     464        2960 :         free(hostcol->terminator);
     465        2960 :         hostcol->terminator = terminator;
     466        2960 :         hostcol->term_len = host_termlen;
     467        2960 :         hostcol->tab_colnum = table_colnum;
     468             : 
     469        2960 :         return SUCCEED;
     470             : }
     471             : 
     472             : /** 
     473             :  * \ingroup dblib_bcp
     474             :  * \brief Specify the format of a host file for bulk copy purposes, 
     475             :  *      with precision and scale support for numeric and decimal columns.
     476             :  * 
     477             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     478             :  * \param host_colnum datafile column number (starting with 1, not zero).
     479             :  * \param host_type dataype token describing the data type in \a host_colnum.  E.g. SYBCHAR for character data.
     480             :  * \param host_prefixlen size of the prefix in the datafile column, if any.  For delimited files: zero.  
     481             :  *                      May be 0, 1, 2, or 4 bytes.  The prefix will be read as an integer (not a character string) from the 
     482             :  *                      data file, and will be interpreted the data size of that column, in bytes.  
     483             :  * \param host_collen maximum size of datafile column, exclusive of any prefix/terminator.  Just the data, ma'am.  
     484             :  *              Special values:
     485             :  *                      - \b 0 indicates NULL.  
     486             :  *                      - \b -1 for fixed-length non-null datatypes
     487             :  *                      - \b -1 for variable-length datatypes (e.g. SYBCHAR) where the length is established 
     488             :  *                              by a prefix/terminator.  
     489             :  * \param host_term the sequence of characters that will serve as a column terminator (delimiter) in the datafile.  
     490             :  *                      Often a tab character, but can be any string of any length.  Zero indicates no terminator.  
     491             :  *                      Special characters:
     492             :  *                              - \b '\\0' terminator is an ASCII NUL.
     493             :  *                              - \b '\\t' terminator is an ASCII TAB.
     494             :  *                              - \b '\\n' terminator is an ASCII NL.
     495             :  * \param host_termlen the length of \a host_term, in bytes. 
     496             :  * \param table_colnum Nth column, starting at 1, in the table that maps to \a host_colnum.  
     497             :  *      If there is a column in the datafile that does not map to a table column, set \a table_colnum to zero.  
     498             :  * \param typeinfo something
     499             :  * \todo Not implemented.
     500             :  * \return SUCCEED or FAIL.
     501             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
     502             :  *      bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_sendrow
     503             :  */
     504             : RETCODE
     505           0 : bcp_colfmt_ps(DBPROCESS * dbproc, int host_colnum, int host_type,
     506             :               int host_prefixlen, DBINT host_collen, BYTE * host_term, int host_termlen, int table_colnum, DBTYPEINFO * typeinfo)
     507             : {
     508           0 :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_colfmt_ps(%p, %d, %d, %d, %d, %p, %d, %d, %p)\n",
     509             :                     dbproc, host_colnum, host_type, host_prefixlen, (int) host_collen,
     510             :                     host_term, host_termlen, table_colnum, typeinfo);
     511           0 :         CHECK_CONN(FAIL);
     512           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     513             :         
     514             :         /* dbperror(dbproc, , 0);        Illegal precision specified */
     515             : 
     516             :         /* TODO see bcp_colfmt */
     517             :         return FAIL;
     518             : }
     519             : 
     520             : 
     521             : /** 
     522             :  * \ingroup dblib_bcp
     523             :  * \brief Set BCP options for uploading a datafile
     524             :  * 
     525             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     526             :  * \param field symbolic constant indicating the option to be set, one of:
     527             :  *              - \b BCPMAXERRS Maximum errors tolerated before quitting. The default is 10.
     528             :  *              - \b BCPFIRST The first row to read in the datafile. The default is 1. 
     529             :  *              - \b BCPLAST The last row to read in the datafile. The default is to copy all rows. A value of
     530             :  *                      -1 resets this field to its default?
     531             :  *              - \b BCPBATCH The number of rows per batch.  Default is 0, meaning a single batch. 
     532             :  * \param value The value for \a field.
     533             :  *
     534             :  * \remarks These options control the behavior of bcp_exec().  
     535             :  * When writing to a table from application host memory variables, 
     536             :  * program logic controls error tolerance and batch size. 
     537             :  * 
     538             :  * \return SUCCEED or FAIL.
     539             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_done(), bcp_exec(), bcp_options()
     540             :  */
     541             : RETCODE
     542           0 : bcp_control(DBPROCESS * dbproc, int field, DBINT value)
     543             : {
     544           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_control(%p, %d, %d)\n", dbproc, field, value);
     545           0 :         CHECK_CONN(FAIL);
     546           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     547             : 
     548           0 :         if (field == BCPKEEPIDENTITY) {
     549           0 :                 dbproc->bcpinfo->identity_insert_on = (value != 0);
     550           0 :                 return SUCCEED;
     551             :         }
     552             : 
     553           0 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBIVI, FAIL);
     554             : 
     555           0 :         switch (field) {
     556             : 
     557           0 :         case BCPMAXERRS:
     558           0 :                 if (value < 1)
     559           0 :                         value = 10;
     560           0 :                 dbproc->hostfileinfo->maxerrs = value;
     561           0 :                 break;
     562           0 :         case BCPFIRST:
     563           0 :                 if (value < 1)
     564           0 :                         value = 1;
     565           0 :                 dbproc->hostfileinfo->firstrow = value;
     566           0 :                 break;
     567           0 :         case BCPLAST:
     568           0 :                 dbproc->hostfileinfo->lastrow = value;
     569           0 :                 break;
     570           0 :         case BCPBATCH:
     571           0 :                 dbproc->hostfileinfo->batch = value;
     572           0 :                 break;
     573             : 
     574           0 :         default:
     575           0 :                 dbperror(dbproc, SYBEIFNB, 0);
     576           0 :                 return FAIL;
     577             :         }
     578             :         return SUCCEED;
     579             : }
     580             : 
     581             : /*
     582             :  * \ingroup dblib_bcp
     583             :  * \brief Get BCP batch option
     584             :  * 
     585             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     586             :  * \remarks This function is specific to FreeTDS.  
     587             :  * 
     588             :  * \return the value that was set by bcp_control.
     589             :  * \sa  bcp_batch(), bcp_control()
     590             :  */
     591             : int
     592           0 : bcp_getbatchsize(DBPROCESS * dbproc)
     593             : {
     594           0 :         return dbproc->hostfileinfo->batch;
     595             : }
     596             : 
     597             : /** 
     598             :  * \ingroup dblib_bcp
     599             :  * \brief Set "hints" for uploading a file.  A FreeTDS-only function.  
     600             :  * 
     601             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     602             :  * \param option symbolic constant indicating the option to be set, one of:
     603             :  *              - \b BCPLABELED Not implemented.
     604             :  *              - \b BCPHINTS The hint to be passed when the bulk-copy begins.  
     605             :  * \param value The string constant for \a option a/k/a the hint.  One of:
     606             :  *              - \b ORDER The data are ordered in accordance with the table's clustered index.
     607             :  *              - \b ROWS_PER_BATCH The batch size
     608             :  *              - \b KILOBYTES_PER_BATCH The approximate number of kilobytes to use for a batch size
     609             :  *              - \b TABLOCK Lock the table
     610             :  *              - \b CHECK_CONSTRAINTS Apply constraints
     611             :  *              - \b FIRE_TRIGGERS Fire any INSERT triggers on the target table
     612             :  * \param valuelen The strlen of \a value.  
     613             :  * 
     614             :  * \return SUCCEED or FAIL.
     615             :  * \sa  bcp_control(), 
     616             :  *      bcp_exec(), 
     617             :  * \todo Simplify.  Remove \a valuelen.
     618             :  */
     619             : RETCODE
     620           0 : bcp_options(DBPROCESS * dbproc, int option, BYTE * value, int valuelen)
     621             : {
     622             :         int i;
     623             :         static const char *const hints[] = {
     624             :                 "ORDER", "ROWS_PER_BATCH", "KILOBYTES_PER_BATCH", "TABLOCK", "CHECK_CONSTRAINTS",
     625             :                 "FIRE_TRIGGERS", "KEEP_NULLS", NULL
     626             :         };
     627             : 
     628           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_options(%p, %d, %p, %d)\n", dbproc, option, value, valuelen);
     629           0 :         CHECK_CONN(FAIL);
     630           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     631           0 :         CHECK_NULP(value, "bcp_options", 3, FAIL);
     632             : 
     633           0 :         switch (option) {
     634           0 :         case BCPLABELED:
     635           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED bcp option: BCPLABELED\n");
     636             :                 break;
     637           0 :         case BCPHINTS:
     638           0 :                 if (!value || valuelen <= 0)
     639             :                         break;
     640             : 
     641           0 :                 for (i = 0; hints[i]; i++) {    /* look up hint */
     642           0 :                         if (strncasecmp((char *) value, hints[i], strlen(hints[i])) == 0) {
     643           0 :                                 if (!tds_dstr_copy(&dbproc->bcpinfo->hint, hints[i]))
     644             :                                         return FAIL;
     645           0 :                                 return SUCCEED;
     646             :                         }
     647             :                 }
     648           0 :                 tdsdump_log(TDS_DBG_FUNC, "failed, no such hint\n");
     649             :                 break;
     650           0 :         default:
     651           0 :                 tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED bcp option: %u\n", option);
     652             :                 break;
     653             :         }
     654           0 :         return FAIL;
     655             : }
     656             : 
     657             : /** 
     658             :  * \ingroup dblib_bcp
     659             :  * \brief Override bcp_bind() by pointing to a different host variable.
     660             :  * 
     661             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     662             :  * \param colptr The pointer, the address of your variable. 
     663             :  * \param table_column The 1-based column ordinal in the table.  
     664             :  * \remarks Use between calls to bcp_sendrow().  After calling bcp_colptr(), 
     665             :  *              subsequent calls to bcp_sendrow() will bind to the new address.  
     666             :  * \return SUCCEED or FAIL.
     667             :  * \sa  bcp_bind(), bcp_collen(), bcp_sendrow() 
     668             :  */
     669             : RETCODE
     670           0 : bcp_colptr(DBPROCESS * dbproc, BYTE * colptr, int table_column)
     671             : {
     672             :         TDSCOLUMN *curcol;
     673             : 
     674           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_colptr(%p, %p, %d)\n", dbproc, colptr, table_column);
     675           0 :         CHECK_CONN(FAIL);
     676           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
     677           0 :         CHECK_PARAMETER(dbproc->bcpinfo->bindinfo, SYBEBCPI, FAIL);
     678             :         /* colptr can be NULL */
     679             : 
     680           0 :         if (dbproc->bcpinfo->direction != DB_IN) {
     681           0 :                 dbperror(dbproc, SYBEBCPN, 0);
     682           0 :                 return FAIL;
     683             :         }
     684           0 :         if (table_column <= 0 || table_column > dbproc->bcpinfo->bindinfo->num_cols) {
     685           0 :                 dbperror(dbproc, SYBEBCPN, 0);
     686           0 :                 return FAIL;
     687             :         }
     688             :         
     689           0 :         curcol = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
     690           0 :         curcol->column_varaddr = (TDS_CHAR *)colptr;
     691             : 
     692           0 :         return SUCCEED;
     693             : }
     694             : 
     695             : 
     696             : /** 
     697             :  * \ingroup dblib_bcp
     698             :  * \brief See if BCP_SETL() was used to set the LOGINREC for BCP work.  
     699             :  * 
     700             :  * \param login Address of the LOGINREC variable to be passed to dbopen(). 
     701             :  * 
     702             :  * \return TRUE or FALSE.
     703             :  * \sa  BCP_SETL(), bcp_init(), dblogin(), dbopen()
     704             :  */
     705             : DBBOOL
     706          10 : bcp_getl(LOGINREC * login)
     707             : {
     708          10 :         TDSLOGIN *tdsl = login->tds_login;
     709             : 
     710          10 :         tdsdump_log(TDS_DBG_FUNC, "bcp_getl(%p)\n", login);
     711             : 
     712          10 :         return (tdsl->bulk_copy);
     713             : }
     714             : 
     715             : /**
     716             :  * Convert column for output (usually to a file)
     717             :  * Conversion is slightly different from input as:
     718             :  * - date is formatted differently;
     719             :  * - you have to set properly numeric while on input the column metadata are
     720             :  *   used;
     721             :  * - we need to make sure buffer is always at least a minimum bytes.
     722             :  */
     723             : static int
     724        1386 : _bcp_convert_out(DBPROCESS * dbproc, TDSCOLUMN *curcol, BCP_HOSTCOLINFO *hostcol, TDS_UCHAR **p_data, const char *bcpdatefmt)
     725             : {
     726             :         BYTE *src;
     727             :         int srclen;
     728             :         int buflen;
     729        1386 :         int srctype = tds_get_conversion_type(curcol->column_type, curcol->column_size);
     730             : 
     731        1386 :         src = curcol->column_data;
     732        1386 :         if (is_blob_col(curcol))
     733          84 :                 src = (BYTE *) ((TDSBLOB *) src)->textvalue;
     734             : 
     735        1386 :         srclen = curcol->column_cur_size;
     736             : 
     737             :         /*
     738             :          * if we are converting datetime to string, need to override any
     739             :          * date time formats already established
     740             :          */
     741        1386 :         if (is_datetime_type(srctype) && is_ascii_type(hostcol->datatype)) {
     742             :                 TDSDATEREC when;
     743             : 
     744          28 :                 tds_datecrack(srctype, src, &when);
     745          28 :                 buflen = (int)tds_strftime((TDS_CHAR *)(*p_data), 256,
     746             :                                          bcpdatefmt, &when, 3);
     747        1358 :         } else if (srclen == 0 && is_variable_type(curcol->column_type)
     748          40 :                    && is_ascii_type(hostcol->datatype)) {
     749             :                 /*
     750             :                  * An empty string is denoted in the output file by a single ASCII NUL
     751             :                  * byte that we request by specifying a destination length of -1.  (Not
     752             :                  * to be confused with a database NULL, which is denoted in the output
     753             :                  * file with an empty string!)
     754             :                  */
     755          40 :                 (*p_data)[0] = 0;
     756          40 :                 buflen = 1;
     757        1318 :         } else if (is_numeric_type(hostcol->datatype)) {
     758           0 :                 TDS_NUMERIC *num = (TDS_NUMERIC *) (*p_data);
     759           0 :                 if (is_numeric_type(srctype)) {
     760           0 :                         TDS_NUMERIC *nsrc = (TDS_NUMERIC *) src;
     761           0 :                         num->precision = nsrc->precision;
     762           0 :                         num->scale = nsrc->scale;
     763             :                 } else {
     764           0 :                         num->precision = 18;
     765           0 :                         num->scale = 0;
     766             :                 }
     767           0 :                 buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) num);
     768           0 :                 if (buflen > 0)
     769           0 :                         buflen = tds_numeric_bytes_per_prec[num->precision] + 2;
     770        1318 :         } else if (!is_variable_type(hostcol->datatype)) {
     771          10 :                 buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) (*p_data));
     772             :         } else {
     773             :                 CONV_RESULT cr;
     774             : 
     775             :                 /*
     776             :                  * For null columns, the above work to determine the output buffer size is moot,
     777             :                  * because bcpcol->data_size is zero, so dbconvert() won't write anything,
     778             :                  * and returns zero.
     779             :                  */
     780        1308 :                 buflen = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, hostcol->datatype, (CONV_RESULT *) &cr);
     781        1308 :                 if (buflen < 0)
     782           0 :                         return buflen;
     783             : 
     784        1308 :                 if (buflen >= 256) {
     785          30 :                         free(*p_data);
     786          30 :                         *p_data = (TDS_UCHAR *) cr.c;
     787             :                 } else {
     788        1278 :                         memcpy(*p_data, cr.c, buflen);
     789        1278 :                         free(cr.c);
     790             :                 }
     791             : 
     792             :                 /*
     793             :                  * Special case:  When outputting database varchar data
     794             :                  * (either varchar or nullable char) conversion may have
     795             :                  * trimmed trailing blanks such that nothing is left.
     796             :                  * In this case we need to put a single blank to the output file.
     797             :                  */
     798        1308 :                 if (is_char_type(curcol->column_type) && srclen > 0 && buflen == 0) {
     799           0 :                         strcpy ((char *) (*p_data), " ");
     800           0 :                         buflen = 1;
     801             :                 }
     802             :         }
     803             :         return buflen;
     804             : }
     805             : 
     806             : static int
     807           0 : bcp_cache_prefix_len(BCP_HOSTCOLINFO *hostcol, const TDSCOLUMN *curcol)
     808             : {
     809             :         int plen;
     810             : 
     811           0 :         if (is_blob_type(hostcol->datatype))
     812             :                 plen = 4;
     813           0 :         else if (is_numeric_type(hostcol->datatype))
     814             :                 plen = 1;
     815           0 :         else if (!is_fixed_type(hostcol->datatype))
     816             :                 plen = 2;
     817           0 :         else if (curcol->column_nullable)
     818             :                 plen = 1;
     819             :         else
     820           0 :                 plen = 0;
     821             :         /* cache */
     822           0 :         return hostcol->prefix_len = plen;
     823             : }
     824             : 
     825             : static RETCODE
     826        1496 : bcp_write_prefix(FILE *hostfile, BCP_HOSTCOLINFO *hostcol, TDSCOLUMN *curcol, int buflen)
     827             : {
     828             :         union {
     829             :                 TDS_TINYINT ti;
     830             :                 TDS_SMALLINT si;
     831             :                 TDS_INT li;
     832             :         } u;
     833             :         int plen;
     834             : 
     835             :         /* compute prefix len if needed */
     836        1496 :         if ((plen = hostcol->prefix_len) == -1)
     837           0 :                 plen = bcp_cache_prefix_len(hostcol, curcol);
     838             : 
     839             :         /* output prefix to file */
     840        1496 :         switch (plen) {
     841             :         default:
     842             :                 return SUCCEED;
     843          10 :         case 1:
     844          10 :                 u.ti = buflen;
     845          10 :                 break;
     846           0 :         case 2:
     847           0 :                 u.si = buflen;
     848           0 :                 break;
     849          10 :         case 4:
     850          10 :                 u.li = buflen;
     851          10 :                 break;
     852             :         }
     853          20 :         if (fwrite(&u, plen, 1, hostfile) == 1)
     854             :                 return SUCCEED;
     855             : 
     856           0 :         return FAIL;
     857             : }
     858             : 
     859             : /**
     860             :  * \ingroup dblib_bcp_internal
     861             :  * \brief
     862             :  *
     863             :  *
     864             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
     865             :  * \param rows_copied
     866             :  *
     867             :  * \return SUCCEED or FAIL.
     868             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
     869             :  */
     870             : static RETCODE
     871         134 : _bcp_exec_out(DBPROCESS * dbproc, DBINT * rows_copied)
     872             : {
     873         134 :         FILE *hostfile = NULL;
     874         134 :         TDS_UCHAR *data = NULL;
     875             :         int i;
     876             : 
     877             :         TDSSOCKET *tds;
     878             :         TDSRESULTINFO *resinfo;
     879         134 :         TDSCOLUMN *curcol = NULL;
     880             :         BCP_HOSTCOLINFO *hostcol;
     881             :         int buflen;
     882             : 
     883             :         TDS_INT result_type;
     884             : 
     885             :         int row_of_query;
     886             :         int rows_written;
     887             :         const char *bcpdatefmt;
     888             :         TDSRET tdsret;
     889             : 
     890         134 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_exec_out(%p, %p)\n", dbproc, rows_copied);
     891         134 :         assert(dbproc);
     892         134 :         assert(rows_copied);
     893             : 
     894         134 :         tds = dbproc->tds_socket;
     895         134 :         assert(tds);
     896             : 
     897         134 :         bcpdatefmt = getenv("FREEBCP_DATEFMT");
     898         134 :         if (!bcpdatefmt)
     899         134 :                 bcpdatefmt = "%Y-%m-%d %H:%M:%S.%z";
     900             : 
     901         134 :         if (dbproc->bcpinfo->direction == DB_QUERYOUT ) {
     902           0 :                 if (TDS_FAILED(tds_submit_query(tds, tds_dstr_cstr(&dbproc->bcpinfo->tablename))))
     903             :                         return FAIL;
     904             :         } else {
     905             :                 /* TODO quote if needed */
     906         268 :                 if (TDS_FAILED(tds_submit_queryf(tds, "select * from %s", tds_dstr_cstr(&dbproc->bcpinfo->tablename))))
     907             :                         return FAIL;
     908             :         }
     909             : 
     910         134 :         tdsret = tds_process_tokens(tds, &result_type, NULL, TDS_TOKEN_RESULTS);
     911         134 :         if (TDS_FAILED(tdsret))
     912             :                 return FAIL;
     913             : 
     914         134 :         if (!tds->res_info) {
     915             :                 /* TODO flush/cancel to keep consistent state */
     916             :                 return FAIL;
     917             :         }
     918             : 
     919         134 :         resinfo = tds->res_info;
     920             : 
     921         134 :         row_of_query = 0;
     922         134 :         rows_written = 0;
     923             : 
     924             :         /* allocate at least 256 bytes */
     925             :         /* allocate data for buffer conversion */
     926         134 :         data = tds_new(TDS_UCHAR, 256);
     927         134 :         if (!data) {
     928           0 :                 dbperror(dbproc, SYBEMEM, errno);
     929             :                 goto Cleanup;
     930             :         }
     931             : 
     932             :         /*
     933             :          * TODO above we allocate many buffer just to convert and store
     934             :          * to file.. avoid all that passages...
     935             :          */
     936             : 
     937         134 :         if (!(hostfile = fopen(dbproc->hostfileinfo->hostfile, "w"))) {
     938           0 :                 dbperror(dbproc, SYBEBCUO, errno);
     939           0 :                 goto Cleanup;
     940             :         }
     941             : 
     942             :         /* fetch a row of data from the server */
     943             : 
     944         404 :         while (tds_process_tokens(tds, &result_type, NULL, TDS_STOPAT_ROWFMT|TDS_RETURN_DONE|TDS_RETURN_ROW|TDS_RETURN_COMPUTE)
     945             :                 == TDS_SUCCESS) {
     946             : 
     947         404 :                 if (result_type != TDS_ROW_RESULT && result_type != TDS_COMPUTE_RESULT)
     948             :                         break;
     949             : 
     950         270 :                 row_of_query++;
     951             : 
     952             :                 /* skip rows outside of the firstrow/lastrow range, if specified */
     953         270 :                 if (dbproc->hostfileinfo->firstrow > row_of_query ||
     954             :                                                       row_of_query > TDS_MAX(dbproc->hostfileinfo->lastrow, 0x7FFFFFFF))
     955           0 :                         continue;
     956             : 
     957             :                 /* Go through the hostfile columns, finding those that relate to database columns. */
     958        1496 :                 for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
     959        1496 :                         hostcol = dbproc->hostfileinfo->host_columns[i];
     960        1496 :                         if (hostcol->tab_colnum < 1 || hostcol->tab_colnum > resinfo->num_cols)
     961           0 :                                 continue;
     962             : 
     963        1496 :                         curcol = resinfo->columns[hostcol->tab_colnum - 1];
     964             : 
     965        1496 :                         if (curcol->column_cur_size < 0) {
     966             :                                 buflen = 0;
     967             :                         } else {
     968        1386 :                                 buflen = _bcp_convert_out(dbproc, curcol, hostcol, &data, bcpdatefmt);
     969             :                         }
     970        1386 :                         if (buflen < 0) {
     971           0 :                                 _dblib_convert_err(dbproc, buflen);
     972           0 :                                 goto Cleanup;
     973             :                         }
     974             : 
     975             :                         /* The prefix */
     976        1496 :                         if (bcp_write_prefix(hostfile, hostcol, curcol, buflen) != SUCCEED)
     977             :                                 goto write_error;
     978             : 
     979             :                         /* The data */
     980        1496 :                         if (hostcol->column_len != -1) {
     981           0 :                                 buflen = TDS_MIN(buflen, hostcol->column_len);
     982             :                         }
     983             : 
     984        1496 :                         if (buflen > 0) {
     985        1386 :                                 if (fwrite(data, buflen, 1, hostfile) != 1)
     986             :                                         goto write_error;
     987             :                         }
     988             : 
     989             :                         /* The terminator */
     990        1496 :                         if (hostcol->terminator && hostcol->term_len > 0) {
     991        1476 :                                 if (fwrite(hostcol->terminator, hostcol->term_len, 1, hostfile) != 1)
     992             :                                         goto write_error;
     993             :                         }
     994             :                 }
     995         270 :                 rows_written++;
     996             :         }
     997         134 :         if (fclose(hostfile) != 0) {
     998           0 :                 dbperror(dbproc, SYBEBCUC, errno);
     999           0 :                 goto Cleanup;
    1000             :         }
    1001         134 :         hostfile = NULL;
    1002             : 
    1003         134 :         if (row_of_query + 1 < dbproc->hostfileinfo->firstrow) {
    1004             :                 /*
    1005             :                  * The table which bulk-copy is attempting to
    1006             :                  * copy to a host-file is shorter than the
    1007             :                  * number of rows which bulk-copy was instructed to skip.
    1008             :                  */
    1009             :                 /* TODO reset TDSSOCKET state */
    1010           0 :                 dbperror(dbproc, SYBETTS, 0);
    1011             :                 goto Cleanup;
    1012             :         }
    1013             : 
    1014         134 :         *rows_copied = rows_written;
    1015         134 :         free(data);
    1016         134 :         return SUCCEED;
    1017             : 
    1018           0 : write_error:
    1019           0 :         dbperror(dbproc, SYBEBCWE, errno);
    1020             : 
    1021           0 : Cleanup:
    1022           0 :         if (hostfile)
    1023           0 :                 fclose(hostfile);
    1024           0 :         free(data);
    1025           0 :         return FAIL;
    1026             : }
    1027             : 
    1028             : static STATUS
    1029         134 : _bcp_check_eof(DBPROCESS * dbproc, FILE *file, int icol)
    1030             : {
    1031         134 :         int errnum = errno;
    1032             : 
    1033         134 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_check_eof(%p, %p, %d)\n", dbproc, file, icol);
    1034         134 :         assert(dbproc);
    1035         134 :         assert(file);
    1036             : 
    1037         134 :         if (feof(file)) {
    1038         134 :                 if (icol == 0) {
    1039         134 :                         tdsdump_log(TDS_DBG_FUNC, "Normal end-of-file reached while loading bcp data file.\n");
    1040             :                         return NO_MORE_ROWS;
    1041             :                 }
    1042           0 :                 dbperror(dbproc, SYBEBEOF, errnum);
    1043           0 :                 return FAIL;
    1044             :         } 
    1045           0 :         dbperror(dbproc, SYBEBCRE, errnum);
    1046           0 :         return FAIL;
    1047             : }
    1048             : 
    1049             : /**
    1050             :  * Convert column for input to a table
    1051             :  */
    1052             : static TDSRET
    1053        4196 : _bcp_convert_in(DBPROCESS *dbproc, TDS_SERVER_TYPE srctype, const TDS_CHAR *src, TDS_UINT srclen,
    1054             :                 TDS_SERVER_TYPE desttype, BCPCOLDATA *coldata)
    1055             : {
    1056        4196 :         bool variable = true;
    1057             :         CONV_RESULT cr, *p_cr;
    1058             :         TDS_INT len;
    1059             : 
    1060        4196 :         coldata->is_null = false;
    1061             : 
    1062        4196 :         if (!is_variable_type(desttype)) {
    1063        3016 :                 variable = false;
    1064        3016 :                 p_cr = (CONV_RESULT *) coldata->data;
    1065             :         } else {
    1066             :                 p_cr = &cr;
    1067             :         }
    1068             : 
    1069        4196 :         len = tds_convert(dbproc->tds_socket->conn->tds_ctx, srctype, src, srclen, desttype, p_cr);
    1070        4196 :         if (len < 0) {
    1071           0 :                 _dblib_convert_err(dbproc, len);
    1072           0 :                 return TDS_FAIL;
    1073             :         }
    1074             : 
    1075        4196 :         coldata->datalen = len;
    1076        4196 :         if (variable) {
    1077        1180 :                 free(coldata->data);
    1078        1180 :                 coldata->data = (TDS_UCHAR *) cr.c;
    1079             :         }
    1080             :         return TDS_SUCCESS;
    1081             : }
    1082             : 
    1083             : static void
    1084        4196 : rtrim_bcpcol(TDSCOLUMN *bcpcol)
    1085             : {
    1086             :         /* trim trailing blanks from character data */
    1087        4196 :         if (is_ascii_type(bcpcol->on_server.column_type)) {
    1088             :                 /* A single NUL byte indicates an empty string. */
    1089        1046 :                 if (bcpcol->bcp_column_data->datalen == 1
    1090         200 :                     && bcpcol->bcp_column_data->data[0] == '\0') {
    1091          24 :                         bcpcol->bcp_column_data->datalen = 0;
    1092          24 :                         return;
    1093             :                 }
    1094        2044 :                 bcpcol->bcp_column_data->datalen = rtrim((char *) bcpcol->bcp_column_data->data,
    1095             :                                                                   bcpcol->bcp_column_data->datalen);
    1096        1022 :                 return;
    1097             :         }
    1098             : 
    1099             :         /* unicode part */
    1100        3150 :         if (is_unicode_type(bcpcol->on_server.column_type)) {
    1101             :                 uint16_t *data, space;
    1102             : 
    1103          50 :                 if (!bcpcol->char_conv || bcpcol->char_conv->to.charset.min_bytes_per_char != 2)
    1104           0 :                         return;
    1105             : 
    1106          50 :                 data = (uint16_t *) bcpcol->bcp_column_data->data;
    1107             :                 /* A single NUL byte indicates an empty string. */
    1108          50 :                 if (bcpcol->bcp_column_data->datalen == 2 && data[0] == 0) {
    1109           0 :                         bcpcol->bcp_column_data->datalen = 0;
    1110           0 :                         return;
    1111             :                 }
    1112          50 :                 switch (bcpcol->char_conv->to.charset.canonic) {
    1113           0 :                 case TDS_CHARSET_UTF_16BE:
    1114             :                 case TDS_CHARSET_UCS_2BE:
    1115           0 :                         TDS_PUT_A2BE(&space, 0x20);
    1116           0 :                         break;
    1117          50 :                 case TDS_CHARSET_UTF_16LE:
    1118             :                 case TDS_CHARSET_UCS_2LE:
    1119          50 :                         TDS_PUT_A2LE(&space, 0x20);
    1120          50 :                         break;
    1121             :                 default:
    1122             :                         return;
    1123             :                 }
    1124         100 :                 bcpcol->bcp_column_data->datalen = rtrim_u16(data, bcpcol->bcp_column_data->datalen, space);
    1125             :         }
    1126             : }
    1127             : 
    1128             : /** 
    1129             :  * \ingroup dblib_bcp_internal
    1130             :  * \brief 
    1131             :  *
    1132             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1133             :  * \param hostfile 
    1134             :  * \param row_error 
    1135             :  * 
    1136             :  * \return MORE_ROWS, NO_MORE_ROWS, or FAIL.
    1137             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1138             :  */
    1139             : static STATUS
    1140         404 : _bcp_read_hostfile(DBPROCESS * dbproc, FILE * hostfile, bool *row_error, bool skip)
    1141             : {
    1142             :         int i;
    1143             : 
    1144         404 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_read_hostfile(%p, %p, %p, %d)\n", dbproc, hostfile, row_error, skip);
    1145         404 :         assert(dbproc);
    1146         404 :         assert(hostfile);
    1147         404 :         assert(row_error);
    1148             : 
    1149             :         /* for each host file column defined by calls to bcp_colfmt */
    1150             : 
    1151        3396 :         for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    1152        1630 :                 TDSCOLUMN *bcpcol = NULL;
    1153             :                 BCP_HOSTCOLINFO *hostcol;
    1154             :                 TDS_CHAR *coldata;
    1155        1630 :                 int collen = 0;
    1156        1630 :                 bool data_is_null = false;
    1157             :                 offset_type col_start;
    1158             : 
    1159        1630 :                 tdsdump_log(TDS_DBG_FUNC, "parsing host column %d\n", i + 1);
    1160        1630 :                 hostcol = dbproc->hostfileinfo->host_columns[i];
    1161             : 
    1162        1630 :                 hostcol->column_error = 0;
    1163             : 
    1164             :                 /* 
    1165             :                  * If this host file column contains table data,
    1166             :                  * find the right element in the table/column list.  
    1167             :                  */
    1168        1630 :                 if (hostcol->tab_colnum > 0) {
    1169        1630 :                         if (hostcol->tab_colnum > dbproc->bcpinfo->bindinfo->num_cols) {
    1170           0 :                                 tdsdump_log(TDS_DBG_FUNC, "error: file wider than table: %d/%d\n", 
    1171             :                                                           i+1, dbproc->bcpinfo->bindinfo->num_cols);
    1172           0 :                                 dbperror(dbproc, SYBEBEOF, 0);
    1173           0 :                                 return FAIL;
    1174             :                         }
    1175        1630 :                         tdsdump_log(TDS_DBG_FUNC, "host column %d uses bcpcol %d (%p)\n", 
    1176             :                                                   i+1, hostcol->tab_colnum, bcpcol);
    1177        1630 :                         bcpcol = dbproc->bcpinfo->bindinfo->columns[hostcol->tab_colnum - 1];
    1178        1630 :                         assert(bcpcol != NULL);
    1179             :                 }
    1180             : 
    1181             :                 /* detect prefix len */
    1182        1630 :                 if (bcpcol && hostcol->prefix_len == -1)
    1183           0 :                         bcp_cache_prefix_len(hostcol, bcpcol);
    1184             : 
    1185             :                 /* a prefix length, if extant, specifies how many bytes to read */
    1186        1630 :                 if (hostcol->prefix_len > 0) {
    1187             :                         union {
    1188             :                                 TDS_TINYINT ti;
    1189             :                                 TDS_SMALLINT si;
    1190             :                                 TDS_INT li;
    1191             :                         } u;
    1192             : 
    1193          30 :                         switch (hostcol->prefix_len) {
    1194          20 :                         case 1:
    1195          20 :                                 if (fread(&u.ti, 1, 1, hostfile) != 1)
    1196          20 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1197          10 :                                 collen = u.ti ? u.ti : -1;
    1198          10 :                                 break;
    1199           0 :                         case 2:
    1200           0 :                                 if (fread(&u.si, 2, 1, hostfile) != 1)
    1201           0 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1202           0 :                                 collen = u.si;
    1203           0 :                                 break;
    1204          10 :                         case 4:
    1205          10 :                                 if (fread(&u.li, 4, 1, hostfile) != 1)
    1206           0 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1207          10 :                                 collen = u.li;
    1208          10 :                                 break;
    1209           0 :                         default:
    1210             :                                 /* FIXME return error, remember that prefix_len can be 3 */
    1211           0 :                                 assert(hostcol->prefix_len <= 4);
    1212             :                                 break;
    1213             :                         }
    1214             : 
    1215             :                         /* TODO test all NULL types */
    1216             :                         /* TODO for < -1 error */
    1217          20 :                         if (collen <= -1) {
    1218             :                                 data_is_null = true;
    1219             :                                 collen = 0;
    1220             :                         }
    1221             :                 }
    1222             : 
    1223             :                 /* if (Max) column length specified take that into consideration. (Meaning what, exactly?) */
    1224             : 
    1225        1620 :                 if (!data_is_null && hostcol->column_len >= 0) {
    1226           0 :                         if (hostcol->column_len == 0)
    1227             :                                 data_is_null = true;
    1228           0 :                         else if (collen)
    1229           0 :                                 collen = TDS_MIN(hostcol->column_len, collen);
    1230             :                         else
    1231             :                                 collen = hostcol->column_len;
    1232             :                 }
    1233             : 
    1234        1620 :                 tdsdump_log(TDS_DBG_FUNC, "prefix_len = %d collen = %d \n", hostcol->prefix_len, collen);
    1235             : 
    1236             :                 /* Fixed Length data - this overrides anything else specified */
    1237             : 
    1238        1620 :                 if (is_fixed_type(hostcol->datatype))
    1239          10 :                         collen = tds_get_size_by_type(hostcol->datatype);
    1240             : 
    1241        1620 :                 col_start = ftello(hostfile);
    1242             : 
    1243             :                 /*
    1244             :                  * The data file either contains prefixes stating the length, or is delimited.  
    1245             :                  * If delimited, we "measure" the field by looking for the terminator, then read it, 
    1246             :                  * and set collen to the field's post-iconv size.  
    1247             :                  */
    1248        1620 :                 if (hostcol->term_len > 0) { /* delimited data file */
    1249             :                         size_t col_bytes;
    1250             :                         TDSRET conv_res;
    1251             : 
    1252             :                         /* 
    1253             :                          * Read and convert the data
    1254             :                          */
    1255        1600 :                         coldata = NULL;
    1256        3200 :                         conv_res = tds_bcp_fread(dbproc->tds_socket, bcpcol ? bcpcol->char_conv : NULL, hostfile,
    1257        1600 :                                                  (const char *) hostcol->terminator, hostcol->term_len, &coldata, &col_bytes);
    1258             : 
    1259        1600 :                         if (TDS_FAILED(conv_res)) {
    1260           0 :                                 tdsdump_log(TDS_DBG_FUNC, "col %d: error converting %ld bytes!\n",
    1261             :                                                         (i+1), (long) collen);
    1262           0 :                                 *row_error = true;
    1263           0 :                                 free(coldata);
    1264           0 :                                 dbperror(dbproc, SYBEBCOR, 0);
    1265           0 :                                 return FAIL;
    1266             :                         }
    1267             : 
    1268        1600 :                         if (conv_res == TDS_NO_MORE_RESULTS) {
    1269         124 :                                 free(coldata);
    1270         124 :                                 return _bcp_check_eof(dbproc, hostfile, i);
    1271             :                         }
    1272             : 
    1273        1476 :                         if (col_bytes > 0x7fffffffl) {
    1274           0 :                                 free(coldata);
    1275           0 :                                 *row_error = true;
    1276           0 :                                 tdsdump_log(TDS_DBG_FUNC, "data from file is too large!\n");
    1277           0 :                                 dbperror(dbproc, SYBEBCOR, 0);
    1278           0 :                                 return FAIL;
    1279             :                         }
    1280             : 
    1281        1476 :                         collen = (int)col_bytes;
    1282        1476 :                         if (collen == 0)
    1283         110 :                                 data_is_null = true;
    1284             : 
    1285             :                         /*
    1286             :                          * TODO:  
    1287             :                          *    Dates are a problem.  In theory, we should be able to read non-English dates, which
    1288             :                          *    would contain non-ASCII characters.  One might suppose we should convert date
    1289             :                          *    strings to ISO-8859-1 (or another canonical form) here, because tds_convert() can't be
    1290             :                          *    expected to deal with encodings. But instead date strings are read verbatim and 
    1291             :                          *    passed to tds_convert() without even waving to iconv().  For English dates, this works, 
    1292             :                          *    because English dates expressed as UTF-8 strings are indistinguishable from the ASCII.  
    1293             :                          */
    1294             :                 } else {        /* unterminated field */
    1295             : 
    1296          20 :                         coldata = tds_new(TDS_CHAR, 1 + collen);
    1297          20 :                         if (coldata == NULL) {
    1298           0 :                                 *row_error = true;
    1299           0 :                                 dbperror(dbproc, SYBEMEM, errno);
    1300           0 :                                 return FAIL;
    1301             :                         }
    1302             : 
    1303          20 :                         coldata[collen] = 0;
    1304          20 :                         if (collen) {
    1305             :                                 /* 
    1306             :                                  * Read and convert the data
    1307             :                                  * TODO: Call tds_bcp_fread() instead of fread(3).
    1308             :                                  *       The columns should each have their iconv cd set, and noncharacter data
    1309             :                                  *       should have -1 as the iconv cd, causing tds_bcp_fread() to not attempt
    1310             :                                  *       any conversion.  We do not need a datatype switch here to decide what to do.  
    1311             :                                  *       As of 0.62, this *should* actually work.  All that remains is to change the
    1312             :                                  *       call and test it. 
    1313             :                                  */
    1314          20 :                                 tdsdump_log(TDS_DBG_FUNC, "Reading %d bytes from hostfile.\n", collen);
    1315          20 :                                 if (fread(coldata, collen, 1, hostfile) != 1) {
    1316           0 :                                         free(coldata);
    1317           0 :                                         return _bcp_check_eof(dbproc, hostfile, i);
    1318             :                                 }
    1319             :                         }
    1320             :                 }
    1321             : 
    1322             :                 /* 
    1323             :                  * At this point, however the field was read, however big it was, its address is coldata and its size is collen.
    1324             :                  */
    1325        1496 :                 tdsdump_log(TDS_DBG_FUNC, "Data read from hostfile: collen is now %d, data_is_null is %d\n", collen, data_is_null);
    1326        1496 :                 if (!skip && bcpcol) {
    1327        1496 :                         if (data_is_null) {
    1328         110 :                                 bcpcol->bcp_column_data->is_null = true;
    1329         110 :                                 bcpcol->bcp_column_data->datalen = 0;
    1330             :                         } else {
    1331             :                                 TDSRET rc;
    1332             :                                 TDS_SERVER_TYPE desttype;
    1333             : 
    1334        1386 :                                 desttype = tds_get_conversion_type(bcpcol->column_type, bcpcol->column_size);
    1335             : 
    1336        1386 :                                 rc = _bcp_convert_in(dbproc, hostcol->datatype, (const TDS_CHAR*) coldata, collen,
    1337             :                                                      desttype, bcpcol->bcp_column_data);
    1338        1386 :                                 if (TDS_FAILED(rc)) {
    1339           0 :                                         hostcol->column_error = HOST_COL_CONV_ERROR;
    1340           0 :                                         *row_error = true;
    1341           0 :                                         tdsdump_log(TDS_DBG_FUNC, 
    1342             :                                                 "_bcp_read_hostfile failed to convert %d bytes at offset 0x%" PRIx64 " in the data file.\n",
    1343             :                                                     collen, (TDS_INT8) col_start);
    1344             :                                 }
    1345             : 
    1346        1386 :                                 rtrim_bcpcol(bcpcol);
    1347             :                         }
    1348             : #if USING_SYBEBCNN
    1349             :                         if (!hostcol->column_error) {
    1350             :                                 if (bcpcol->bcp_column_data->datalen <= 0) {   /* Are we trying to insert a NULL ? */
    1351             :                                         if (!bcpcol->column_nullable) {
    1352             :                                                 /* too bad if the column is not nullable */
    1353             :                                                 hostcol->column_error = HOST_COL_NULL_ERROR;
    1354             :                                                 *row_error = true;
    1355             :                                                 dbperror(dbproc, SYBEBCNN, 0);
    1356             :                                         }
    1357             :                                 }
    1358             :                         }
    1359             : #endif
    1360             :                 }
    1361        1496 :                 free(coldata);
    1362             :         }
    1363             :         return MORE_ROWS;
    1364             : }
    1365             : 
    1366             : /** 
    1367             :  * \ingroup dblib_bcp
    1368             :  * \brief Write data in host variables to the table.  
    1369             :  * 
    1370             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1371             :  * 
    1372             :  * \remarks Call bcp_bind() first to describe the variables to be used.  
    1373             :  *      Use bcp_batch() to commit sets of rows. 
    1374             :  *      After sending the last row call bcp_done().
    1375             :  * \return SUCCEED or FAIL.
    1376             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), 
    1377             :  *      bcp_control(), bcp_done(), bcp_exec(), bcp_init(), bcp_moretext(), bcp_options()
    1378             :  */
    1379             : RETCODE
    1380         180 : bcp_sendrow(DBPROCESS * dbproc)
    1381             : {
    1382             :         TDSSOCKET *tds;
    1383             : 
    1384         180 :         tdsdump_log(TDS_DBG_FUNC, "bcp_sendrow(%p)\n", dbproc);
    1385         180 :         CHECK_CONN(FAIL);
    1386         180 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1387             : 
    1388         180 :         tds = dbproc->tds_socket;
    1389             : 
    1390         180 :         if (dbproc->bcpinfo->direction != DB_IN) {
    1391           0 :                 dbperror(dbproc, SYBEBCPN, 0);
    1392           0 :                 return FAIL;
    1393             :         }
    1394             : 
    1395         180 :         if (dbproc->hostfileinfo != NULL) {
    1396           0 :                 dbperror(dbproc, SYBEBCPB, 0);
    1397           0 :                 return FAIL;
    1398             :         }
    1399             : 
    1400             :         /* 
    1401             :          * The first time sendrow is called after bcp_init,
    1402             :          * there is a certain amount of initialisation to be done.
    1403             :          */
    1404         180 :         if (!dbproc->bcpinfo->xfer_init) {
    1405             : 
    1406             :                 /* The start_copy function retrieves details of the table's columns */
    1407          28 :                 if (TDS_FAILED(tds_bcp_start_copy_in(tds, dbproc->bcpinfo))) {
    1408           0 :                         dbperror(dbproc, SYBEBULKINSERT, 0);
    1409           0 :                         return FAIL;
    1410             :                 }
    1411             : 
    1412          28 :                 dbproc->bcpinfo->xfer_init = true;
    1413             : 
    1414             :         }
    1415             : 
    1416         180 :         dbproc->bcpinfo->parent = dbproc;
    1417         180 :         return TDS_FAILED(tds_bcp_send_record(dbproc->tds_socket, dbproc->bcpinfo,
    1418         180 :                           _bcp_get_col_data, _bcp_null_error, 0)) ? FAIL : SUCCEED;
    1419             : }
    1420             : 
    1421             : 
    1422             : /** 
    1423             :  * \ingroup dblib_bcp_internal
    1424             :  * \brief 
    1425             :  *
    1426             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1427             :  * \param rows_copied 
    1428             :  * 
    1429             :  * \return SUCCEED or FAIL.
    1430             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1431             :  */
    1432             : static RETCODE
    1433         134 : _bcp_exec_in(DBPROCESS * dbproc, DBINT * rows_copied)
    1434             : {
    1435         134 :         FILE *hostfile, *errfile = NULL;
    1436         134 :         TDSSOCKET *tds = dbproc->tds_socket;
    1437             :         BCP_HOSTCOLINFO *hostcol;
    1438             :         STATUS ret;
    1439             : 
    1440             :         int i, row_of_hostfile, rows_written_so_far;
    1441             :         int row_error_count;
    1442             :         bool row_error;
    1443             :         offset_type row_start, row_end;
    1444             :         offset_type error_row_size;
    1445         134 :         const size_t chunk_size = 0x20000u;
    1446             :         
    1447         134 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_exec_in(%p, %p)\n", dbproc, rows_copied);
    1448             :         assert(dbproc);
    1449         134 :         assert(rows_copied);
    1450             : 
    1451         134 :         *rows_copied = 0;
    1452             :         
    1453         134 :         if (!(hostfile = fopen(dbproc->hostfileinfo->hostfile, "r"))) {
    1454           0 :                 dbperror(dbproc, SYBEBCUO, 0);
    1455           0 :                 return FAIL;
    1456             :         }
    1457             : 
    1458         134 :         if (TDS_FAILED(tds_bcp_start_copy_in(tds, dbproc->bcpinfo))) {
    1459           0 :                 fclose(hostfile);
    1460           0 :                 return FAIL;
    1461             :         }
    1462             : 
    1463         134 :         row_of_hostfile = 0;
    1464         134 :         rows_written_so_far = 0;
    1465             : 
    1466         134 :         row_error_count = 0;
    1467         134 :         dbproc->bcpinfo->parent = dbproc;
    1468             : 
    1469             :         for (;;) {
    1470             :                 bool skip;
    1471             : 
    1472         404 :                 row_start = ftello(hostfile);
    1473         404 :                 row_error = false;
    1474             : 
    1475         404 :                 row_of_hostfile++;
    1476             : 
    1477             :                 if (row_of_hostfile > TDS_MAX(dbproc->hostfileinfo->lastrow, 0x7FFFFFFF))
    1478             :                         break;
    1479             : 
    1480         404 :                 skip = dbproc->hostfileinfo->firstrow > row_of_hostfile;
    1481         404 :                 ret = _bcp_read_hostfile(dbproc, hostfile, &row_error, skip);
    1482         404 :                 if (ret != MORE_ROWS)
    1483             :                         break;
    1484             : 
    1485         270 :                 if (row_error) {
    1486             :                         int count;
    1487             : 
    1488           0 :                         if (errfile == NULL && dbproc->hostfileinfo->errorfile) {
    1489           0 :                                 if (!(errfile = fopen(dbproc->hostfileinfo->errorfile, "w"))) {
    1490           0 :                                         fclose(hostfile);
    1491           0 :                                         dbperror(dbproc, SYBEBUOE, 0);
    1492           0 :                                         return FAIL;
    1493             :                                 }
    1494             :                         }
    1495             : 
    1496           0 :                         if (errfile != NULL) {
    1497             :                                 char *row_in_error = NULL;
    1498             : 
    1499           0 :                                 for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    1500           0 :                                         hostcol = dbproc->hostfileinfo->host_columns[i];
    1501           0 :                                         if (hostcol->column_error == HOST_COL_CONV_ERROR) {
    1502           0 :                                                 count = fprintf(errfile, 
    1503             :                                                         "#@ data conversion error on host data file Row %d Column %d\n",
    1504             :                                                         row_of_hostfile, i + 1);
    1505           0 :                                                 if( count < 0 ) {
    1506           0 :                                                         dbperror(dbproc, SYBEBWEF, errno);
    1507             :                                                 }
    1508           0 :                                         } else if (hostcol->column_error == HOST_COL_NULL_ERROR) {
    1509           0 :                                                 count = fprintf(errfile, "#@ Attempt to bulk-copy a NULL value into Server column"
    1510             :                                                                 " which does not accept NULL values. Row %d, Column %d\n",
    1511             :                                                                 row_of_hostfile, i + 1);
    1512           0 :                                                 if( count < 0 ) {
    1513           0 :                                                         dbperror(dbproc, SYBEBWEF, errno);
    1514             :                                                 }
    1515             : 
    1516             :                                         }
    1517             :                                 }
    1518             : 
    1519           0 :                                 row_end = ftello(hostfile);
    1520             : 
    1521             :                                 /* error data can be very long so split in chunks */
    1522           0 :                                 error_row_size = row_end - row_start;
    1523           0 :                                 fseeko(hostfile, row_start, SEEK_SET);
    1524             : 
    1525           0 :                                 while (error_row_size > 0) {
    1526           0 :                                         size_t chunk = TDS_MIN((size_t) error_row_size, chunk_size);
    1527             : 
    1528           0 :                                         if (!row_in_error) {
    1529           0 :                                                 if ((row_in_error = tds_new(char, chunk)) == NULL) {
    1530           0 :                                                         dbperror(dbproc, SYBEMEM, errno);
    1531             :                                                 }
    1532             :                                         }
    1533             : 
    1534           0 :                                         if (fread(row_in_error, chunk, 1, hostfile) != 1) {
    1535           0 :                                                 tdsdump_log(TDS_DBG_ERROR, "BILL fread failed after fseek\n");
    1536             :                                         }
    1537           0 :                                         count = (int)fwrite(row_in_error, chunk, 1, errfile);
    1538           0 :                                         if( (size_t)count < chunk ) {
    1539           0 :                                                 dbperror(dbproc, SYBEBWEF, errno);
    1540             :                                         }
    1541           0 :                                         error_row_size -= chunk;
    1542             :                                 }
    1543           0 :                                 free(row_in_error);
    1544             : 
    1545           0 :                                 fseeko(hostfile, row_end, SEEK_SET);
    1546           0 :                                 count = fprintf(errfile, "\n");
    1547           0 :                                 if( count < 0 ) {
    1548           0 :                                         dbperror(dbproc, SYBEBWEF, errno);
    1549             :                                 }
    1550             :                         }
    1551           0 :                         row_error_count++;
    1552           0 :                         if (row_error_count >= dbproc->hostfileinfo->maxerrs)
    1553             :                                 break;
    1554           0 :                         continue;
    1555             :                 }
    1556             : 
    1557         270 :                 if (skip)
    1558           0 :                         continue;
    1559             : 
    1560         270 :                 if (TDS_SUCCEED(tds_bcp_send_record(dbproc->tds_socket, dbproc->bcpinfo,
    1561             :                                                     _bcp_no_get_col_data, _bcp_null_error, 0))) {
    1562             : 
    1563         270 :                         rows_written_so_far++;
    1564             : 
    1565         270 :                         if (dbproc->hostfileinfo->batch > 0 && rows_written_so_far == dbproc->hostfileinfo->batch) {
    1566           0 :                                 if (TDS_FAILED(tds_bcp_done(tds, &rows_written_so_far))) {
    1567           0 :                                         if (errfile)
    1568           0 :                                                 fclose(errfile);
    1569           0 :                                         fclose(hostfile);
    1570           0 :                                         return FAIL;
    1571             :                                 }
    1572             : 
    1573           0 :                                 *rows_copied += rows_written_so_far;
    1574           0 :                                 rows_written_so_far = 0;
    1575             : 
    1576           0 :                                 dbperror(dbproc, SYBEBBCI, 0); /* batch copied to server */
    1577             : 
    1578           0 :                                 tds_bcp_start(tds, dbproc->bcpinfo);
    1579             :                         }
    1580             :                 }
    1581             :         }
    1582             :         
    1583         134 :         if (row_error_count == 0 && row_of_hostfile < dbproc->hostfileinfo->firstrow) {
    1584             :                 /* "The BCP hostfile '%1!' contains only %2! rows.  */
    1585           0 :                 dbperror(dbproc, SYBEBCSA, 0, dbproc->hostfileinfo->hostfile, row_of_hostfile); 
    1586             :         }
    1587             : 
    1588         134 :         if (errfile &&  0 != fclose(errfile) ) {
    1589           0 :                 dbperror(dbproc, SYBEBUCE, 0);
    1590             :         }
    1591             : 
    1592         134 :         if (fclose(hostfile) != 0) {
    1593           0 :                 dbperror(dbproc, SYBEBCUC, 0);
    1594           0 :                 ret = FAIL;
    1595             :         }
    1596             : 
    1597         134 :         tds_bcp_done(tds, &rows_written_so_far);
    1598         134 :         *rows_copied += rows_written_so_far;
    1599             : 
    1600         134 :         return ret == NO_MORE_ROWS? SUCCEED : FAIL;     /* (ret is returned from _bcp_read_hostfile) */
    1601             : }
    1602             : 
    1603             : /** 
    1604             :  * \ingroup dblib_bcp
    1605             :  * \brief Write a datafile to a table. 
    1606             :  *
    1607             :  * 
    1608             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1609             :  * \param rows_copied bcp_exec will write the count of rows successfully written to this address. 
    1610             :  *      If \a rows_copied is NULL, it will be ignored by db-lib. 
    1611             :  *
    1612             :  * \return SUCCEED or FAIL.
    1613             :  * \sa  bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(),
    1614             :  *      bcp_control(), bcp_done(), bcp_init(), bcp_sendrow()
    1615             :  */
    1616             : RETCODE
    1617         268 : bcp_exec(DBPROCESS * dbproc, DBINT *rows_copied)
    1618             : {
    1619             :         DBINT dummy_copied;
    1620         268 :         RETCODE ret = FAIL;
    1621             : 
    1622         268 :         tdsdump_log(TDS_DBG_FUNC, "bcp_exec(%p, %p)\n", dbproc, rows_copied);
    1623         268 :         CHECK_CONN(FAIL);
    1624         268 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1625         268 :         CHECK_PARAMETER(dbproc->hostfileinfo, SYBEBCVH, FAIL);
    1626             : 
    1627         268 :         if (rows_copied == NULL) /* NULL means we should ignore it */
    1628           0 :                 rows_copied = &dummy_copied;
    1629             : 
    1630         268 :         if (dbproc->bcpinfo->direction == DB_OUT || dbproc->bcpinfo->direction == DB_QUERYOUT) {
    1631         134 :                 ret = _bcp_exec_out(dbproc, rows_copied);
    1632         134 :         } else if (dbproc->bcpinfo->direction == DB_IN) {
    1633         134 :                 ret = _bcp_exec_in(dbproc, rows_copied);
    1634             :         }
    1635         268 :         _bcp_free_storage(dbproc);
    1636             :         
    1637         268 :         return ret;
    1638             : }
    1639             : 
    1640             : /** 
    1641             :  * \ingroup dblib_bcp_internal
    1642             :  * \brief 
    1643             :  *
    1644             :  * \param buffer 
    1645             :  * \param size 
    1646             :  * \param f 
    1647             :  * 
    1648             :  * \return SUCCEED or FAIL.
    1649             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1650             :  */
    1651             : static char *
    1652           0 : _bcp_fgets(char *buffer, int size, FILE *f)
    1653             : {
    1654           0 :         char *p = fgets(buffer, size, f);
    1655           0 :         if (p == NULL) 
    1656             :                 return p;
    1657             : 
    1658             :         /* discard newline */
    1659           0 :         p = strchr(buffer, 0) - 1;
    1660           0 :         if (p >= buffer && *p == '\n')
    1661           0 :                 *p = 0;
    1662             :         return buffer;
    1663             : }
    1664             : 
    1665             : /** 
    1666             :  * \ingroup dblib_bcp
    1667             :  * \brief Read a format definition file.
    1668             :  *
    1669             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1670             :  * \param filename Name that will be passed to fopen(3).  
    1671             :  * 
    1672             :  * \remarks Reads a format file and calls bcp_columns() and bcp_colfmt() as needed. 
    1673             :  *
    1674             :  * \return SUCCEED or FAIL.
    1675             :  * \sa  bcp_colfmt(), bcp_colfmt_ps(), bcp_columns(), bcp_writefmt()
    1676             :  */
    1677             : RETCODE
    1678           0 : bcp_readfmt(DBPROCESS * dbproc, const char filename[])
    1679             : {
    1680             :         BCP_HOSTCOLINFO hostcol[1];
    1681             :         FILE *ffile;
    1682             :         char buffer[1024];
    1683           0 :         float lf_version = 0.0;
    1684           0 :         int li_numcols = 0;
    1685           0 :         int colinfo_count = 0;
    1686             : 
    1687           0 :         tdsdump_log(TDS_DBG_FUNC, "bcp_readfmt(%p, %s)\n", dbproc, filename? filename:"NULL");
    1688           0 :         CHECK_CONN(FAIL);
    1689           0 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1690           0 :         CHECK_NULP(filename, "bcp_readfmt", 2, FAIL);
    1691             : 
    1692           0 :         memset(hostcol, 0, sizeof(hostcol));
    1693             : 
    1694           0 :         if ((ffile = fopen(filename, "r")) == NULL) {
    1695           0 :                 dbperror(dbproc, SYBEBUOF, 0);
    1696           0 :                 goto Cleanup;
    1697             :         }
    1698             : 
    1699           0 :         if ((_bcp_fgets(buffer, sizeof(buffer), ffile)) != NULL) {
    1700           0 :                 lf_version = (float)atof(buffer);
    1701           0 :         } else if (ferror(ffile)) {
    1702           0 :                 dbperror(dbproc, SYBEBRFF, errno);
    1703           0 :                 goto Cleanup;
    1704             :         }
    1705             : 
    1706           0 :         if ((_bcp_fgets(buffer, sizeof(buffer), ffile)) != NULL) {
    1707           0 :                 li_numcols = atoi(buffer);
    1708           0 :         } else if (ferror(ffile)) {
    1709           0 :                 dbperror(dbproc, SYBEBRFF, errno);
    1710           0 :                 goto Cleanup;
    1711             :         }
    1712             : 
    1713           0 :         if (li_numcols <= 0)
    1714             :                 goto Cleanup;
    1715             : 
    1716           0 :         if (bcp_columns(dbproc, li_numcols) == FAIL)
    1717             :                 goto Cleanup;
    1718             : 
    1719             :         do {
    1720           0 :                 memset(hostcol, 0, sizeof(hostcol));
    1721             : 
    1722           0 :                 if (_bcp_fgets(buffer, sizeof(buffer), ffile) == NULL)
    1723             :                         goto Cleanup;
    1724             : 
    1725           0 :                 if (!_bcp_readfmt_colinfo(dbproc, buffer, hostcol))
    1726             :                         goto Cleanup;
    1727             : 
    1728           0 :                 if (bcp_colfmt(dbproc, hostcol->host_column, hostcol->datatype,
    1729             :                                hostcol->prefix_len, hostcol->column_len,
    1730           0 :                                hostcol->terminator, hostcol->term_len, hostcol->tab_colnum) == FAIL) {
    1731             :                         goto Cleanup;
    1732             :                 }
    1733             : 
    1734           0 :                 TDS_ZERO_FREE(hostcol->terminator);
    1735           0 :         } while (++colinfo_count < li_numcols);
    1736             : 
    1737           0 :         if (ferror(ffile)) {
    1738           0 :                 dbperror(dbproc, SYBEBRFF, errno);
    1739           0 :                 goto Cleanup;
    1740             :         }
    1741             :         
    1742           0 :         if (fclose(ffile) != 0) {
    1743           0 :                 dbperror(dbproc, SYBEBUCF, 0);
    1744             :                 /* even if failure is returned ffile is no more valid */
    1745           0 :                 ffile = NULL;
    1746           0 :                 goto Cleanup;
    1747             :         }
    1748           0 :         ffile = NULL;
    1749             : 
    1750           0 :         if (colinfo_count != li_numcols)
    1751             :                 goto Cleanup;
    1752             : 
    1753             :         return SUCCEED;
    1754             : 
    1755           0 : Cleanup:
    1756           0 :         TDS_ZERO_FREE(hostcol->terminator);
    1757           0 :         _bcp_free_columns(dbproc);
    1758           0 :         if (ffile)
    1759           0 :                 fclose(ffile);
    1760             :         return FAIL;
    1761             : }
    1762             : 
    1763             : /** 
    1764             :  * \ingroup dblib_bcp_internal
    1765             :  * \brief 
    1766             :  *
    1767             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1768             :  * \param buf 
    1769             :  * \param ci 
    1770             :  * 
    1771             :  * \return SUCCEED or FAIL.
    1772             :  * \sa  BCP_SETL(), bcp_batch(), bcp_bind(), bcp_colfmt(), bcp_colfmt_ps(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), bcp_done(), bcp_exec(), bcp_getl(), bcp_init(), bcp_moretext(), bcp_options(), bcp_readfmt(), bcp_sendrow()
    1773             :  */
    1774             : static int
    1775           0 : _bcp_readfmt_colinfo(DBPROCESS * dbproc, char *buf, BCP_HOSTCOLINFO * ci)
    1776             : {
    1777             :         char *tok;
    1778             :         int whichcol;
    1779             :         char term[30];
    1780             :         int i;
    1781             :         char *lasts;
    1782             : 
    1783             :         enum nextcol
    1784             :         {
    1785             :                 HOST_COLUMN,
    1786             :                 DATATYPE,
    1787             :                 PREFIX_LEN,
    1788             :                 COLUMN_LEN,
    1789             :                 TERMINATOR,
    1790             :                 TAB_COLNUM,
    1791             :                 NO_MORE_COLS
    1792             :         };
    1793             : 
    1794           0 :         assert(dbproc);
    1795           0 :         assert(buf);
    1796           0 :         assert(ci);
    1797           0 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_readfmt_colinfo(%p, %s, %p)\n", dbproc, buf, ci);
    1798             : 
    1799           0 :         tok = strtok_r(buf, " \t", &lasts);
    1800           0 :         whichcol = HOST_COLUMN;
    1801             : 
    1802             :         /* TODO use a better way to get an int atoi is very error prone */
    1803           0 :         while (tok != NULL && whichcol != NO_MORE_COLS) {
    1804           0 :                 switch (whichcol) {
    1805             : 
    1806           0 :                 case HOST_COLUMN:
    1807           0 :                         ci->host_column = atoi(tok);
    1808             : 
    1809           0 :                         if (ci->host_column < 1) {
    1810           0 :                                 dbperror(dbproc, SYBEBIHC, 0);
    1811           0 :                                 return (FALSE);
    1812             :                         }
    1813             : 
    1814             :                         whichcol = DATATYPE;
    1815             :                         break;
    1816             : 
    1817           0 :                 case DATATYPE:
    1818           0 :                         if (strcmp(tok, "SYBCHAR") == 0)
    1819           0 :                                 ci->datatype = SYBCHAR;
    1820           0 :                         else if (strcmp(tok, "SYBTEXT") == 0)
    1821           0 :                                 ci->datatype = SYBTEXT;
    1822           0 :                         else if (strcmp(tok, "SYBBINARY") == 0)
    1823           0 :                                 ci->datatype = SYBBINARY;
    1824           0 :                         else if (strcmp(tok, "SYBIMAGE") == 0)
    1825           0 :                                 ci->datatype = SYBIMAGE;
    1826           0 :                         else if (strcmp(tok, "SYBINT1") == 0)
    1827           0 :                                 ci->datatype = SYBINT1;
    1828           0 :                         else if (strcmp(tok, "SYBINT2") == 0)
    1829           0 :                                 ci->datatype = SYBINT2;
    1830           0 :                         else if (strcmp(tok, "SYBINT4") == 0)
    1831           0 :                                 ci->datatype = SYBINT4;
    1832           0 :                         else if (strcmp(tok, "SYBINT8") == 0)
    1833           0 :                                 ci->datatype = SYBINT8;
    1834           0 :                         else if (strcmp(tok, "SYBFLT8") == 0)
    1835           0 :                                 ci->datatype = SYBFLT8;
    1836           0 :                         else if (strcmp(tok, "SYBREAL") == 0)
    1837           0 :                                 ci->datatype = SYBREAL;
    1838           0 :                         else if (strcmp(tok, "SYBBIT") == 0)
    1839           0 :                                 ci->datatype = SYBBIT;
    1840           0 :                         else if (strcmp(tok, "SYBNUMERIC") == 0)
    1841           0 :                                 ci->datatype = SYBNUMERIC;
    1842           0 :                         else if (strcmp(tok, "SYBDECIMAL") == 0)
    1843           0 :                                 ci->datatype = SYBDECIMAL;
    1844           0 :                         else if (strcmp(tok, "SYBMONEY") == 0)
    1845           0 :                                 ci->datatype = SYBMONEY;
    1846           0 :                         else if (strcmp(tok, "SYBMONEY4") == 0)
    1847           0 :                                 ci->datatype = SYBMONEY4;
    1848           0 :                         else if (strcmp(tok, "SYBDATETIME") == 0)
    1849           0 :                                 ci->datatype = SYBDATETIME;
    1850           0 :                         else if (strcmp(tok, "SYBDATETIME4") == 0)
    1851           0 :                                 ci->datatype = SYBDATETIME4;
    1852             :                         /* TODO SQL* for MS
    1853             :                            SQLNCHAR SQLBIGINT SQLTINYINT SQLSMALLINT
    1854             :                            SQLUNIQUEID SQLVARIANT SQLUDT */
    1855             :                         else {
    1856           0 :                                 dbperror(dbproc, SYBEBUDF, 0);
    1857           0 :                                 return (FALSE);
    1858             :                         }
    1859             : 
    1860             :                         whichcol = PREFIX_LEN;
    1861             :                         break;
    1862             : 
    1863           0 :                 case PREFIX_LEN:
    1864           0 :                         ci->prefix_len = atoi(tok);
    1865           0 :                         whichcol = COLUMN_LEN;
    1866           0 :                         break;
    1867           0 :                 case COLUMN_LEN:
    1868           0 :                         ci->column_len = atoi(tok);
    1869           0 :                         whichcol = TERMINATOR;
    1870           0 :                         break;
    1871           0 :                 case TERMINATOR:
    1872             : 
    1873           0 :                         if (*tok++ != '\"')
    1874             :                                 return (FALSE);
    1875             : 
    1876           0 :                         for (i = 0; *tok != '\"' && i < sizeof(term); i++) {
    1877           0 :                                 if (*tok == '\\') {
    1878           0 :                                         tok++;
    1879           0 :                                         switch (*tok) {
    1880           0 :                                         case 't':
    1881           0 :                                                 term[i] = '\t';
    1882           0 :                                                 break;
    1883           0 :                                         case 'n':
    1884           0 :                                                 term[i] = '\n';
    1885           0 :                                                 break;
    1886           0 :                                         case 'r':
    1887           0 :                                                 term[i] = '\r';
    1888           0 :                                                 break;
    1889           0 :                                         case '\\':
    1890           0 :                                                 term[i] = '\\';
    1891           0 :                                                 break;
    1892           0 :                                         case '0':
    1893           0 :                                                 term[i] = '\0';
    1894           0 :                                                 break;
    1895             :                                         default:
    1896             :                                                 return (FALSE);
    1897             :                                         }
    1898           0 :                                         tok++;
    1899             :                                 } else
    1900           0 :                                         term[i] = *tok++;
    1901             :                         }
    1902             : 
    1903           0 :                         if (*tok != '\"')
    1904             :                                 return (FALSE);
    1905             : 
    1906           0 :                         ci->term_len = i;
    1907           0 :                         TDS_ZERO_FREE(ci->terminator);
    1908           0 :                         if (i > 0) {
    1909           0 :                                 if ((ci->terminator = tds_new(BYTE, i)) == NULL) {
    1910           0 :                                         dbperror(dbproc, SYBEMEM, errno);
    1911           0 :                                         return FALSE;
    1912             :                                 }
    1913           0 :                                 memcpy(ci->terminator, term, i);
    1914             :                         }
    1915             : 
    1916             :                         whichcol = TAB_COLNUM;
    1917             :                         break;
    1918             : 
    1919           0 :                 case TAB_COLNUM:
    1920           0 :                         ci->tab_colnum = atoi(tok);
    1921           0 :                         whichcol = NO_MORE_COLS;
    1922           0 :                         break;
    1923             : 
    1924             :                 }
    1925           0 :                 tok = strtok_r(NULL, " \t", &lasts);
    1926             :         }
    1927           0 :         if (whichcol == NO_MORE_COLS)
    1928             :                 return (TRUE);
    1929             :         else
    1930           0 :                 return (FALSE);
    1931             : }
    1932             : 
    1933             : #if defined(DBLIB_UNIMPLEMENTED)
    1934             : /** 
    1935             :  * \ingroup dblib_bcp
    1936             :  * \brief Write a format definition file. Not Implemented. 
    1937             :  *
    1938             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1939             :  * \param filename Name that would be passed to fopen(3).  
    1940             :  * 
    1941             :  * \remarks Reads a format file and calls bcp_columns() and bcp_colfmt() as needed. 
    1942             :  * \a FreeTDS includes freebcp, a utility to copy data to or from a host file. 
    1943             :  *
    1944             :  * \todo For completeness, \a freebcp ought to be able to create format files, but that functionality 
    1945             :  *      is currently lacking, as is bcp_writefmt().
    1946             :  * \todo See the vendors' documentation for the format of these files.
    1947             :  *
    1948             :  * \return SUCCEED or FAIL.
    1949             :  * \sa  bcp_colfmt(), bcp_colfmt_ps(), bcp_columns(), bcp_readfmt()
    1950             :  */
    1951             : RETCODE
    1952             : bcp_writefmt(DBPROCESS * dbproc, const char filename[])
    1953             : {
    1954             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_writefmt(%p, %s)\n", dbproc, filename? filename:"NULL");
    1955             :         CHECK_CONN(FAIL);
    1956             :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1957             :         CHECK_NULP(filename, "bcp_writefmt", 2, FAIL);
    1958             : 
    1959             : #if 0
    1960             :         dbperror(dbproc, SYBEBUFF, errno);      /* bcp: Unable to create format file */
    1961             :         dbperror(dbproc, SYBEBWFF, errno);      /* I/O error while writing bcp format file */
    1962             : #endif
    1963             : 
    1964             :         return FAIL;
    1965             : }
    1966             : 
    1967             : /** 
    1968             :  * \ingroup dblib_bcp
    1969             :  * \brief Write some text or image data to the server.  Not implemented, sadly.  
    1970             :  *
    1971             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    1972             :  * \param size How much to write, in bytes. 
    1973             :  * \param text Address of the data to be written. 
    1974             :  * \remarks For a SYBTEXT or SYBIMAGE column, bcp_bind() can be called with 
    1975             :  *      a NULL varaddr parameter.  If it is, bcp_sendrow() will return control
    1976             :  *      to the application after the non-text data have been sent.  The application then calls 
    1977             :  *      bcp_moretext() -- usually in a loop -- to send the text data in manageable chunks.  
    1978             :  * \todo implement bcp_moretext().
    1979             :  * \return SUCCEED or FAIL.
    1980             :  * \sa  bcp_bind(), bcp_sendrow(), dbmoretext(), dbwritetext()
    1981             :  */
    1982             : RETCODE
    1983             : bcp_moretext(DBPROCESS * dbproc, DBINT size, BYTE * text)
    1984             : {
    1985             :         tdsdump_log(TDS_DBG_FUNC, "UNIMPLEMENTED: bcp_moretext(%p, %d, %p)\n", dbproc, size, text);
    1986             :         CHECK_CONN(FAIL);
    1987             :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    1988             :         CHECK_NULP(text, "bcp_moretext", 3, FAIL);
    1989             :         
    1990             : #if 0
    1991             :         dbperror(dbproc, SYBEBCMTXT, 0); 
    1992             :                 /* bcp_moretext may be used only when there is at least one text or image column in the server table */
    1993             :         dbperror(dbproc, SYBEBTMT, 0); 
    1994             :                 /* Attempt to send too much text data via the bcp_moretext call */
    1995             : #endif
    1996             :         return FAIL;
    1997             : }
    1998             : #endif
    1999             : 
    2000             : /** 
    2001             :  * \ingroup dblib_bcp
    2002             :  * \brief Commit a set of rows to the table. 
    2003             :  * 
    2004             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2005             :  * \remarks If not called, bcp_done() will cause the rows to be saved.  
    2006             :  * \return Count of rows saved, or -1 on error.
    2007             :  * \sa  bcp_bind(), bcp_done(), bcp_sendrow()
    2008             :  */
    2009             : DBINT
    2010          10 : bcp_batch(DBPROCESS * dbproc)
    2011             : {
    2012          10 :         int rows_copied = 0;
    2013             : 
    2014          10 :         tdsdump_log(TDS_DBG_FUNC, "bcp_batch(%p)\n", dbproc);
    2015          10 :         CHECK_CONN(-1);
    2016          10 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, -1);
    2017             : 
    2018          10 :         if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
    2019             :                 return -1;
    2020             : 
    2021          10 :         tds_bcp_start(dbproc->tds_socket, dbproc->bcpinfo);
    2022             : 
    2023          10 :         return rows_copied;
    2024             : }
    2025             : 
    2026             : /** 
    2027             :  * \ingroup dblib_bcp
    2028             :  * \brief Conclude the transfer of data from program variables.
    2029             :  * 
    2030             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2031             :  * \remarks Do not overlook this function.  According to Sybase, failure to call bcp_done() 
    2032             :  * "will result in unpredictable errors".
    2033             :  * \return As with bcp_batch(), the count of rows saved, or -1 on error.
    2034             :  * \sa  bcp_batch(), bcp_bind(), bcp_moretext(), bcp_sendrow()
    2035             :  */
    2036             : DBINT
    2037          38 : bcp_done(DBPROCESS * dbproc)
    2038             : {
    2039             :         int rows_copied;
    2040             : 
    2041          38 :         tdsdump_log(TDS_DBG_FUNC, "bcp_done(%p)\n", dbproc);
    2042          38 :         CHECK_CONN(-1);
    2043             :         
    2044          38 :         if (!(dbproc->bcpinfo))
    2045             :                 return -1;
    2046             : 
    2047          38 :         if (TDS_FAILED(tds_bcp_done(dbproc->tds_socket, &rows_copied)))
    2048             :                 return -1;
    2049             : 
    2050          20 :         _bcp_free_storage(dbproc);
    2051             : 
    2052          20 :         return rows_copied;
    2053             : }
    2054             : 
    2055             : /** 
    2056             :  * \ingroup dblib_bcp
    2057             :  * \brief Bind a program host variable to a database column
    2058             :  * 
    2059             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2060             :  * \param varaddr address of host variable
    2061             :  * \param prefixlen length of any prefix found at the beginning of \a varaddr, in bytes.  
    2062             :         Use zero for fixed-length datatypes. 
    2063             :  * \param varlen bytes of data in \a varaddr.  Zero for NULL, -1 for fixed-length datatypes. 
    2064             :  * \param terminator byte sequence that marks the end of the data in \a varaddr
    2065             :  * \param termlen length of \a terminator
    2066             :  * \param vartype datatype of the host variable
    2067             :  * \param table_column Nth column, starting at 1, in the table.
    2068             :  * 
    2069             :  * \remarks The order of operation is:
    2070             :  *      - bcp_init() with \a hfile == NULL and \a direction == DB_IN.
    2071             :  *      - bcp_bind(), once per column you want to write to
    2072             :  *      - bcp_batch(), optionally, to commit a set of rows
    2073             :  *      - bcp_done() 
    2074             :  * \return SUCCEED or FAIL.
    2075             :  * \sa  bcp_batch(), bcp_colfmt(), bcp_collen(), bcp_colptr(), bcp_columns(), bcp_control(), 
    2076             :  *      bcp_done(), bcp_exec(), bcp_moretext(), bcp_sendrow() 
    2077             :  */
    2078             : RETCODE
    2079         324 : bcp_bind(DBPROCESS * dbproc, BYTE * varaddr, int prefixlen, DBINT varlen,
    2080             :          BYTE * terminator, int termlen, int db_vartype, int table_column)
    2081             : {
    2082             :         TDS_SERVER_TYPE vartype;
    2083             :         TDSCOLUMN *colinfo;
    2084             : 
    2085         324 :         tdsdump_log(TDS_DBG_FUNC, "bcp_bind(%p, %p, %d, %d -- %p, %d, %s, %d)\n", 
    2086             :                                                 dbproc, varaddr, prefixlen, varlen, 
    2087             :                                                 terminator, termlen, dbprtype(db_vartype), table_column);
    2088         324 :         CHECK_CONN(FAIL);
    2089         324 :         CHECK_PARAMETER(dbproc->bcpinfo, SYBEBCPI, FAIL);
    2090         648 :         DBPERROR_RETURN(db_vartype != 0 && !is_tds_type_valid(db_vartype), SYBEUDTY);
    2091         324 :         vartype = (TDS_SERVER_TYPE) db_vartype;
    2092             : 
    2093         324 :         if (dbproc->hostfileinfo != NULL) {
    2094           0 :                 dbperror(dbproc, SYBEBCPB, 0);
    2095           0 :                 return FAIL;
    2096             :         }
    2097             : 
    2098         324 :         if (dbproc->bcpinfo->direction != DB_IN) {
    2099           0 :                 dbperror(dbproc, SYBEBCPN, 0);
    2100           0 :                 return FAIL;
    2101             :         }
    2102             : 
    2103         324 :         if (varlen < -1) {
    2104           0 :                 dbperror(dbproc, SYBEBCVLEN, 0);
    2105           0 :                 return FAIL;
    2106             :         }
    2107             : 
    2108         324 :         if (prefixlen != 0 && prefixlen != 1 && prefixlen != 2 && prefixlen != 4) {
    2109           0 :                 dbperror(dbproc, SYBEBCBPREF, 0);
    2110           0 :                 return FAIL;
    2111             :         }
    2112             : 
    2113         324 :         if (prefixlen == 0 && varlen == -1 && termlen == -1 && !is_fixed_type(vartype)) {
    2114           0 :                 tdsdump_log(TDS_DBG_FUNC, "bcp_bind(): non-fixed type %d requires prefix or terminator\n", vartype);
    2115             :                 return FAIL;
    2116             :         }
    2117             : 
    2118         324 :         if (is_fixed_type(vartype) && (varlen != -1 && varlen != 0)) {
    2119           0 :                 dbperror(dbproc, SYBEBCIT, 0);
    2120           0 :                 return FAIL;
    2121             :         }
    2122             : 
    2123         324 :         if (table_column <= 0 ||  table_column > dbproc->bcpinfo->bindinfo->num_cols) {
    2124           0 :                 dbperror(dbproc, SYBECNOR, 0);
    2125           0 :                 return FAIL;
    2126             :         }
    2127             :         
    2128         324 :         if (varaddr == NULL && (prefixlen != 0 || termlen != 0)) {
    2129           0 :                 dbperror(dbproc, SYBEBCBNPR, 0);
    2130           0 :                 return FAIL;
    2131             :         }
    2132             : 
    2133         324 :         colinfo = dbproc->bcpinfo->bindinfo->columns[table_column - 1];
    2134             : 
    2135             :         /* If varaddr is NULL and varlen greater than 0, the table column type must be SYBTEXT or SYBIMAGE 
    2136             :                 and the program variable type must be SYBTEXT, SYBCHAR, SYBIMAGE or SYBBINARY */
    2137         324 :         if (varaddr == NULL && varlen > 0) {
    2138           0 :                 int fOK = (colinfo->column_type == SYBTEXT || colinfo->column_type == SYBIMAGE) &&
    2139           0 :                           (vartype == SYBTEXT || vartype == SYBCHAR || vartype == SYBIMAGE || vartype == SYBBINARY );
    2140             :                 if( !fOK ) {
    2141           0 :                         dbperror(dbproc, SYBEBCBNTYP, 0);
    2142           0 :                         tdsdump_log(TDS_DBG_FUNC, "bcp_bind: SYBEBCBNTYP: column=%d and vartype=%d (should fail?)\n", 
    2143           0 :                                                         colinfo->column_type, vartype);
    2144             :                         /* return FAIL; */
    2145             :                 }
    2146             :         }
    2147             : 
    2148         324 :         colinfo->column_varaddr  = (char *)varaddr;
    2149         324 :         colinfo->column_bindtype = vartype;
    2150         324 :         colinfo->column_bindlen  = varlen;
    2151         324 :         colinfo->bcp_prefix_len = prefixlen;
    2152             : 
    2153         324 :         TDS_ZERO_FREE(colinfo->bcp_terminator);
    2154         324 :         colinfo->bcp_term_len = 0;
    2155         324 :         if (termlen > 0) {
    2156          54 :                 if ((colinfo->bcp_terminator =  tds_new(TDS_CHAR, termlen)) == NULL) {
    2157           0 :                         dbperror(dbproc, SYBEMEM, errno);
    2158           0 :                         return FAIL;
    2159             :                 }
    2160          54 :                 memcpy(colinfo->bcp_terminator, terminator, termlen);
    2161          54 :                 colinfo->bcp_term_len = termlen;
    2162             :         }
    2163             : 
    2164             :         return SUCCEED;
    2165             : }
    2166             : 
    2167             : static void
    2168           2 : _bcp_null_error(TDSBCPINFO *bcpinfo, int index TDS_UNUSED, int offset TDS_UNUSED)
    2169             : {
    2170           2 :         DBPROCESS *dbproc = (DBPROCESS *) bcpinfo->parent;
    2171           2 :         dbperror(dbproc, SYBEBCNN, 0);
    2172           2 : }
    2173             : 
    2174             : /** 
    2175             :  * \ingroup dblib_bcp_internal
    2176             :  * \brief For a bcp in from program variables, get the data from the host variable
    2177             :  *
    2178             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2179             :  * \param bindcol 
    2180             :  * 
    2181             :  * \return TDS_SUCCESS or TDS_FAIL.
    2182             :  * \sa  _bcp_add_fixed_columns, _bcp_add_variable_columns, _bcp_send_bcp_record
    2183             :  */
    2184             : static TDSRET
    2185        4138 : _bcp_get_col_data(TDSBCPINFO *bcpinfo, TDSCOLUMN *bindcol, int offset TDS_UNUSED)
    2186             : {
    2187             :         TDS_SERVER_TYPE coltype, desttype;
    2188             :         int collen;
    2189             :         int bytes_read;
    2190             :         BYTE *dataptr;
    2191        4138 :         DBPROCESS *dbproc = (DBPROCESS *) bcpinfo->parent;
    2192             :         TDSRET rc;
    2193             : 
    2194        4138 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_get_col_data(%p, %p)\n", bcpinfo, bindcol);
    2195        4138 :         CHECK_CONN(TDS_FAIL);
    2196        4138 :         CHECK_NULP(bindcol, "_bcp_get_col_data", 2, TDS_FAIL);
    2197             : 
    2198        4138 :         dataptr = (BYTE *) bindcol->column_varaddr;
    2199             : 
    2200        4138 :         collen = 0;
    2201             : 
    2202             :         /* If a prefix length specified, read the correct  amount of data. */
    2203             : 
    2204        4138 :         if (bindcol->bcp_prefix_len > 0) {
    2205             : 
    2206           0 :                 switch (bindcol->bcp_prefix_len) {
    2207           0 :                 case 1:
    2208           0 :                         collen = TDS_GET_UA1(dataptr);
    2209           0 :                         dataptr += 1;
    2210           0 :                         break;
    2211           0 :                 case 2:
    2212           0 :                         collen = (TDS_SMALLINT) TDS_GET_UA2(dataptr);
    2213           0 :                         dataptr += 2;
    2214           0 :                         break;
    2215           0 :                 case 4:
    2216           0 :                         collen = (TDS_INT) TDS_GET_UA4(dataptr);
    2217           0 :                         dataptr += 4;
    2218           0 :                         break;
    2219             :                 }
    2220           0 :                 if (collen <= 0)
    2221             :                         goto null_data;
    2222             :         }
    2223             : 
    2224             :         /* if (Max) column length specified take that into consideration. */
    2225             : 
    2226        4138 :         if (bindcol->column_bindlen >= 0) {
    2227        2800 :                 if (bindcol->column_bindlen == 0)
    2228             :                         goto null_data;
    2229        1500 :                 if (collen)
    2230           0 :                         collen = TDS_MIN(bindcol->column_bindlen, collen);
    2231             :                 else
    2232             :                         collen = bindcol->column_bindlen;
    2233             :         }
    2234             : 
    2235        2838 :         desttype = tds_get_conversion_type(bindcol->column_type, bindcol->column_size);
    2236             : 
    2237        2838 :         coltype = bindcol->column_bindtype == 0 ? desttype : (TDS_SERVER_TYPE) bindcol->column_bindtype;
    2238             : 
    2239             :         /* Fixed Length data - this overrides anything else specified */
    2240        2838 :         if (is_fixed_type(coltype))
    2241         750 :                 collen = tds_get_size_by_type(coltype);
    2242             : 
    2243             :         /* read the data, finally */
    2244             : 
    2245        2838 :         if (bindcol->bcp_term_len > 0) {  /* terminated field */
    2246          88 :                 bytes_read = _bcp_get_term_var(dataptr, (BYTE *)bindcol->bcp_terminator, bindcol->bcp_term_len);
    2247             : 
    2248          88 :                 if (collen <= 0 || bytes_read < collen)
    2249          88 :                         collen = bytes_read;
    2250             : 
    2251          88 :                 if (collen == 0)
    2252             :                         goto null_data;
    2253             :         }
    2254             : 
    2255        2810 :         if (collen < 0)
    2256           0 :                 collen = (int) strlen((char *) dataptr);
    2257             : 
    2258        2810 :         rc = _bcp_convert_in(dbproc, coltype, (const TDS_CHAR*) dataptr, collen,
    2259             :                                             desttype, bindcol->bcp_column_data);
    2260        2810 :         if (TDS_FAILED(rc))
    2261             :                 return rc;
    2262        2810 :         rtrim_bcpcol(bindcol);
    2263             : 
    2264        2810 :         return TDS_SUCCESS;
    2265             : 
    2266        1328 : null_data:
    2267        1328 :         bindcol->bcp_column_data->datalen = 0;
    2268        1328 :         bindcol->bcp_column_data->is_null = true;
    2269        1328 :         return TDS_SUCCESS;
    2270             : }
    2271             : 
    2272             : /**
    2273             :  * Function to read data from file. I this case is empty as data
    2274             :  * are already on bcp_column_data
    2275             :  */
    2276             : static TDSRET
    2277        1506 : _bcp_no_get_col_data(TDSBCPINFO *bcpinfo TDS_UNUSED, TDSCOLUMN *bindcol TDS_UNUSED, int offset TDS_UNUSED)
    2278             : {
    2279        1506 :         return TDS_SUCCESS;
    2280             : }
    2281             : 
    2282             : /**
    2283             :  * Get the data for bcp-in from program variables, where the program data
    2284             :  * have been identified as character terminated,  
    2285             :  * This is a low-level, internal function.  Call it correctly.  
    2286             :  */
    2287             : /** 
    2288             :  * \ingroup dblib_bcp_internal
    2289             :  * \brief 
    2290             :  *
    2291             :  * \param pdata 
    2292             :  * \param term 
    2293             :  * \param term_len 
    2294             :  * 
    2295             :  * \return data length.
    2296             :  */
    2297             : static int
    2298          88 : _bcp_get_term_var(const BYTE * pdata, const BYTE * term, int term_len)
    2299             : {
    2300             :         int bufpos;
    2301             : 
    2302          88 :         assert(term_len > 0);
    2303             : 
    2304             :         /* if bufpos becomes negative, we probably failed to find the terminator */
    2305         330 :         for (bufpos = 0; bufpos >= 0 && memcmp(pdata, term, term_len) != 0; pdata++) {
    2306         330 :                 bufpos++;
    2307             :         }
    2308             :         
    2309             :         assert(bufpos >= 0);
    2310          88 :         return bufpos;
    2311             : }
    2312             : 
    2313             : /** 
    2314             :  * \ingroup dblib_bcp_internal
    2315             :  * \brief trim a string of trailing blanks 
    2316             :  *
    2317             :  *      Replaces spaces at the end of a string with NULs
    2318             :  * \param str pointer to a character buffer (not null-terminated)
    2319             :  * \param len size of the \a str in bytes 
    2320             :  * 
    2321             :  * \return modified length
    2322             :  */
    2323             : static int
    2324             : rtrim(char *str, int len)
    2325             : {
    2326        1022 :         char *p = str + len - 1;
    2327             : 
    2328        1516 :         while (p > str && *p == ' ') {
    2329         494 :                 *p-- = '\0';
    2330             :         }
    2331        1022 :         return (int)(1 + p - str);
    2332             : }
    2333             : 
    2334             : static int
    2335             : rtrim_u16(uint16_t *str, int len, uint16_t space)
    2336             : {
    2337          50 :         uint16_t *p = str + len / 2 - 1;
    2338             : 
    2339          50 :         while (p > str && *p == space) {
    2340           0 :                 *p-- = '\0';
    2341             :         }
    2342          50 :         return (int)(1 + p - str) * 2;
    2343             : }
    2344             : 
    2345             : /** 
    2346             :  * \ingroup dblib_bcp_internal
    2347             :  * \brief 
    2348             :  *
    2349             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2350             :  */
    2351             : static void
    2352         804 : _bcp_free_columns(DBPROCESS * dbproc)
    2353             : {
    2354             :         int i;
    2355             : 
    2356         804 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_free_columns(%p)\n", dbproc);
    2357         804 :         assert(dbproc && dbproc->hostfileinfo);
    2358             : 
    2359         804 :         if (dbproc->hostfileinfo->host_columns) {
    2360        2960 :                 for (i = 0; i < dbproc->hostfileinfo->host_colcount; i++) {
    2361        2960 :                         TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns[i]->terminator);
    2362        2960 :                         TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns[i]);
    2363             :                 }
    2364         536 :                 TDS_ZERO_FREE(dbproc->hostfileinfo->host_columns);
    2365         536 :                 dbproc->hostfileinfo->host_colcount = 0;
    2366             :         }
    2367         804 : }
    2368             : 
    2369             : /** 
    2370             :  * \ingroup dblib_bcp_internal
    2371             :  * \brief 
    2372             :  *
    2373             :  * \param dbproc contains all information needed by db-lib to manage communications with the server.
    2374             :  * 
    2375             :  * \sa  bcp_done(), bcp_exec(), bcp_init()
    2376             :  */
    2377             : static void
    2378         594 : _bcp_free_storage(DBPROCESS * dbproc)
    2379             : {
    2380         594 :         tdsdump_log(TDS_DBG_FUNC, "_bcp_free_storage(%p)\n", dbproc);
    2381         594 :         assert(dbproc);
    2382             : 
    2383         594 :         if (dbproc->hostfileinfo) {
    2384         268 :                 TDS_ZERO_FREE(dbproc->hostfileinfo->hostfile);
    2385         268 :                 TDS_ZERO_FREE(dbproc->hostfileinfo->errorfile);
    2386         268 :                 _bcp_free_columns(dbproc);
    2387         268 :                 TDS_ZERO_FREE(dbproc->hostfileinfo);
    2388             :         }
    2389             : 
    2390         594 :         tds_free_bcpinfo(dbproc->bcpinfo);
    2391         594 :         dbproc->bcpinfo = NULL;
    2392         594 : }
    2393             : 

Generated by: LCOV version 1.13