1 /* 2 * Run a program using the supplied arguments. 3 * 4 * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <stdio.h> 23 #include <sys/types.h> 24 #include <errno.h> 25 26 /* NOTE: To be provided via a header. */ 27 28 extern pid_t popenv(int, const char *[], FILE **input, FILE **output, FILE **error); 29 30 31 32 /* Transfer size for communication. */ 33 34 static const offset_t TO_TRANSFER = 1024; 35 36 37 38 /* List objects in the filesystem image. */ 39 40 int main(int argc, char *argv[]) 41 { 42 FILE *input, *output, *error; 43 pid_t pid; 44 char buffer[TO_TRANSFER]; 45 size_t nread; 46 47 if (argc < 2) 48 { 49 printf("Usage: test_popenv <program> <argument>...\n"); 50 return 1; 51 } 52 53 printf("Running: %s\n", argv[1]); 54 55 pid = popenv(argc - 1, (const char **) argv + 1, &input, &output, &error); 56 57 if (pid == -1) 58 { 59 printf("Error: %d\n", errno); 60 return 1; 61 } 62 63 printf("Process: %d\n", pid); 64 65 /* Wait for events on the streams and the process. 66 NOTE: Currently just reading from the process. */ 67 68 while ((nread = fread(buffer, sizeof(char), TO_TRANSFER, output))) 69 { 70 if (!fwrite(buffer, sizeof(char), nread, stdout)) 71 break; 72 } 73 74 fclose(input); 75 fclose(output); 76 77 /* NOTE: Error not yet used. */ 78 79 return 0; 80 } 81 82 /* vim: tabstop=4 expandtab shiftwidth=4 83 */