#include #include #include #include #include #include #include #include #include #include using namespace std; void print_help() { std::cout << "usage: sciotest -h|-v|-t TEST_ID\n"; std::cout << " -h, --help show this help text and exit\n"; std::cout << " -t, --test perform test with given TEST_ID\n"; std::cout << " currently a number between 1 and 5, inclusive\n"; std::cout << " -v, --version show version number and exit\n"; } void print_version() { std::cout << "sciotest version 1.0\n"; } void test1() { sc::io::fdostream chan {1}; chan << "Please enter a line of text:\n"; string line; sc::io::fdistream is {0}; getline(is, line); cout << line << '\n'; } void test2() { cout << "starting server\n"; sc::io::socketstream server(AF_INET6, SOCK_STREAM); sc::io::socket_address addr {"", 4242}; server.make_server(addr); for (;;) { sc::io::socketstream client = server.accept(); cerr << "connection from " << client.address() << '\n'; client << "ready to echo...\necho> "; string line; while (getline(client, line)) { cerr << "received: " << line << "\n"; if (line == "quit\r") { client << "bye\n"; client.close(); break; } else { client << line << '\n'; } client << "echo> "; } } } void test3() { string passwd {sc::file_get_contents("/etc/passwd")}; int fd = -1; int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; int mode = O_WRONLY | O_CREAT | O_TRUNC; throw_if_min1(fd = ::open("passwd", mode, perm)); sc::io::fdostream os {fd}; os << passwd; } void test4() { int fd = -1; throw_if_min1(fd = ::open("/etc/passwd", O_RDONLY)); sc::io::fdistream is {fd}; string line; while (getline(is, line)) { cout << line << '\n'; } } void test5() { int fd = -1; int perm = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; int mode = O_RDWR | O_CREAT | O_TRUNC; throw_if_min1(fd = ::open("test5", mode, perm)); sc::io::fdstream file {fd}; file << "A line of text\n"; file.seekg(0); string line; if (getline(file, line)) { cout << line << '\n'; } } int main(int argc, char * argv[]) { try { struct option long_options[] = { {"help", no_argument, nullptr, 'h'}, {"test", required_argument, nullptr, 't'}, {"version", no_argument, nullptr, 'v'}, {nullptr, 0, nullptr, 0} }; int opt_char, option_index; int test_id = 0; while ((opt_char = getopt_long(argc, argv, "ht:v", long_options, &option_index)) != -1) { switch (opt_char) { case 0: { // handle long-only options here std::string optname {long_options[option_index].name}; break; } case 'h': print_help(); return EXIT_SUCCESS; case 't': test_id = atoi(optarg); break; case 'v': print_version(); return EXIT_SUCCESS; case '?': throw std::runtime_error("unrecognized option"); } } if (optind == argc) { // here when no file args } for (int i = optind; i < argc; ++i) { try { // process file argv[i] } catch (const std::runtime_error& ex) { std::cerr << "sciotest: " << ex.what() << endl; } } if (test_id == 0) throw std::runtime_error("please provide a test id"); switch (test_id) { case 1: test1(); break; case 2: test2(); break; case 3: test3(); break; case 4: test4(); break; case 5: test5(); break; default: throw std::runtime_error("no such test"); } } catch (const exception& ex) { cerr << "sciotest: " << ex.what() << '\n'; return EXIT_FAILURE; } return EXIT_SUCCESS; }