aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@member.fsf.org>2015-12-23 21:27:19 +0100
committerMattias Andrée <maandree@member.fsf.org>2015-12-23 21:27:19 +0100
commitb841f545e0846f3a714107757f0a84f0b936231b (patch)
treee4488865463475dfb641a36442d72109104a8a15
parentfix possible overflow (diff)
downloadsat-b841f545e0846f3a714107757f0a84f0b936231b.tar.gz
sat-b841f545e0846f3a714107757f0a84f0b936231b.tar.bz2
sat-b841f545e0846f3a714107757f0a84f0b936231b.tar.xz
m + construct msg for daemon
Signed-off-by: Mattias Andrée <maandree@member.fsf.org>
-rw-r--r--src/sat.c75
1 files changed, 62 insertions, 13 deletions
diff --git a/src/sat.c b/src/sat.c
index 4860a4d..969722a 100644
--- a/src/sat.c
+++ b/src/sat.c
@@ -48,25 +48,63 @@ 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;