[freetds] getopt compatibility

ZIGLIO, Frediano, VF-IT Frediano.Ziglio at vodafone.com
Tue May 27 11:09:17 EDT 2008


....
> 
> yes, I think I'll try to review get_opt_flags
> 

Something like this (still to test)

Index: src/apps/tsql.c
===================================================================
RCS file: /cvs/freetds/freetds/src/apps/tsql.c,v
retrieving revision 1.115
diff -u -1 -0 -u -b -r1.115 tsql.c
--- src/apps/tsql.c     27 May 2008 08:25:04 -0000      1.115
+++ src/apps/tsql.c     27 May 2008 15:08:22 -0000
@@ -306,58 +306,72 @@
 /*
  * The 'GO' command may be followed by options that apply to the batch.
  * If they don't appear to be right, assume the letters "go" are part
of the
  * SQL, not a batch separator.
  */
 static int
 get_opt_flags(char *s, int *opt_flags)
 {
        char **argv;
        int argc;
-       int opt;

        /* make sure we have enough elements */
        assert(s && opt_flags);
        argv = (char **) calloc(strlen(s) + 2, sizeof(char*));
        if (!argv)
                return 0;

        /* parse the command line and assign to argv */
        for (argc=0; (argv[argc] = strtok(s, " ")) != NULL; argc++)
                s = NULL;

        *opt_flags = 0;
        optind = 0;             /* reset getopt */
        opterr = 0;             /* suppress error messages */
-       while ((opt = getopt(argc, argv, "fhqtv")) != -1) {
-               switch (opt) {
+       while (++argv != NULL) {
+               const char *p_opt = *argv;
+
+               /* arguments finished */
+               if (!p_opt)
+                       break;
+
+               if (*p_opt++ != '-')
+                       continue;
+
+               /* end of options */
+               if (strcmp(p_opt, "-") == 0)
+                       break;
+
+               for (; *p_opt; ++p_opt) {
+                       switch (*p_opt) {
                case 'f':
                        *opt_flags |= OPT_NOFOOTER;
                        break;
                case 'h':
                        *opt_flags |= OPT_NOHEADER;
                        break;
                case 't':
                        *opt_flags |= OPT_TIMER;
                        break;
                case 'v':
                        *opt_flags |= OPT_VERSION;
                        break;
                case 'q':
                        *opt_flags |= OPT_QUIET;
                        break;
                default:
-                       fprintf(stderr, "Warning: invalid option '%s'
found: \"go\" treated as simple SQL\n", argv[optind-1]);
+                               fprintf(stderr, "Warning: invalid option
'%c' found: \"go\" treated as simple SQL\n", *p_opt);
                        free(argv);
                        return 0;
                }
        }
+       }

        free(argv);
        return 1;
 }

 static void
 populate_login(TDSLOGIN * login, int argc, char **argv)
 {
        const TDS_COMPILETIME_SETTINGS *settings;
        char *hostname = NULL;

freddy77



More information about the FreeTDS mailing list