1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/test_files/programs/test_popenv.c Sun May 26 22:18:20 2024 +0200
1.3 @@ -0,0 +1,83 @@
1.4 +/*
1.5 + * Run a program using the supplied arguments.
1.6 + *
1.7 + * Copyright (C) 2024 Paul Boddie <paul@boddie.org.uk>
1.8 + *
1.9 + * This program is free software; you can redistribute it and/or
1.10 + * modify it under the terms of the GNU General Public License as
1.11 + * published by the Free Software Foundation; either version 2 of
1.12 + * the License, or (at your option) any later version.
1.13 + *
1.14 + * This program is distributed in the hope that it will be useful,
1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.17 + * GNU General Public License for more details.
1.18 + *
1.19 + * You should have received a copy of the GNU General Public License
1.20 + * along with this program; if not, write to the Free Software
1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.22 + * Boston, MA 02110-1301, USA
1.23 + */
1.24 +
1.25 +#include <stdio.h>
1.26 +#include <sys/types.h>
1.27 +#include <errno.h>
1.28 +
1.29 +/* NOTE: To be provided via a header. */
1.30 +
1.31 +extern pid_t popenv(int, const char *[], FILE **input, FILE **output, FILE **error);
1.32 +
1.33 +
1.34 +
1.35 +/* Transfer size for communication. */
1.36 +
1.37 +static const offset_t TO_TRANSFER = 1024;
1.38 +
1.39 +
1.40 +
1.41 +/* List objects in the filesystem image. */
1.42 +
1.43 +int main(int argc, char *argv[])
1.44 +{
1.45 + FILE *input, *output, *error;
1.46 + pid_t pid;
1.47 + char buffer[TO_TRANSFER];
1.48 + size_t nread;
1.49 +
1.50 + if (argc < 2)
1.51 + {
1.52 + printf("Usage: test_popenv <program> <argument>...\n");
1.53 + return 1;
1.54 + }
1.55 +
1.56 + printf("Running: %s\n", argv[1]);
1.57 +
1.58 + pid = popenv(argc - 1, (const char **) argv + 1, &input, &output, &error);
1.59 +
1.60 + if (pid == -1)
1.61 + {
1.62 + printf("Error: %d\n", errno);
1.63 + return 1;
1.64 + }
1.65 +
1.66 + printf("Process: %d\n", pid);
1.67 +
1.68 + /* Wait for events on the streams and the process.
1.69 + NOTE: Currently just reading from the process. */
1.70 +
1.71 + while ((nread = fread(buffer, sizeof(char), TO_TRANSFER, output)))
1.72 + {
1.73 + if (!fwrite(buffer, sizeof(char), nread, stdout))
1.74 + break;
1.75 + }
1.76 +
1.77 + fclose(input);
1.78 + fclose(output);
1.79 +
1.80 + /* NOTE: Error not yet used. */
1.81 +
1.82 + return 0;
1.83 +}
1.84 +
1.85 +/* vim: tabstop=4 expandtab shiftwidth=4
1.86 +*/