       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
     582
     583
     584
     585
     586
     587
     588
     589
     590
     591
     592
     593
     594
     595
     596
     597
     598
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
     760
     761
     762
     763
     764
     765
     766
     767
     768
     769
     770
     771
     772
     773
     774
     775
     776
     777
     778
     779
     780
     781
     782
     783
     784
     785
     786
     787
     788
     789
     790
     791
     792
     793
     794
     795
     796
     797
     798
     799
     800
     801
     802
     803
     804
     805
     806
     807
     808
     809
     810
     811
     812
     813
     814
     815
     816
     817
     818
     819
     820
     821
     822
     823
     824
     825
     826
     827
     828
     829
     830
     831
     832
     833
     834
     835
     836
     837
     838
     839
     840
     841
     842
     843
     844
     845
     846
     847
     848
     849
     850
     851
     852
     853
     854
     855
     856
     857
     858
     859
     860
     861
     862
     863
     864
     865
     866
     867
     868
     869
     870
     871
     872
     873
     874
     875
     876
     877
     878
     879
     880
     881
     882
     883
     884
     885
     886
     887
     888
     889
     890
     891
     892
     893
     894
     895
     896
     897
     898
     899
     900
     901
     902
     903
     904
     905
     906
     907
     908
     909
     910
     911
     912
     913
     914
     915
     916
     917
     918
     919
     920
     921
     922
     923
     924
     925
     926
     927
     928
     929
     930
     931
     932
     933
     934
     935
     936
     937
     938
     939
     940
     941
     942
     943
     944
     945
     946
     947
     948
     949
     950
     951
     952
     953
     954
     955
     956
     957
     958
     959
     960
     961
     962
     963
     964
     965
     966
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
     986
     987
     988
     989
     990
     991
     992
     993
     994
     995
     996
     997
     998
     999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440
    1441
    1442
    1443
    1444
    1445
    1446
    1447
    1448
    1449
    1450
    1451
    1452
    1453
    1454
    1455
    1456
    1457
    1458
    1459
    1460
    1461
    1462
    1463
    1464
    1465
    1466
    1467
    1468
    1469
    1470
    1471
    1472
    1473
    1474
    1475
    1476
    1477
    1478
    1479
    1480
    1481
    1482
    1483
    1484
    1485
    1486
    1487
    1488
    1489
    1490
    1491
    1492
    1493
    1494
    1495
    1496
    1497
    1498
    1499
    1500
    1501
    1502
    1503
    1504
    1505
    1506
    1507
    1508
    1509
    1510
    1511
    1512
    1513
    1514
    1515
    1516
    1517
    1518

    // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    //! A module for working with processes.
    //!
    //! # Examples
    //!
    //! Basic usage where we try to execute the `cat` shell command:
    //!
    //! ```should_panic
    //! use std::process::Command;
    //!
    //! let mut child = Command::new("/bin/cat")
    //!                         .arg("file.txt")
    //!                         .spawn()
    //!                         .expect("failed to execute child");
    //!
    //! let ecode = child.wait()
    //!                  .expect("failed to wait on child");
    //!
    //! assert!(ecode.success());
    //! ```
    //!
    //! Calling a command with input and reading its output:
    //!
    //! ```no_run
    //! use std::process::{Command, Stdio};
    //! use std::io::Write;
    //!
    //! let mut child = Command::new("/bin/cat")
    //!     .stdin(Stdio::piped())
    //!     .stdout(Stdio::piped())
    //!     .spawn()
    //!     .expect("failed to execute child");
    //!
    //! {
    //!     // limited borrow of stdin
    //!     let stdin = child.stdin.as_mut().expect("failed to get stdin");
    //!     stdin.write_all(b"test").expect("failed to write to stdin");
    //! }
    //!
    //! let output = child
    //!     .wait_with_output()
    //!     .expect("failed to wait on child");
    //!
    //! assert_eq!(b"test", output.stdout.as_slice());
    //! ```

    #![stable(feature = "process", since = "1.0.0")]

    use io::prelude::*;

    use ffi::OsStr;
    use fmt;
    use io;
    use path::Path;
    use str;
    use sys::pipe::{read2, AnonPipe};
    use sys::process as imp;
    use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};

    /// Representation of a running or exited child process.
    ///
    /// This structure is used to represent and manage child processes. A child
    /// process is created via the [`Command`] struct, which configures the
    /// spawning process and can itself be constructed using a builder-style
    /// interface.
    ///
    /// # Examples
    ///
    /// ```should_panic
    /// use std::process::Command;
    ///
    /// let mut child = Command::new("/bin/cat")
    ///                         .arg("file.txt")
    ///                         .spawn()
    ///                         .expect("failed to execute child");
    ///
    /// let ecode = child.wait()
    ///                  .expect("failed to wait on child");
    ///
    /// assert!(ecode.success());
    /// ```
    ///
    /// # Note
    ///
    /// Take note that there is no implementation of [`Drop`] for child processes,
    /// so if you do not ensure the `Child` has exited then it will continue to
    /// run, even after the `Child` handle to the child process has gone out of
    /// scope.
    ///
    /// Calling [`wait`][`wait`] (or other functions that wrap around it) will make
    /// the parent process wait until the child has actually exited before
    /// continuing.
    ///
    /// [`Command`]: struct.Command.html
    /// [`Drop`]: ../../core/ops/trait.Drop.html
    /// [`wait`]: #method.wait
    #[stable(feature = "process", since = "1.0.0")]
    pub struct Child {
        handle: imp::Process,

        /// The handle for writing to the child's stdin, if it has been captured
        #[stable(feature = "process", since = "1.0.0")]
        pub stdin: Option<ChildStdin>,

        /// The handle for reading from the child's stdout, if it has been captured
        #[stable(feature = "process", since = "1.0.0")]
        pub stdout: Option<ChildStdout>,

        /// The handle for reading from the child's stderr, if it has been captured
        #[stable(feature = "process", since = "1.0.0")]
        pub stderr: Option<ChildStderr>,
    }

    impl AsInner<imp::Process> for Child {
        fn as_inner(&self) -> &imp::Process { &self.handle }
    }

    impl FromInner<(imp::Process, imp::StdioPipes)> for Child {
        fn from_inner((handle, io): (imp::Process, imp::StdioPipes)) -> Child {
            Child {
                handle: handle,
                stdin: io.stdin.map(ChildStdin::from_inner),
                stdout: io.stdout.map(ChildStdout::from_inner),
                stderr: io.stderr.map(ChildStderr::from_inner),
            }
        }
    }

    impl IntoInner<imp::Process> for Child {
        fn into_inner(self) -> imp::Process { self.handle }
    }

    #[stable(feature = "std_debug", since = "1.16.0")]
    impl fmt::Debug for Child {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.debug_struct("Child")
                .field("stdin", &self.stdin)
                .field("stdout", &self.stdout)
                .field("stderr", &self.stderr)
                .finish()
        }
    }

    /// A handle to a child process's stdin. This struct is used in the [`stdin`]
    /// field on [`Child`].
    ///
    /// [`Child`]: struct.Child.html
    /// [`stdin`]: struct.Child.html#structfield.stdin
    #[stable(feature = "process", since = "1.0.0")]
    pub struct ChildStdin {
        inner: AnonPipe
    }

    #[stable(feature = "process", since = "1.0.0")]
    impl Write for ChildStdin {
        fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
            self.inner.write(buf)
        }

        fn flush(&mut self) -> io::Result<()> {
            Ok(())
        }
    }

    impl AsInner<AnonPipe> for ChildStdin {
        fn as_inner(&self) -> &AnonPipe { &self.inner }
    }

    impl IntoInner<AnonPipe> for ChildStdin {
        fn into_inner(self) -> AnonPipe { self.inner }
    }

    impl FromInner<AnonPipe> for ChildStdin {
        fn from_inner(pipe: AnonPipe) -> ChildStdin {
            ChildStdin { inner: pipe }
        }
    }

    #[stable(feature = "std_debug", since = "1.16.0")]
    impl fmt::Debug for ChildStdin {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.pad("ChildStdin { .. }")
        }
    }

    /// A handle to a child process's stdout. This struct is used in the [`stdout`]
    /// field on [`Child`].
    ///
    /// [`Child`]: struct.Child.html
    /// [`stdout`]: struct.Child.html#structfield.stdout
    #[stable(feature = "process", since = "1.0.0")]
    pub struct ChildStdout {
        inner: AnonPipe
    }

    #[stable(feature = "process", since = "1.0.0")]
    impl Read for ChildStdout {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            self.inner.read(buf)
        }
        fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
            self.inner.read_to_end(buf)
        }
    }

    impl AsInner<AnonPipe> for ChildStdout {
        fn as_inner(&self) -> &AnonPipe { &self.inner }
    }

    impl IntoInner<AnonPipe> for ChildStdout {
        fn into_inner(self) -> AnonPipe { self.inner }
    }

    impl FromInner<AnonPipe> for ChildStdout {
        fn from_inner(pipe: AnonPipe) -> ChildStdout {
            ChildStdout { inner: pipe }
        }
    }

    #[stable(feature = "std_debug", since = "1.16.0")]
    impl fmt::Debug for ChildStdout {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.pad("ChildStdout { .. }")
        }
    }

    /// A handle to a child process's stderr. This struct is used in the [`stderr`]
    /// field on [`Child`].
    ///
    /// [`Child`]: struct.Child.html
    /// [`stderr`]: struct.Child.html#structfield.stderr
    #[stable(feature = "process", since = "1.0.0")]
    pub struct ChildStderr {
        inner: AnonPipe
    }

    #[stable(feature = "process", since = "1.0.0")]
    impl Read for ChildStderr {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            self.inner.read(buf)
        }
        fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
            self.inner.read_to_end(buf)
        }
    }

    impl AsInner<AnonPipe> for ChildStderr {
        fn as_inner(&self) -> &AnonPipe { &self.inner }
    }

    impl IntoInner<AnonPipe> for ChildStderr {
        fn into_inner(self) -> AnonPipe { self.inner }
    }

    impl FromInner<AnonPipe> for ChildStderr {
        fn from_inner(pipe: AnonPipe) -> ChildStderr {
            ChildStderr { inner: pipe }
        }
    }

    #[stable(feature = "std_debug", since = "1.16.0")]
    impl fmt::Debug for ChildStderr {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.pad("ChildStderr { .. }")
        }
    }

    /// A process builder, providing fine-grained control
    /// over how a new process should be spawned.
    ///
    /// A default configuration can be
    /// generated using `Command::new(program)`, where `program` gives a path to the
    /// program to be executed. Additional builder methods allow the configuration
    /// to be changed (for example, by adding arguments) prior to spawning:
    ///
    /// ```
    /// use std::process::Command;
    ///
    /// let output = if cfg!(target_os = "windows") {
    ///     Command::new("cmd")
    ///             .args(&["/C", "echo hello"])
    ///             .output()
    ///             .expect("failed to execute process")
    /// } else {
    ///     Command::new("sh")
    ///             .arg("-c")
    ///             .arg("echo hello")
    ///             .output()
    ///             .expect("failed to execute process")
    /// };
    ///
    /// let hello = output.stdout;
    /// ```
    #[stable(feature = "process", since = "1.0.0")]
    pub struct Command {
        inner: imp::Command,
    }

    impl Command {
        /// Constructs a new `Command` for launching the program at
        /// path `program`, with the following default configuration:
        ///
        /// * No arguments to the program
        /// * Inherit the current process's environment
        /// * Inherit the current process's working directory
        /// * Inherit stdin/stdout/stderr for `spawn` or `status`, but create pipes for `output`
        ///
        /// Builder methods are provided to change these defaults and
        /// otherwise configure the process.
        ///
        /// If `program` is not an absolute path, the `PATH` will be searched in
        /// an OS-defined way.
        ///
        /// The search path to be used may be controlled by setting the
        /// `PATH` environment variable on the Command,
        /// but this has some implementation limitations on Windows
        /// (see https://github.com/rust-lang/rust/issues/37519).
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("sh")
        ///         .spawn()
        ///         .expect("sh command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
            Command { inner: imp::Command::new(program.as_ref()) }
        }

        /// Add an argument to pass to the program.
        ///
        /// Only one argument can be passed per use. So instead of:
        ///
        /// ```ignore
        /// .arg("-C /path/to/repo")
        /// ```
        ///
        /// usage would be:
        ///
        /// ```ignore
        /// .arg("-C")
        /// .arg("/path/to/repo")
        /// ```
        ///
        /// To pass multiple arguments see [`args`].
        ///
        /// [`args`]: #method.args
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .arg("-l")
        ///         .arg("-a")
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
            self.inner.arg(arg.as_ref());
            self
        }

        /// Add multiple arguments to pass to the program.
        ///
        /// To pass a single argument see [`arg`].
        ///
        /// [`arg`]: #method.arg
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .args(&["-l", "-a"])
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn args<I, S>(&mut self, args: I) -> &mut Command
            where I: IntoIterator<Item=S>, S: AsRef<OsStr>
        {
            for arg in args {
                self.arg(arg.as_ref());
            }
            self
        }

        /// Inserts or updates an environment variable mapping.
        ///
        /// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
        /// and case-sensitive on all other platforms.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .env("PATH", "/bin")
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
            where K: AsRef<OsStr>, V: AsRef<OsStr>
        {
            self.inner.env(key.as_ref(), val.as_ref());
            self
        }

        /// Add or update multiple environment variable mappings.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// #![feature(command_envs)]
        ///
        /// use std::process::{Command, Stdio};
        /// use std::env;
        /// use std::collections::HashMap;
        ///
        /// let filtered_env : HashMap<String, String> =
        ///     env::vars().filter(|&(ref k, _)|
        ///         k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH"
        ///     ).collect();
        ///
        /// Command::new("printenv")
        ///         .stdin(Stdio::null())
        ///         .stdout(Stdio::inherit())
        ///         .env_clear()
        ///         .envs(&filtered_env)
        ///         .spawn()
        ///         .expect("printenv failed to start");
        /// ```
        #[unstable(feature = "command_envs", issue = "38526")]
        pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
            where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>
        {
            for (ref key, ref val) in vars {
                self.inner.env(key.as_ref(), val.as_ref());
            }
            self
        }

        /// Removes an environment variable mapping.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .env_remove("PATH")
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
            self.inner.env_remove(key.as_ref());
            self
        }

        /// Clears the entire environment map for the child process.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .env_clear()
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn env_clear(&mut self) -> &mut Command {
            self.inner.env_clear();
            self
        }

        /// Sets the working directory for the child process.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .current_dir("/bin")
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
            self.inner.cwd(dir.as_ref().as_ref());
            self
        }

        /// Configuration for the child process's stdin handle (file descriptor 0).
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::{Command, Stdio};
        ///
        /// Command::new("ls")
        ///         .stdin(Stdio::null())
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn stdin(&mut self, cfg: Stdio) -> &mut Command {
            self.inner.stdin(cfg.0);
            self
        }

        /// Configuration for the child process's stdout handle (file descriptor 1).
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::{Command, Stdio};
        ///
        /// Command::new("ls")
        ///         .stdout(Stdio::null())
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn stdout(&mut self, cfg: Stdio) -> &mut Command {
            self.inner.stdout(cfg.0);
            self
        }

        /// Configuration for the child process's stderr handle (file descriptor 2).
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::{Command, Stdio};
        ///
        /// Command::new("ls")
        ///         .stderr(Stdio::null())
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn stderr(&mut self, cfg: Stdio) -> &mut Command {
            self.inner.stderr(cfg.0);
            self
        }

        /// Executes the command as a child process, returning a handle to it.
        ///
        /// By default, stdin, stdout and stderr are inherited from the parent.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// Command::new("ls")
        ///         .spawn()
        ///         .expect("ls command failed to start");
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn spawn(&mut self) -> io::Result<Child> {
            self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner)
        }

        /// Executes the command as a child process, waiting for it to finish and
        /// collecting all of its output.
        ///
        /// By default, stdin, stdout and stderr are captured (and used to
        /// provide the resulting output).
        ///
        /// # Examples
        ///
        /// ```should_panic
        /// use std::process::Command;
        /// let output = Command::new("/bin/cat")
        ///                      .arg("file.txt")
        ///                      .output()
        ///                      .expect("failed to execute process");
        ///
        /// println!("status: {}", output.status);
        /// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
        /// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
        ///
        /// assert!(output.status.success());
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn output(&mut self) -> io::Result<Output> {
            self.inner.spawn(imp::Stdio::MakePipe, false).map(Child::from_inner)
                .and_then(|p| p.wait_with_output())
        }

        /// Executes a command as a child process, waiting for it to finish and
        /// collecting its exit status.
        ///
        /// By default, stdin, stdout and stderr are inherited from the parent.
        ///
        /// # Examples
        ///
        /// ```should_panic
        /// use std::process::Command;
        ///
        /// let status = Command::new("/bin/cat")
        ///                      .arg("file.txt")
        ///                      .status()
        ///                      .expect("failed to execute process");
        ///
        /// println!("process exited with: {}", status);
        ///
        /// assert!(status.success());
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn status(&mut self) -> io::Result<ExitStatus> {
            self.inner.spawn(imp::Stdio::Inherit, true).map(Child::from_inner)
                      .and_then(|mut p| p.wait())
        }
    }

    #[stable(feature = "rust1", since = "1.0.0")]
    impl fmt::Debug for Command {
        /// Format the program and arguments of a Command for display. Any
        /// non-utf8 data is lossily converted using the utf8 replacement
        /// character.
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            self.inner.fmt(f)
        }
    }

    impl AsInner<imp::Command> for Command {
        fn as_inner(&self) -> &imp::Command { &self.inner }
    }

    impl AsInnerMut<imp::Command> for Command {
        fn as_inner_mut(&mut self) -> &mut imp::Command { &mut self.inner }
    }

    /// The output of a finished process.
    #[derive(PartialEq, Eq, Clone)]
    #[stable(feature = "process", since = "1.0.0")]
    pub struct Output {
        /// The status (exit code) of the process.
        #[stable(feature = "process", since = "1.0.0")]
        pub status: ExitStatus,
        /// The data that the process wrote to stdout.
        #[stable(feature = "process", since = "1.0.0")]
        pub stdout: Vec<u8>,
        /// The data that the process wrote to stderr.
        #[stable(feature = "process", since = "1.0.0")]
        pub stderr: Vec<u8>,
    }

    // If either stderr or stdout are valid utf8 strings it prints the valid
    // strings, otherwise it prints the byte sequence instead
    #[stable(feature = "process_output_debug", since = "1.7.0")]
    impl fmt::Debug for Output {
        fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {

            let stdout_utf8 = str::from_utf8(&self.stdout);
            let stdout_debug: &fmt::Debug = match stdout_utf8 {
                Ok(ref str) => str,
                Err(_) => &self.stdout
            };

            let stderr_utf8 = str::from_utf8(&self.stderr);
            let stderr_debug: &fmt::Debug = match stderr_utf8 {
                Ok(ref str) => str,
                Err(_) => &self.stderr
            };

            fmt.debug_struct("Output")
                .field("status", &self.status)
                .field("stdout", stdout_debug)
                .field("stderr", stderr_debug)
                .finish()
        }
    }

    /// Describes what to do with a standard I/O stream for a child process.
    #[stable(feature = "process", since = "1.0.0")]
    pub struct Stdio(imp::Stdio);

    impl Stdio {
        /// A new pipe should be arranged to connect the parent and child processes.
        #[stable(feature = "process", since = "1.0.0")]
        pub fn piped() -> Stdio { Stdio(imp::Stdio::MakePipe) }

        /// The child inherits from the corresponding parent descriptor.
        #[stable(feature = "process", since = "1.0.0")]
        pub fn inherit() -> Stdio { Stdio(imp::Stdio::Inherit) }

        /// This stream will be ignored. This is the equivalent of attaching the
        /// stream to `/dev/null`
        #[stable(feature = "process", since = "1.0.0")]
        pub fn null() -> Stdio { Stdio(imp::Stdio::Null) }
    }

    impl FromInner<imp::Stdio> for Stdio {
        fn from_inner(inner: imp::Stdio) -> Stdio {
            Stdio(inner)
        }
    }

    #[stable(feature = "std_debug", since = "1.16.0")]
    impl fmt::Debug for Stdio {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            f.pad("Stdio { .. }")
        }
    }

    /// Describes the result of a process after it has terminated.
    #[derive(PartialEq, Eq, Clone, Copy, Debug)]
    #[stable(feature = "process", since = "1.0.0")]
    pub struct ExitStatus(imp::ExitStatus);

    impl ExitStatus {
        /// Was termination successful? Signal termination not considered a success,
        /// and success is defined as a zero exit status.
        ///
        /// # Examples
        ///
        /// ```rust,no_run
        /// use std::process::Command;
        ///
        /// let status = Command::new("mkdir")
        ///                      .arg("projects")
        ///                      .status()
        ///                      .expect("failed to execute mkdir");
        ///
        /// if status.success() {
        ///     println!("'projects/' directory created");
        /// } else {
        ///     println!("failed to create 'projects/' directory");
        /// }
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn success(&self) -> bool {
            self.0.success()
        }

        /// Returns the exit code of the process, if any.
        ///
        /// On Unix, this will return `None` if the process was terminated
        /// by a signal; `std::os::unix` provides an extension trait for
        /// extracting the signal and other details from the `ExitStatus`.
        #[stable(feature = "process", since = "1.0.0")]
        pub fn code(&self) -> Option<i32> {
            self.0.code()
        }
    }

    impl AsInner<imp::ExitStatus> for ExitStatus {
        fn as_inner(&self) -> &imp::ExitStatus { &self.0 }
    }

    impl FromInner<imp::ExitStatus> for ExitStatus {
        fn from_inner(s: imp::ExitStatus) -> ExitStatus {
            ExitStatus(s)
        }
    }

    #[stable(feature = "process", since = "1.0.0")]
    impl fmt::Display for ExitStatus {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            self.0.fmt(f)
        }
    }

    impl Child {
        /// Forces the child to exit. This is equivalent to sending a
        /// SIGKILL on unix platforms.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// let mut command = Command::new("yes");
        /// if let Ok(mut child) = command.spawn() {
        ///     child.kill().expect("command wasn't running");
        /// } else {
        ///     println!("yes command didn't start");
        /// }
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn kill(&mut self) -> io::Result<()> {
            self.handle.kill()
        }

        /// Returns the OS-assigned process identifier associated with this child.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// let mut command = Command::new("ls");
        /// if let Ok(child) = command.spawn() {
        ///     println!("Child's id is {}", child.id());
        /// } else {
        ///     println!("ls command didn't start");
        /// }
        /// ```
        #[stable(feature = "process_id", since = "1.3.0")]
        pub fn id(&self) -> u32 {
            self.handle.id()
        }

        /// Waits for the child to exit completely, returning the status that it
        /// exited with. This function will continue to have the same return value
        /// after it has been called at least once.
        ///
        /// The stdin handle to the child process, if any, will be closed
        /// before waiting. This helps avoid deadlock: it ensures that the
        /// child does not block waiting for input from the parent, while
        /// the parent waits for the child to exit.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// use std::process::Command;
        ///
        /// let mut command = Command::new("ls");
        /// if let Ok(mut child) = command.spawn() {
        ///     child.wait().expect("command wasn't running");
        ///     println!("Child has finished its execution!");
        /// } else {
        ///     println!("ls command didn't start");
        /// }
        /// ```
        #[stable(feature = "process", since = "1.0.0")]
        pub fn wait(&mut self) -> io::Result<ExitStatus> {
            drop(self.stdin.take());
            self.handle.wait().map(ExitStatus)
        }

        /// Attempts to collect the exit status of the child if it has already
        /// exited.
        ///
        /// This function will not block the calling thread and will only advisorily
        /// check to see if the child process has exited or not. If the child has
        /// exited then on Unix the process id is reaped. This function is
        /// guaranteed to repeatedly return a successful exit status so long as the
        /// child has already exited.
        ///
        /// If the child has exited, then `Ok(Some(status))` is returned. If the
        /// exit status is not available at this time then `Ok(None)` is returned.
        /// If an error occurs, then that error is returned.
        ///
        /// Note that unlike `wait`, this function will not attempt to drop stdin.
        ///
        /// # Examples
        ///
        /// Basic usage:
        ///
        /// ```no_run
        /// #![feature(process_try_wait)]
        ///
        /// use std::process::Command;
        ///
        /// let mut child = Command::new("ls").spawn().unwrap();
        ///
        /// match child.try_wait() {
        ///     Ok(Some(status)) => println!("exited with: {}", status),
        ///     Ok(None) => {
        ///         println!("status not ready yet, let's really wait");
        ///         let res = child.wait();
        ///         println!("result: {:?}", res);
        ///     }
        ///     Err(e) => println!("error attempting to wait: {}", e),
        /// }
        /// ```
        #[unstable(feature = "process_try_wait", issue = "38903")]
        pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
            Ok(self.handle.try_wait()?.map(ExitStatus))
        }

        /// Simultaneously waits for the child to exit and collect all remaining
        /// output on the stdout/stderr handles, returning an `Output`
        /// instance.
        ///
        /// The stdin handle to the child process, if any, will be closed
        /// before waiting. This helps avoid deadlock: it ensures that the
        /// child does not block waiting for input from the parent, while
        /// the parent waits for the child to exit.
        ///
        /// By default, stdin, stdout and stderr are inherited from the parent.
        /// In order to capture the output into this `Result<Output>` it is
        /// necessary to create new pipes between parent and child. Use
        /// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively.
        ///
        /// # Examples
        ///
        /// ```should_panic
        /// use std::process::{Command, Stdio};
        ///
        /// let child = Command::new("/bin/cat")
        ///     .arg("file.txt")
        ///     .stdout(Stdio::piped())
        ///     .spawn()
        ///     .expect("failed to execute child");
        ///
        /// let output = child
        ///     .wait_with_output()
        ///     .expect("failed to wait on child");
        ///
        /// assert!(output.status.success());
        /// ```
        ///
        #[stable(feature = "process", since = "1.0.0")]
        pub fn wait_with_output(mut self) -> io::Result<Output> {
            drop(self.stdin.take());

            let (mut stdout, mut stderr) = (Vec::new(), Vec::new());
            match (self.stdout.take(), self.stderr.take()) {
                (None, None) => {}
                (Some(mut out), None) => {
                    let res = out.read_to_end(&mut stdout);
                    res.unwrap();
                }
                (None, Some(mut err)) => {
                    let res = err.read_to_end(&mut stderr);
                    res.unwrap();
                }
                (Some(out), Some(err)) => {
                    let res = read2(out.inner, &mut stdout, err.inner, &mut stderr);
                    res.unwrap();
                }
            }

            let status = self.wait()?;
            Ok(Output {
                status: status,
                stdout: stdout,
                stderr: stderr,
            })
        }
    }

    /// Terminates the current process with the specified exit code.
    ///
    /// This function will never return and will immediately terminate the current
    /// process. The exit code is passed through to the underlying OS and will be
    /// available for consumption by another process.
    ///
    /// Note that because this function never returns, and that it terminates the
    /// process, no destructors on the current stack or any other thread's stack
    /// will be run. If a clean shutdown is needed it is recommended to only call
    /// this function at a known point where there are no more destructors left
    /// to run.
    ///
    /// ## Platform-specific behavior
    ///
    /// **Unix**: On Unix-like platforms, it is unlikely that all 32 bits of `exit`
    /// will be visible to a parent process inspecting the exit code. On most
    /// Unix-like platforms, only the eight least-significant bits are considered.
    ///
    /// # Examples
    ///
    /// Due to this function’s behavior regarding destructors, a conventional way
    /// to use the function is to extract the actual computation to another
    /// function and compute the exit code from its return value:
    ///
    /// ```
    /// use std::io::{self, Write};
    ///
    /// fn run_app() -> Result<(), ()> {
    ///     // Application logic here
    ///     Ok(())
    /// }
    ///
    /// fn main() {
    ///     ::std::process::exit(match run_app() {
    ///        Ok(_) => 0,
    ///        Err(err) => {
    ///            writeln!(io::stderr(), "error: {:?}", err).unwrap();
    ///            1
    ///        }
    ///     });
    /// }
    /// ```
    ///
    /// Due to [platform-specific behavior], the exit code for this example will be
    /// `0` on Linux, but `256` on Windows:
    ///
    /// ```no_run
    /// use std::process;
    ///
    /// process::exit(0x0f00);
    /// ```
    ///
    /// [platform-specific behavior]: #platform-specific-behavior
    #[stable(feature = "rust1", since = "1.0.0")]
    pub fn exit(code: i32) -> ! {
        ::sys_common::cleanup();
        ::sys::os::exit(code)
    }

    /// Terminates the process in an abnormal fashion.
    ///
    /// The function will never return and will immediately terminate the current
    /// process in a platform specific "abnormal" manner.
    ///
    /// Note that because this function never returns, and that it terminates the
    /// process, no destructors on the current stack or any other thread's stack
    /// will be run. If a clean shutdown is needed it is recommended to only call
    /// this function at a known point where there are no more destructors left
    /// to run.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::process;
    ///
    /// fn main() {
    ///     println!("aborting");
    ///
    ///     process::abort();
    ///
    ///     // execution never gets here
    /// }
    /// ```
    ///
    /// The [`abort`] function terminates the process, so the destructor will not
    /// get run on the example below:
    ///
    /// ```no_run
    /// use std::process;
    ///
    /// struct HasDrop;
    ///
    /// impl Drop for HasDrop {
    ///     fn drop(&mut self) {
    ///         println!("This will never be printed!");
    ///     }
    /// }
    ///
    /// fn main() {
    ///     let _x = HasDrop;
    ///     process::abort();
    ///     // the destructor implemented for HasDrop will never get run
    /// }
    /// ```
    #[stable(feature = "process_abort", since = "1.17.0")]
    pub fn abort() -> ! {
        unsafe { ::sys::abort_internal() };
    }

    #[cfg(all(test, not(target_os = "emscripten")))]
    mod tests {
        use io::prelude::*;

        use io::ErrorKind;
        use str;
        use super::{Command, Output, Stdio};

        // FIXME(#10380) these tests should not all be ignored on android.

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn smoke() {
            let p = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "exit 0"]).spawn()
            } else {
                Command::new("true").spawn()
            };
            assert!(p.is_ok());
            let mut p = p.unwrap();
            assert!(p.wait().unwrap().success());
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn smoke_failure() {
            match Command::new("if-this-is-a-binary-then-the-world-has-ended").spawn() {
                Ok(..) => panic!(),
                Err(..) => {}
            }
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn exit_reported_right() {
            let p = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "exit 1"]).spawn()
            } else {
                Command::new("false").spawn()
            };
            assert!(p.is_ok());
            let mut p = p.unwrap();
            assert!(p.wait().unwrap().code() == Some(1));
            drop(p.wait());
        }

        #[test]
        #[cfg(unix)]
        #[cfg_attr(target_os = "android", ignore)]
        fn signal_reported_right() {
            use os::unix::process::ExitStatusExt;

            let mut p = Command::new("/bin/sh")
                                .arg("-c").arg("read a")
                                .stdin(Stdio::piped())
                                .spawn().unwrap();
            p.kill().unwrap();
            match p.wait().unwrap().signal() {
                Some(9) => {},
                result => panic!("not terminated by signal 9 (instead, {:?})",
                                 result),
            }
        }

        pub fn run_output(mut cmd: Command) -> String {
            let p = cmd.spawn();
            assert!(p.is_ok());
            let mut p = p.unwrap();
            assert!(p.stdout.is_some());
            let mut ret = String::new();
            p.stdout.as_mut().unwrap().read_to_string(&mut ret).unwrap();
            assert!(p.wait().unwrap().success());
            return ret;
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn stdout_works() {
            if cfg!(target_os = "windows") {
                let mut cmd = Command::new("cmd");
                cmd.args(&["/C", "echo foobar"]).stdout(Stdio::piped());
                assert_eq!(run_output(cmd), "foobar\r\n");
            } else {
                let mut cmd = Command::new("echo");
                cmd.arg("foobar").stdout(Stdio::piped());
                assert_eq!(run_output(cmd), "foobar\n");
            }
        }

        #[test]
        #[cfg_attr(any(windows, target_os = "android"), ignore)]
        fn set_current_dir_works() {
            let mut cmd = Command::new("/bin/sh");
            cmd.arg("-c").arg("pwd")
               .current_dir("/")
               .stdout(Stdio::piped());
            assert_eq!(run_output(cmd), "/\n");
        }

        #[test]
        #[cfg_attr(any(windows, target_os = "android"), ignore)]
        fn stdin_works() {
            let mut p = Command::new("/bin/sh")
                                .arg("-c").arg("read line; echo $line")
                                .stdin(Stdio::piped())
                                .stdout(Stdio::piped())
                                .spawn().unwrap();
            p.stdin.as_mut().unwrap().write("foobar".as_bytes()).unwrap();
            drop(p.stdin.take());
            let mut out = String::new();
            p.stdout.as_mut().unwrap().read_to_string(&mut out).unwrap();
            assert!(p.wait().unwrap().success());
            assert_eq!(out, "foobar\n");
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        #[cfg(unix)]
        fn uid_works() {
            use os::unix::prelude::*;
            use libc;
            let mut p = Command::new("/bin/sh")
                                .arg("-c").arg("true")
                                .uid(unsafe { libc::getuid() })
                                .gid(unsafe { libc::getgid() })
                                .spawn().unwrap();
            assert!(p.wait().unwrap().success());
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        #[cfg(unix)]
        fn uid_to_root_fails() {
            use os::unix::prelude::*;
            use libc;

            // if we're already root, this isn't a valid test. Most of the bots run
            // as non-root though (android is an exception).
            if unsafe { libc::getuid() == 0 } { return }
            assert!(Command::new("/bin/ls").uid(0).gid(0).spawn().is_err());
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn test_process_status() {
            let mut status = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "exit 1"]).status().unwrap()
            } else {
                Command::new("false").status().unwrap()
            };
            assert!(status.code() == Some(1));

            status = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "exit 0"]).status().unwrap()
            } else {
                Command::new("true").status().unwrap()
            };
            assert!(status.success());
        }

        #[test]
        fn test_process_output_fail_to_start() {
            match Command::new("/no-binary-by-this-name-should-exist").output() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::NotFound),
                Ok(..) => panic!()
            }
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn test_process_output_output() {
            let Output {status, stdout, stderr}
                 = if cfg!(target_os = "windows") {
                     Command::new("cmd").args(&["/C", "echo hello"]).output().unwrap()
                 } else {
                     Command::new("echo").arg("hello").output().unwrap()
                 };
            let output_str = str::from_utf8(&stdout).unwrap();

            assert!(status.success());
            assert_eq!(output_str.trim().to_string(), "hello");
            assert_eq!(stderr, Vec::new());
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn test_process_output_error() {
            let Output {status, stdout, stderr}
                 = if cfg!(target_os = "windows") {
                     Command::new("cmd").args(&["/C", "mkdir ."]).output().unwrap()
                 } else {
                     Command::new("mkdir").arg(".").output().unwrap()
                 };

            assert!(status.code() == Some(1));
            assert_eq!(stdout, Vec::new());
            assert!(!stderr.is_empty());
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn test_finish_once() {
            let mut prog = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
            } else {
                Command::new("false").spawn().unwrap()
            };
            assert!(prog.wait().unwrap().code() == Some(1));
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn test_finish_twice() {
            let mut prog = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "exit 1"]).spawn().unwrap()
            } else {
                Command::new("false").spawn().unwrap()
            };
            assert!(prog.wait().unwrap().code() == Some(1));
            assert!(prog.wait().unwrap().code() == Some(1));
        }

        #[test]
        #[cfg_attr(target_os = "android", ignore)]
        fn test_wait_with_output_once() {
            let prog = if cfg!(target_os = "windows") {
                Command::new("cmd").args(&["/C", "echo hello"]).stdout(Stdio::piped()).spawn().unwrap()
            } else {
                Command::new("echo").arg("hello").stdout(Stdio::piped()).spawn().unwrap()
            };

            let Output {status, stdout, stderr} = prog.wait_with_output().unwrap();
            let output_str = str::from_utf8(&stdout).unwrap();

            assert!(status.success());
            assert_eq!(output_str.trim().to_string(), "hello");
            assert_eq!(stderr, Vec::new());
        }

        #[cfg(all(unix, not(target_os="android")))]
        pub fn env_cmd() -> Command {
            Command::new("env")
        }
        #[cfg(target_os="android")]
        pub fn env_cmd() -> Command {
            let mut cmd = Command::new("/system/bin/sh");
            cmd.arg("-c").arg("set");
            cmd
        }

        #[cfg(windows)]
        pub fn env_cmd() -> Command {
            let mut cmd = Command::new("cmd");
            cmd.arg("/c").arg("set");
            cmd
        }

        #[test]
        fn test_inherit_env() {
            use env;

            let result = env_cmd().output().unwrap();
            let output = String::from_utf8(result.stdout).unwrap();

            for (ref k, ref v) in env::vars() {
                // don't check android RANDOM variables
                if cfg!(target_os = "android") && *k == "RANDOM" {
                    continue
                }

                // Windows has hidden environment variables whose names start with
                // equals signs (`=`). Those do not show up in the output of the
                // `set` command.
                assert!((cfg!(windows) && k.starts_with("=")) ||
                        k.starts_with("DYLD") ||
                        output.contains(&format!("{}={}", *k, *v)) ||
                        output.contains(&format!("{}='{}'", *k, *v)),
                        "output doesn't contain `{}={}`\n{}",
                        k, v, output);
            }
        }

        #[test]
        fn test_override_env() {
            use env;

            // In some build environments (such as chrooted Nix builds), `env` can
            // only be found in the explicitly-provided PATH env variable, not in
            // default places such as /bin or /usr/bin. So we need to pass through
            // PATH to our sub-process.
            let mut cmd = env_cmd();
            cmd.env_clear().env("RUN_TEST_NEW_ENV", "123");
            if let Some(p) = env::var_os("PATH") {
                cmd.env("PATH", &p);
            }
            let result = cmd.output().unwrap();
            let output = String::from_utf8_lossy(&result.stdout).to_string();

            assert!(output.contains("RUN_TEST_NEW_ENV=123"),
                    "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
        }

        #[test]
        fn test_add_to_env() {
            let result = env_cmd().env("RUN_TEST_NEW_ENV", "123").output().unwrap();
            let output = String::from_utf8_lossy(&result.stdout).to_string();

            assert!(output.contains("RUN_TEST_NEW_ENV=123"),
                    "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output);
        }

        // Regression tests for #30858.
        #[test]
        fn test_interior_nul_in_progname_is_error() {
            match Command::new("has-some-\0\0s-inside").spawn() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
                Ok(_) => panic!(),
            }
        }

        #[test]
        fn test_interior_nul_in_arg_is_error() {
            match Command::new("echo").arg("has-some-\0\0s-inside").spawn() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
                Ok(_) => panic!(),
            }
        }

        #[test]
        fn test_interior_nul_in_args_is_error() {
            match Command::new("echo").args(&["has-some-\0\0s-inside"]).spawn() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
                Ok(_) => panic!(),
            }
        }

        #[test]
        fn test_interior_nul_in_current_dir_is_error() {
            match Command::new("echo").current_dir("has-some-\0\0s-inside").spawn() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
                Ok(_) => panic!(),
            }
        }

        // Regression tests for #30862.
        #[test]
        fn test_interior_nul_in_env_key_is_error() {
            match env_cmd().env("has-some-\0\0s-inside", "value").spawn() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
                Ok(_) => panic!(),
            }
        }

        #[test]
        fn test_interior_nul_in_env_value_is_error() {
            match env_cmd().env("key", "has-some-\0\0s-inside").spawn() {
                Err(e) => assert_eq!(e.kind(), ErrorKind::InvalidInput),
                Ok(_) => panic!(),
            }
        }

        /// Test that process creation flags work by debugging a process.
        /// Other creation flags make it hard or impossible to detect
        /// behavioral changes in the process.
        #[test]
        #[cfg(windows)]
        fn test_creation_flags() {
            use os::windows::process::CommandExt;
            use sys::c::{BOOL, DWORD, INFINITE};
            #[repr(C, packed)]
            struct DEBUG_EVENT {
                pub event_code: DWORD,
                pub process_id: DWORD,
                pub thread_id: DWORD,
                // This is a union in the real struct, but we don't
                // need this data for the purposes of this test.
                pub _junk: [u8; 164],
            }

            extern "system" {
                fn WaitForDebugEvent(lpDebugEvent: *mut DEBUG_EVENT, dwMilliseconds: DWORD) -> BOOL;
                fn ContinueDebugEvent(dwProcessId: DWORD, dwThreadId: DWORD,
                                      dwContinueStatus: DWORD) -> BOOL;
            }

            const DEBUG_PROCESS: DWORD = 1;
            const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
            const DBG_EXCEPTION_NOT_HANDLED: DWORD = 0x80010001;

            let mut child = Command::new("cmd")
                .creation_flags(DEBUG_PROCESS)
                .stdin(Stdio::piped()).spawn().unwrap();
            child.stdin.take().unwrap().write_all(b"exit\r\n").unwrap();
            let mut events = 0;
            let mut event = DEBUG_EVENT {
                event_code: 0,
                process_id: 0,
                thread_id: 0,
                _junk: [0; 164],
            };
            loop {
                if unsafe { WaitForDebugEvent(&mut event as *mut DEBUG_EVENT, INFINITE) } == 0 {
                    panic!("WaitForDebugEvent failed!");
                }
                events += 1;

                if event.event_code == EXIT_PROCESS_DEBUG_EVENT {
                    break;
                }

                if unsafe { ContinueDebugEvent(event.process_id,
                                               event.thread_id,
                                               DBG_EXCEPTION_NOT_HANDLED) } == 0 {
                    panic!("ContinueDebugEvent failed!");
                }
            }
            assert!(events > 0);
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)