From b841f545e0846f3a714107757f0a84f0b936231b Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 23 Dec 2015 21:27:19 +0100 Subject: m + construct msg for daemon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/sat.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 13 deletions(-) (limited to 'src/sat.c') diff --git a/src/sat.c b/src/sat.c index 4860a4d..969722a 100644 --- a/src/sat.c +++ b/src/sat.c @@ -47,26 +47,64 @@ usage(void) } +/** + * Return the number of bytes required to store a string array. + * + * @param array The string array. + * @return The number of bytes required to store the array. + */ +static size_t +measure_array(char *array[]) +{ + size_t rc = 1; + for (; *array; array++) + rc += strlen(*array) + 1; + return rc * sizeof(char); +} + + +/** + * Store a string array. + * + * @param storage The buffer where the array is to be stored. + * @param array The array to store. + * @return Where in the buffer the array ends. + */ +static char * +store_array(char *restrict storage, char *array[]) +{ + for (; *array; array++) { + storage = stpcpy(storage, *array); + *storage++ = 0; + } + *storage++ = 0; + return storage; +} + + /** * Queue a job for later execution. * - * @param argc You guess! - * @param argv The first element should be the name of the process, - * the second argument should be the POSIX time (seconds - * since Epoch (1970-01-01 00:00:00 UTC), disregarding - * leap seconds) the job shall be executed. The rest of - * the arguments (being at least one) shoul be the - * command line arguments for the job the run. - * @return 0 The process was successful. - * @return 1 The process failed queuing the job. - * @return 2 User error, you do not know what you are doing. - * @return 3 satd(1) failed to queue the job. + * @param argc You guess! + * @param argv The first element should be the name of the process, + * the second argument should be the POSIX time (seconds + * since Epoch (1970-01-01 00:00:00 UTC), disregarding + * leap seconds) the job shall be executed. The rest of + * the arguments (being at least one) shoul be the + * command line arguments for the job the run. + * @param envp The environment. + * @return 0 The process was successful. + * @return 1 The process failed queuing the job. + * @return 2 User error, you do not know what you are doing. + * @return 3 satd(1) failed to queue the job. */ int -main(int argc, char *argv[]) +main(int argc, char *argv[], char *envp[]) { struct timespec ts; clockid_t clk; + char *message; + size_t message_size; if ((argc < 3) || (argv[1][0] == '-')) { usage(); @@ -74,7 +112,8 @@ main(int argc, char *argv[]) argv0 = argv[0]; - if (parse_time(str, &ts, &clk)) { + /* Parse the time argument. */ + if (parse_time(argv[1], &ts, &clk)) { switch (errno) { case EINVAL: fprintf(stderr, @@ -96,6 +135,16 @@ main(int argc, char *argv[]) } } + argc -= 2; + argv += 2; + + /* Construct message to send to the daemon. */ + message_size = measure_array(argv) + measure_array(envp); + message = malloc(message_size); + if (!message) + goto fail; + store_array(store_array(message, argv), envp); + fail: perror(argv0); return 1; -- cgit v1.2.3-70-g09d2