-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
62 lines (49 loc) · 1.31 KB
/
main.cpp
File metadata and controls
62 lines (49 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#define _POSIX_C_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libsh.h>
#include <libsh/cxx.hpp>
#include "mini.h"
using namespace std::literals;
static FILE *use(int fd, FILE *standard_stream, const char *mode)
{
if (fd != -1 && fd == fileno(standard_stream)){
return standard_stream;
}else
{
FILE *result = fdopen(fd, mode);
if (result == NULL){
fprintf(stderr, "mini: %d: cannot use file descriptor: %s\n", fd, strerror(errno));
exit (EXIT_FAILURE);
}
return result;
}
}
int main(int, char *argv[])
{
sh_init (argv[0]);
sh_arg_parse (&argv, "Usage: "s + sh_get_program () + " [OPTION]... IN-FD OUT-FD ERR-FD\n"s, "");
int in_fd = fileno(stdin);
int out_fd = fileno(stdout);
int err_fd = fileno(stderr);
if (argv[0] != NULL)
{
in_fd = sh_long2int (sh_xx_strtol (argv[0], 0));
++argv;
if (argv[0] != NULL)
{
out_fd = sh_long2int (sh_xx_strtol (argv[0], 0));
++argv;
if (argv[0] != NULL)
{
err_fd = sh_long2int (sh_xx_strtol (argv[0], 0));
++argv;
}
}
}
sh_arg_end (argv);
mini (use(in_fd, stdin, "r"), use(out_fd, stdout, "w"), use(err_fd, stderr, "w"));
exit (EXIT_SUCCESS);
}