gitignore: Add some entries
[smailq.git] / sendmail.c
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6
7
8 static void usage(char* arg0) {
9 puts("A wrapper script for smailq that mimics sendmail.\n"
10 "USAGE:\n");
11 printf(" %s [OPTIONS] [recipient ...]\n", arg0);
12 printf(" %s -bp\n", arg0);
13 puts("\n"
14 "OPTIONS:\n"
15 " -bp\n"
16 " Calls `smailq --list`\n"
17 "\n"
18 " -h\n"
19 " Prints this help text\n"
20 "\n"
21 " [OPTIONS] [recipient ...]\n"
22 " Calls `smailq --send -- [OPTIONS] [recipient ...]`.\n"
23 " It passes all options as MSA options to smailq.\n");
24 }
25
26 int main(int argc, char* argv[]) {
27 if (argc == 1) {
28 fputs("No arguments given.\n", stderr);
29 usage(argv[0]);
30 return EINVAL;
31 }
32
33 if (strcmp(argv[1], "-h") == 0) {
34 usage(argv[0]);
35 return EXIT_SUCCESS;
36 }
37
38 if (strcmp(argv[1], "-bp") == 0) {
39 execlp("smailq", "smailq", "list", NULL);
40 perror("Error calling smailq --list");
41 return EXIT_FAILURE;
42 }
43
44 /* We have to add two more arguments: -- --send */
45 char** newargv = malloc((argc+2)*sizeof(char*));
46 if (newargv == NULL) {
47 perror(NULL);
48 return EXIT_FAILURE;
49 }
50
51 /* Fill in the arguments for smailq. Note that argv[argc] is NULL and
52 * newargv must be termined by a NULL pointer for execvp(). */
53 newargv[0] = "smailq";
54 newargv[1] = "--send";
55 newargv[2] = "--";
56 for (int i=1; i <= argc; ++i)
57 newargv[2+i] = argv[i];
58
59 execvp("smailq", newargv);
60 perror("Error calling smailq --list");
61 free(newargv);
62 return EXIT_FAILURE;
63 }