Thread: system() patch for Win32

system() patch for Win32

From
Bruce Momjian
Date:
Here is a patch to handle 'rm', 'cp', and 'exec' usage by system();
again very small.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/commands/dbcommands.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/commands/dbcommands.c,v
retrieving revision 1.110
diff -c -c -r1.110 dbcommands.c
*** src/backend/commands/dbcommands.c    27 Jan 2003 00:46:41 -0000    1.110
--- src/backend/commands/dbcommands.c    3 Apr 2003 05:24:39 -0000
***************
*** 302,308 ****
--- 302,312 ----
      }

      /* Copy the template database to the new location */
+ #ifdef WIN32
+     snprintf(buf, sizeof(buf), "xcopy /e /i /q '%s' '%s'", src_loc, target_dir);
+ #else
      snprintf(buf, sizeof(buf), "cp -r '%s' '%s'", src_loc, target_dir);
+ #endif

      if (system(buf) != 0)
      {
***************
*** 751,757 ****
--- 755,765 ----
          }
      }

+ #ifdef WIN32
+     snprintf(buf, sizeof(buf), "rmdir /s /q \"%s\"", target_dir);
+ #else
      snprintf(buf, sizeof(buf), "rm -rf '%s'", target_dir);
+ #endif

      if (system(buf) != 0)
      {
Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.94
diff -c -c -r1.94 command.c
*** src/bin/psql/command.c    20 Mar 2003 06:43:35 -0000    1.94
--- src/bin/psql/command.c    3 Apr 2003 05:24:45 -0000
***************
*** 66,72 ****
  static bool do_connect(const char *new_dbname, const char *new_user);
  static bool do_shell(const char *command);

!

  /*----------
   * HandleSlashCmds:
--- 66,76 ----
  static bool do_connect(const char *new_dbname, const char *new_user);
  static bool do_shell(const char *command);

! #ifndef    WIN32
! #define    EXEC    "exec "
! #else
! #define    EXEC    ""
! #endif

  /*----------
   * HandleSlashCmds:
***************
*** 1515,1521 ****
      sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1);
      if (!sys)
          return false;
!     sprintf(sys, "exec  %s '%s'", editorName, fname);
      result = system(sys);
      if (result == -1)
          psql_error("could not start editor %s\n", editorName);
--- 1519,1525 ----
      sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1);
      if (!sys)
          return false;
!     sprintf(sys, "%s%s '%s'", EXEC, editorName, fname);
      result = system(sys);
      if (result == -1)
          psql_error("could not start editor %s\n", editorName);
***************
*** 1944,1950 ****
              else
                  exit(EXIT_FAILURE);
          }
!         sprintf(sys, "exec %s", shellName);
          result = system(sys);
          free(sys);
      }
--- 1948,1954 ----
              else
                  exit(EXIT_FAILURE);
          }
!         sprintf(sys, "%s%s", EXEC, shellName);
          result = system(sys);
          free(sys);
      }

Re: system() patch for Win32

From
Tom Lane
Date:
Bruce Momjian <pgman@candle.pha.pa.us> writes:
> Here is a patch to handle 'rm', 'cp', and 'exec' usage by system();
> again very small.

This part seems unnecessarily obscurantist:

> ! #ifndef    WIN32
> ! #define    EXEC    "exec "
> ! #else
> ! #define    EXEC    ""
> ! #endif

> !     sprintf(sys, "%s%s '%s'", EXEC, editorName, fname);

I like the other style (just #ifdef to choose one of two sprintf
commands) better.  It seems easier to understand, as well as less
fragile --- the way you have it here makes it *real* easy to break
the computation of the buffer size for the command string.

            regards, tom lane


Re: system() patch for Win32

From
Bruce Momjian
Date:
OK, I have modified the use of "exec" to be more local to the snprintf.

---------------------------------------------------------------------------

Tom Lane wrote:
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > Here is a patch to handle 'rm', 'cp', and 'exec' usage by system();
> > again very small.
>
> This part seems unnecessarily obscurantist:
>
> > ! #ifndef    WIN32
> > ! #define    EXEC    "exec "
> > ! #else
> > ! #define    EXEC    ""
> > ! #endif
>
> > !     sprintf(sys, "%s%s '%s'", EXEC, editorName, fname);
>
> I like the other style (just #ifdef to choose one of two sprintf
> commands) better.  It seems easier to understand, as well as less
> fragile --- the way you have it here makes it *real* easy to break
> the computation of the buffer size for the command string.
>
>             regards, tom lane
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
>

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/commands/dbcommands.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/backend/commands/dbcommands.c,v
retrieving revision 1.110
diff -c -c -r1.110 dbcommands.c
*** src/backend/commands/dbcommands.c    27 Jan 2003 00:46:41 -0000    1.110
--- src/backend/commands/dbcommands.c    4 Apr 2003 14:49:19 -0000
***************
*** 302,308 ****
--- 302,312 ----
      }

      /* Copy the template database to the new location */
+ #ifndef WIN32
      snprintf(buf, sizeof(buf), "cp -r '%s' '%s'", src_loc, target_dir);
+ #else
+     snprintf(buf, sizeof(buf), "xcopy /e /i /q '%s' '%s'", src_loc, target_dir);
+ #endif

      if (system(buf) != 0)
      {
***************
*** 751,757 ****
--- 755,765 ----
          }
      }

+ #ifndef WIN32
      snprintf(buf, sizeof(buf), "rm -rf '%s'", target_dir);
+ #else
+     snprintf(buf, sizeof(buf), "rmdir /s /q \"%s\"", target_dir);
+ #endif

      if (system(buf) != 0)
      {
Index: src/bin/psql/command.c
===================================================================
RCS file: /cvsroot/pgsql-server/src/bin/psql/command.c,v
retrieving revision 1.94
diff -c -c -r1.94 command.c
*** src/bin/psql/command.c    20 Mar 2003 06:43:35 -0000    1.94
--- src/bin/psql/command.c    4 Apr 2003 14:49:37 -0000
***************
*** 66,73 ****
  static bool do_connect(const char *new_dbname, const char *new_user);
  static bool do_shell(const char *command);

-
-
  /*----------
   * HandleSlashCmds:
   *
--- 66,71 ----
***************
*** 1515,1521 ****
      sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1);
      if (!sys)
          return false;
!     sprintf(sys, "exec  %s '%s'", editorName, fname);
      result = system(sys);
      if (result == -1)
          psql_error("could not start editor %s\n", editorName);
--- 1513,1523 ----
      sys = malloc(strlen(editorName) + strlen(fname) + 10 + 1);
      if (!sys)
          return false;
!     sprintf(sys,
! #ifndef WIN32
!         "exec "
! #endif
!         "%s '%s'", editorName, fname);
      result = system(sys);
      if (result == -1)
          psql_error("could not start editor %s\n", editorName);
***************
*** 1944,1950 ****
              else
                  exit(EXIT_FAILURE);
          }
!         sprintf(sys, "exec %s", shellName);
          result = system(sys);
          free(sys);
      }
--- 1946,1956 ----
              else
                  exit(EXIT_FAILURE);
          }
!         sprintf(sys,
! #ifndef WIN32
!             "exec "
! #endif
!             "%s", shellName);
          result = system(sys);
          free(sys);
      }