Re: bug fixes

Frediano Ziglio <[email protected]>
Newsgroups gmane.comp.db.tds.freetds
Message-ID <[email protected]>
Thanks Christos,

Your patch contains a lot of stuff. I think that the only really bug
fix is queryout, other stuff will go to HEAD. Note that defncopy patch
is wrong, you save password to a variable and not use it.

Regards
  Frediano

2011/5/24 Christos Zoulas <[email protected]>:
> Hello,
>
> Here are a bunch of fixes, some I sent before:
>
> - Add a new getpassarg function in replacements that centralizes password
>  retrieval from the command line arguments, and setting the command line
>  argument to *'s so that it is not visible with ps. This was done
>  differently in many places. Handling it in one place only makes sense.
>
> - Fix the freebcp usage to mention queryout.
>
> - In tsql add a usage function, and a flag [-a] to set the application
>  name from the command line.
>
> - Delete trailing spaces from ct.c
>
> - bcp queryout broke recently: Fix broken code in bulk.c that attempted
>  to get just the metadata using "select * from %s where 0 = 1".
>  Guess what? %s is not the table name in queryout... Fix it properly
>  using FMTONLY like it was done in freebcp.c. Again this code is
>  duplicated from freebcp.c and we probably don't need to do it
>  there anymore, and we should move the functionality to the library.
>
> christos
>
> --- /dev/null   2011-05-24 09:57:14.000000000 -0400
> +++ getpassarg.c        2011-05-24 14:00:53.000000000 -0400
> @@ -0,0 +1,74 @@
> +/* FreeTDS - Library of routines accessing Sybase and Microsoft databases
> + * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005  Brian Bruns
> + * Copyright (C) 2006, 2007, 2008, 2009, 2010  Frediano Ziglio
> + *
> + * This library is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Library General Public
> + * License as published by the Free Software Foundation; either
> + * version 2 of the License, or (at your option) any later version.
> + *
> + * This library is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Library General Public License for more details.
> + *
> + * You should have received a copy of the GNU Library General Public
> + * License along with this library; if not, write to the
> + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
> + * Boston, MA 02111-1307, USA.
> + */
> +
> +#if HAVE_CONFIG_H
> +#include <config.h>
> +#endif
> +
> +#include <stdio.h>
> +
> +#if HAVE_LIMITS_H
> +#include <limits.h>
> +#endif
> +
> +#if HAVE_STRING_H
> +#include <string.h>
> +#endif /* HAVE_STRING_H */
> +
> +#include <tds.h>
> +#include <tdsthread.h>
> +#include <tdsconvert.h>
> +#include <replacements.h>
> +#include <sybfront.h>
> +#include <sybdb.h>
> +#include <syberror.h>
> +#include <dblib.h>
> +
> +#ifdef DMALLOC
> +#include <dmalloc.h>
> +#endif
> +
> +TDS_RCSID(var, "Id: getpass.c,v 1.1.1.14 2010/06/07 14:37:14 christos Exp");
> +
> +/*
> + * return a copy of the password, reading from stdin if arg is '-'
> + * trashing he argument in the process.
> + */
> +char *
> +getpassarg(char *arg)
> +{
> +       char pwd[255], *ptr, *q;
> +
> +       if (strcmp(arg, "-") == 0) {
> +               memset(pwd, 0, sizeof(pwd));
> +               if (fgets(pwd, sizeof(pwd), stdin) == NULL)
> +                       return NULL;
> +               ptr = strchr(pwd, '\n');
> +               if (ptr) *ptr = '\0';
> +               arg = pwd;
> +       }
> +
> +       ptr = strdup(arg);
> +
> +       for (q = arg; *q; *q++ = '*')
> +               continue;
> +
> +       return ptr;
> +}
>
> Index: include/replacements.h
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/include/replacements.h,v
> retrieving revision 1.1.1.9
> retrieving revision 1.3
> diff -u -r1.1.1.9 -r1.3
> --- include/replacements.h      23 May 2011 18:18:07 -0000      1.1.1.9
> +++ include/replacements.h      23 May 2011 18:29:07 -0000      1.3
> @@ -96,6 +96,8 @@
>  char *tds_basename(char *path);
>  #endif
>
> +char *getpassarg(char *arg);
> +
>  /*
>  * Microsoft's C Runtime library is missing strcasecmp and strncasecmp.
>  * Other Win32 C runtime libraries, notably minwg, may define it.
> Index: src/apps/bsqldb.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/bsqldb.c,v
> retrieving revision 1.1.1.8
> retrieving revision 1.4
> diff -u -r1.1.1.8 -r1.4
> --- src/apps/bsqldb.c   23 May 2011 18:18:07 -0000      1.1.1.8
> +++ src/apps/bsqldb.c   23 May 2011 21:04:43 -0000      1.4
> @@ -828,8 +828,7 @@
>                        username = strdup(optarg);
>                        break;
>                case 'P':
> -                       password = strdup(optarg);
> -                       memset(optarg, 0, strlen(optarg));
> +                       password = getpassarg(optarg);
>                        break;
>                case 'S':
>                        options->servername = strdup(optarg);
> @@ -889,10 +888,10 @@
>                DBSETLPWD(login, password);
>                memset(password, 0, strlen(password));
>        } else if (username) {
> -               char password[128];
> +               char pwd[128];
>
> -               readpassphrase("Password: ", password, sizeof(password), RPP_ECHO_OFF);
> -               DBSETLPWD(login, password);
> +               readpassphrase("Password: ", pwd, sizeof(pwd), RPP_ECHO_OFF);
> +               DBSETLPWD(login, pwd);
>         }
>
>        if (!options->servername) {
> Index: src/apps/bsqlodbc.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/bsqlodbc.c,v
> retrieving revision 1.1.1.5
> retrieving revision 1.3
> diff -u -r1.1.1.5 -r1.3
> Index: src/apps/defncopy.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/defncopy.c,v
> retrieving revision 1.1.1.7
> retrieving revision 1.3
> diff -u -r1.1.1.7 -r1.3
> --- src/apps/defncopy.c 23 May 2011 18:18:07 -0000      1.1.1.7
> +++ src/apps/defncopy.c 23 May 2011 18:29:07 -0000      1.3
> @@ -652,6 +652,7 @@
>  get_login(int argc, char *argv[], OPTIONS *options)
>  {
>        LOGINREC *login;
> +       char *password;
>        int ch;
>        int fdomain = TRUE;
>
> @@ -682,8 +683,7 @@
>                        fdomain = FALSE;
>                        break;
>                case 'P':
> -                       DBSETLPWD(login, optarg);
> -                       memset(optarg, 0, strlen(optarg));
> +                       password = getpassarg(optarg);
>                        fdomain = FALSE;
>                        break;
>                case 'S':
> Index: src/apps/freebcp.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/freebcp.c,v
> retrieving revision 1.1.1.12
> retrieving revision 1.4
> diff -u -r1.1.1.12 -r1.4
> --- src/apps/freebcp.c  23 May 2011 18:18:07 -0000      1.1.1.12
> +++ src/apps/freebcp.c  23 May 2011 21:56:14 -0000      1.4
> @@ -267,18 +267,7 @@
>                        break;
>                case 'P':
>                        pdata->Pflag++;
> -                       if ((strcmp(optarg, "-")) == 0) {
> -                               char pwd[255], *nl;
> -                               memset(pwd, 0, 255);
> -                               fgets(pwd, 255, stdin);
> -                               nl = strchr(pwd, '\n');
> -                               if(nl) *nl = '\0';
> -                               pdata->pass = strdup(pwd);
> -                               memset(pwd, 0, 255);
> -                       } else {
> -                               pdata->pass = strdup(optarg);
> -                               memset(optarg, 0, strlen(optarg));
> -                       }
> +                       pdata->pass = getpassarg(optarg);
>                        break;
>                case 'i':
>                        free(pdata->inputfile);
> @@ -761,7 +750,7 @@
>  void
>  pusage(void)
>  {
> -       fprintf(stderr, "usage:  freebcp [[database_name.]owner.]table_name {in | out} datafile\n");
> +       fprintf(stderr, "usage:  freebcp [[database_name.]owner.]table_name|query {in | out | queryout } datafile\n");
>        fprintf(stderr, "        [-m maxerrors] [-f formatfile] [-e errfile]\n");
>        fprintf(stderr, "        [-F firstrow] [-L lastrow] [-b batchsize]\n");
>        fprintf(stderr, "        [-n] [-c] [-t field_terminator] [-r row_terminator]\n");
> Index: src/apps/tsql.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/apps/tsql.c,v
> retrieving revision 1.1.1.12
> retrieving revision 1.20
> diff -u -r1.1.1.12 -r1.20
> --- src/apps/tsql.c     23 May 2011 18:18:07 -0000      1.1.1.12
> +++ src/apps/tsql.c     23 May 2011 18:29:07 -0000      1.20
> @@ -292,6 +292,23 @@
>  }
>
>  static void
> +tsql_print_usage(const char *progname)
> +{
> +       fprintf(stderr,
> +               "Usage:\t%s [-a <appname>] [-S <server> | -H <hostname> -p <port>] -U <username> [-P <password>] [-I <config file>] [-o <options>] [-t delim] [-r delim] [-D database]\n"
> +               "\t%s -C\n"
> +               "Options:\n"
> +               "\tf\tDo not print footer\n"
> +               "\th\tDo not print header\n"
> +               "\tt\tPrint time informations\n"
> +               "\tv\tPrint TDS version\n"
> +               "\tq\tQuiet\n\n"
> +               "\tDelimiters can be multi-char strings appropriately escaped for your shell.\n"
> +               "\tDefault column delimitor is <tab>; default row delimiter is <newline>\n",
> +               progname, progname);
> +}
> +
> +static void
>  reset_getopt(void)
>  {
>  #ifdef HAVE_GETOPT_OPTRESET
> @@ -390,12 +410,18 @@
>        char *hostname = NULL, *servername = NULL;
>        char *username = NULL, *password = NULL;
>        char *confile = NULL;
> +       const char *appname = "TSQL";
> +       const char *locale = NULL;
>        int opt, port=0, use_domain_login=0;
>        const char *charset = NULL;
>        char *opt_flags_str = NULL;
>
> -       while ((opt = getopt(argc, argv, "H:S:I:J:P:U:p:Co:t:r:D:Lv")) != -1) {
> +       while ((opt = getopt(argc, argv, "a:H:S:I:J:P:U:p:Co:t:r:D:Lv")) != -1)
> +       {
>                switch (opt) {
> +               case 'a':
> +                       appname = optarg;
> +                       break;
>                case 't':
>                        opt_col_term = strdup(optarg);
>                        break;
> @@ -422,8 +448,7 @@
>                        break;
>                case 'P':
>                        free(password);
> -                       password = strdup(optarg);
> -                       memset(optarg, 0, strlen(optarg));
> +                       password = getpassarg(optarg);
>                        break;
>                case 'I':
>                        free(confile);
> @@ -461,7 +486,7 @@
>                        exit(0);
>                        break;
>                default:
> -                       fprintf(stderr, "%s: error: invalid option %c\n", argv[0], (char)opt);
> +                       tsql_print_usage(argv[0]);
>                        exit(1);
>                        break;
>                }
> @@ -544,7 +569,7 @@
>        /* if it's a servername */
>        if (servername) {
>                tds_set_user(login, username);
> -               tds_set_app(login, "TSQL");
> +               tds_set_app(login, appname);
>                tds_set_library(login, "TDS-Library");
>                tds_set_server(login, servername);
>                if (charset) tds_set_client_charset(login, charset);
> @@ -556,7 +581,7 @@
>                /* else we specified hostname/port */
>        } else {
>                tds_set_user(login, username);
> -               tds_set_app(login, "TSQL");
> +               tds_set_app(login, appname);
>                tds_set_library(login, "TDS-Library");
>                tds_set_server(login, hostname);
>                tds_set_port(login, port);
> Index: src/ctlib/ct.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/ctlib/ct.c,v
> retrieving revision 1.1.1.16
> retrieving revision 1.17
> diff -u -r1.1.1.16 -r1.17
> --- src/ctlib/ct.c      23 May 2011 18:18:07 -0000      1.1.1.16
> +++ src/ctlib/ct.c      23 May 2011 18:29:08 -0000      1.17
> @@ -620,11 +620,11 @@
>        /* override locale settings with CS_CONNECTION settings, if any */
>        if (con->locale) {
>                if (con->locale->charset) {
> -                       if (!tds_dstr_copy(&connection->server_charset, con->locale->charset))
> +                       if (!tds_dstr_copy(&connection->server_charset, con->locale->charset))
>                                goto Cleanup;
>                }
>                if (con->locale->language) {
> -                       if (!tds_dstr_copy(&connection->language, con->locale->language))
> +                       if (!tds_dstr_copy(&connection->language, con->locale->language))
>                                goto Cleanup;
>                }
>                if (con->locale->time && con->tds_socket->tds_ctx) {
> @@ -3467,7 +3467,7 @@
>                case CS_FALSE:
>                        break;  /* end valid choices */
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                }
>                break;
> @@ -3480,7 +3480,7 @@
>                        tds_option = TDS_OPT_ARITHABORTOFF;
>                        break;
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                        tds_option = TDS_OPT_ARITHABORTON;
>                }
> @@ -3496,7 +3496,7 @@
>                        tds_option = TDS_OPT_ARITHIGNOREOFF;
>                        break;
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                }
>                tds_argument.i = TDS_OPT_ARITHOVERFLOW | TDS_OPT_NUMERICTRUNC;
> @@ -3538,7 +3538,7 @@
>                        tds_argument.ti = TDS_OPT_SATURDAY;
>                        break;
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                }
>                tds_argsize = (action == CS_SET) ? 1 : 0;
> @@ -3565,7 +3565,7 @@
>                        tds_argument.ti = TDS_OPT_FMTDYM;
>                        break;
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                }
>                tds_argsize = (action == CS_SET) ? 1 : 0;
> @@ -3584,7 +3584,7 @@
>                        tds_argument.ti = TDS_OPT_LEVEL3;
>                        break;
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                }
>                tds_argsize = (action == CS_SET) ? 1 : 0;
> @@ -3596,7 +3596,7 @@
>                case CS_FALSE:
>                        break;
>                default:
> -                       if (action == CS_SET)
> +                       if (action == CS_SET)
>                                return CS_FAIL;
>                }
>                tds_argument.ti = !*(char *) param;
> Index: src/replacements/Makefile.am
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/replacements/Makefile.am,v
> retrieving revision 1.1.1.10
> retrieving revision 1.2
> diff -u -r1.1.1.10 -r1.2
> --- src/replacements/Makefile.am        23 May 2011 18:18:07 -0000      1.1.1.10
> +++ src/replacements/Makefile.am        23 Jun 2010 21:16:33 -0000      1.2
> @@ -1,13 +1,12 @@
> -# Id: Makefile.am,v 1.17 2010/10/12 15:36:24 jklowden Exp
> +# $Id: Makefile.am,v 1.2 2010/06/23 21:16:33 christos Exp $
>  AM_CPPFLAGS=                   -I$(top_srcdir)/include -I$(top_srcdir)/src/replacements
>  noinst_LTLIBRARIES=            libreplacements.la
> -libreplacements_la_SOURCES=    iconv.c gettimeofday.c fakepoll.c
> +libreplacements_la_SOURCES=    iconv.c gettimeofday.c fakepoll.c getpassarg.c
>  libreplacements_la_LDFLAGS=
>  libreplacements_la_LIBADD=     @LTLIBOBJS@
>  EXTRA_DIST=    asprintf.c \
>                atoll.c \
>                basename.c \
> -               getopt.c \
>                readpassphrase.c \
>                strlcat.c \
>                strlcpy.c \
> Index: src/tds/bulk.c
> ===================================================================
> RCS file: /src/twosigma/cvsroot/external/public/freetds/src/tds/bulk.c,v
> retrieving revision 1.1.1.4
> retrieving revision 1.2
> diff -u -r1.1.1.4 -r1.2
> --- src/tds/bulk.c      23 May 2011 18:18:07 -0000      1.1.1.4
> +++ src/tds/bulk.c      23 May 2011 21:55:06 -0000      1.2
> @@ -37,6 +37,7 @@
>  #include "tds_checks.h"
>  #include "tdsbytes.h"
>  #include "replacements.h"
> +#include "sybdb.h"
>  #ifdef DMALLOC
>  #include <dmalloc.h>
>  #endif
> @@ -68,11 +69,17 @@
>        TDSCOLUMN *curcol;
>        TDS_INT result_type;
>        int i, rc;
> +       const char *fmt;
>
>        /* FIXME don't leave state in processing state */
>
>        /* TODO quote tablename if needed */
> -       if (tds_submit_queryf(tds, "select * from %s where 0 = 1", bcpinfo->tablename) == TDS_FAIL)
> +       if (bcpinfo->direction != DB_QUERYOUT)
> +           fmt = "SET FMTONLY ON select * from %s SET FMTONLY OFF";
> +       else
> +           fmt = "SET FMTONLY ON %s SET FMTONLY OFF";
> +
> +       if (tds_submit_queryf(tds, fmt, bcpinfo->tablename) == TDS_FAIL)
>                /* TODO return an error ?? */
>                /* Attempt to use Bulk Copy with a non-existent Server table (might be why ...) */
>                return TDS_FAIL;
> _______________________________________________
> FreeTDS mailing list
> [email protected]
> http://lists.ibiblio.org/mailman/listinfo/freetds
>
_______________________________________________
FreeTDS mailing list
[email protected]
http://lists.ibiblio.org/mailman/listinfo/freetds
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.