From 08b1ed1c518c927a6738f6fcf314172fba084c17 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sat, 28 Apr 2012 11:41:07 -0300 Subject: [PATCH] b2/boom.c: move opening of file as stdin to separate function --- b2/boom.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/b2/boom.c b/b2/boom.c index d3dde4a..b575ab5 100644 --- a/b2/boom.c +++ b/b2/boom.c @@ -18,6 +18,22 @@ #include "lang.h" +static void open_stdin(const char *name) +{ + int fd; + + fd = open(name, O_RDONLY); + if (fd < 0) { + perror(name); + exit(1); + } + if (dup2(fd, 0) < 0) { + perror("dup2"); + exit(1); + } +} + + static void usage(const char *name) { fprintf(stderr, "usage: %s [file]\n", name); @@ -27,21 +43,11 @@ static void usage(const char *name) int main(int argc, char **argv) { - int fd; - switch (argc) { case 1: break; case 2: - fd = open(argv[1], O_RDONLY); - if (fd < 0) { - perror(argv[1]); - exit(1); - } - if (dup2(fd, 0) < 0) { - perror("dup2"); - exit(1); - } + open_stdin(argv[1]); break; default: usage(*argv);