From 9649cd9b3815cd217f53e98f8f7bcfc72ff69e3b Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Tue, 2 May 2006 13:32:27 +0200 Subject: [PATCH] [PATCH] vmsplice2: add switch for stack allocation --- vmsplice2.c | 48 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/vmsplice2.c b/vmsplice2.c index f4182ed..a905b83 100644 --- a/vmsplice2.c +++ b/vmsplice2.c @@ -13,6 +13,8 @@ #include "splice.h" +static int alloc_stack; + int do_vmsplice(int fd, struct iovec *iov, unsigned long nr_vecs) { struct pollfd pfd = { .fd = fd, .events = POLLOUT, }; @@ -53,10 +55,28 @@ int do_vmsplice(int fd, struct iovec *iov, unsigned long nr_vecs) static int usage(char *name) { - fprintf(stderr, "%s | ...\n", name); + fprintf(stderr, "%s [-s(tack)] | ...\n", name); return 1; } +static int parse_options(int argc, char *argv[]) +{ + int c, index = 1; + + while ((c = getopt(argc, argv, "s")) != -1) { + switch (c) { + case 's': + alloc_stack = 1; + index++; + break; + default: + return -1; + } + } + + return index; +} + #define S1 "header header header header header header header header" #define S2 "body body body body body body body body body body body" #define S3 "footer footer footer footer footer footer footer footer" @@ -64,24 +84,28 @@ static int usage(char *name) int main(int argc, char *argv[]) { struct iovec vecs[3]; -#if 0 - /* Dangerous on-stack usage! */ - char h[] = S1; - char b[] = S2; - char f[] = S3; + char *h, *b, *f; + + if (parse_options(argc, argv) < 0) + return usage(argv[0]); + + if (alloc_stack) { + /* Dangerous on-stack usage! */ + h = S1; + b = S2; + f = S3; + } else { + h = strdup(S1); + b = strdup(S2); + f = strdup(S3); + } vecs[0].iov_base = h; vecs[1].iov_base = b; vecs[2].iov_base = f; -#else - vecs[0].iov_base = strdup(S1); - vecs[1].iov_base = strdup(S2); - vecs[2].iov_base = strdup(S3); - vecs[0].iov_len = strlen(vecs[0].iov_base); vecs[1].iov_len = strlen(vecs[1].iov_base); vecs[2].iov_len = strlen(vecs[2].iov_base); -#endif if (check_output_pipe()) return usage(argv[0]); -- 2.25.1