1. /*
  2. * @(#)Win32Process.java 1.26 03/02/20
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.io.*;
  9. class Win32Process extends Process {
  10. private long handle = 0;
  11. private FileDescriptor stdin_fd;
  12. private FileDescriptor stdout_fd;
  13. private FileDescriptor stderr_fd;
  14. private OutputStream stdin_stream;
  15. private InputStream stdout_stream;
  16. private InputStream stderr_stream;
  17. Win32Process(String cmd[], String env[]) throws Exception {
  18. this(cmd, env, null);
  19. }
  20. Win32Process(String cmd[], String env[], String path) throws Exception {
  21. StringBuffer cmdbuf = new StringBuffer(80);
  22. for (int i = 0; i < cmd.length; i++) {
  23. if (i > 0) {
  24. cmdbuf.append(' ');
  25. }
  26. String s = cmd[i];
  27. if (s.indexOf(' ') >= 0 || s.indexOf('\t') >= 0) {
  28. if (s.charAt(0) != '"') {
  29. cmdbuf.append('"');
  30. cmdbuf.append(s);
  31. if (s.endsWith("\\")) {
  32. cmdbuf.append("\\");
  33. }
  34. cmdbuf.append('"');
  35. } else if (s.endsWith("\"")) {
  36. /* The argument has already been quoted. */
  37. cmdbuf.append(s);
  38. } else {
  39. /* Unmatched quote for the argument. */
  40. throw new IllegalArgumentException();
  41. }
  42. } else {
  43. cmdbuf.append(s);
  44. }
  45. }
  46. String cmdstr = cmdbuf.toString();
  47. String envstr = null;
  48. if (env != null) {
  49. StringBuffer envbuf = new StringBuffer(256);
  50. for (int i = 0; i < env.length; i++) {
  51. envbuf.append(env[i]).append('\0');
  52. }
  53. envstr = envbuf.toString();
  54. }
  55. stdin_fd = new FileDescriptor();
  56. stdout_fd = new FileDescriptor();
  57. stderr_fd = new FileDescriptor();
  58. handle = create(cmdstr, envstr, path, stdin_fd, stdout_fd, stderr_fd);
  59. java.security.AccessController.doPrivileged(
  60. new java.security.PrivilegedAction() {
  61. public Object run() {
  62. stdin_stream =
  63. new BufferedOutputStream(new FileOutputStream(stdin_fd));
  64. stdout_stream =
  65. new BufferedInputStream(new FileInputStream(stdout_fd));
  66. stderr_stream =
  67. new FileInputStream(stderr_fd);
  68. return null;
  69. }
  70. });
  71. }
  72. public OutputStream getOutputStream() {
  73. return stdin_stream;
  74. }
  75. public InputStream getInputStream() {
  76. return stdout_stream;
  77. }
  78. public InputStream getErrorStream() {
  79. return stderr_stream;
  80. }
  81. public void finalize() {
  82. close();
  83. }
  84. public native int exitValue();
  85. public native int waitFor();
  86. public native void destroy();
  87. private native long create(String cmdstr, String envstr, String path,
  88. FileDescriptor in_fd,
  89. FileDescriptor out_fd,
  90. FileDescriptor err_fd);
  91. private native void close();
  92. }