📄 uxtestfile.cpp
字号:
}static void time_test(void){ Timetm tm; Timestr ts; cout << "Epoch: " << tm << endl; cout << "Now: " << Timestr().ctime(); tm.tm_year = 98; cout << "1998: " << tm << endl; cout << "ts as constructed: " << ts << endl; cout << "bgn of time: " << Timestr().ctime(INT_MIN); cout << "end of time: " << Timestr().ctime(INT_MAX); tm.strptime("12-June-2003", "%d-%b-%Y"); cout << "My Birthday (wrong wday): " << tm << endl; cout << "My Birthday (right wday): " << Timetm(tm.mktime()) << endl; try { tm.getdate("12-June-2003"); cout << "My Birthday: " << tm << endl; } catch (const Error& e) { cout << "getdate failed, as expected: " << e << endl; } char sbuf[100]; tm.strftime(sbuf, sizeof(sbuf), "%x %X"); cout << "My Birthday (right wday, strftime): " << sbuf << endl;}static void sigset_test(void){ Sigset set; set.add(SIGINT); if (!set.ismember(SIGINT)) cout << "add error" << endl; set.del(SIGINT); if (set.ismember(SIGINT)) cout << "del error" << endl; set.fill(); if (!set.ismember(SIGINT) || !set.ismember(SIGUSR1)) cout << "fill error" << endl; cout << "+++ End of sigset_test +++" << endl;}static void at_exit_fcn(void){ cout << endl << "atexit fcn called" << endl;}static void dirstream_test(void){ DirStream d; int i; long loc; d.open_alloc("/aup"); for (i = 0; d.read(); i++) { cout << "dir entry " << i << ": " << d.get_name() << endl; if (i == 3) loc = d.tell(); } d.seek(loc); if (d.read()) cout << "dir entry 4 again: " << d.get_name() << endl; d.rewind(); if (d.read()) cout << "dir entry 1 again: " << d.get_name() << endl; d.close(); cout << "+++ End of dirstream_test +++" << endl;}static void process_test(void){ Process p; ExitStatus status; Process::atexit(at_exit_fcn); if ((p = Process::fork()) == 0) { try { Process::execlp("echo", "echo", "arg_one", "arg_two", "arg_three", NULL); } catch (const Error& e) { EC__EXIT(e) } } p.waitpid(&status); cout << "Process " << p << " exit status: " << status << endl; if ((p = Process::fork()) == 0) { char *av[] = {"sh", "-c", "set", NULL }; char *ev[] = {"SOMETHING=DOGCOW", NULL}; try { Process::execvpe("sh", av, ev); } catch (const Error& e) { EC__EXIT(e) } } p.waitpid(&status); cout << "Process " << p << " exit status: " << status << endl; if ((p = Process::fork()) == 0) { try { Process::pause(); } catch (const Error& e) { EC__EXIT(e) } } Clock::sleep(1); Clock::usleep(123456); p.kill(SIGFPE); p.waitpid(&status); cout << "Process " << p << " exit status: " << status << endl; Dir dcwd; dcwd.alloc(); Process::getcwd(dcwd); cout << dcwd << endl; Dir d("."); d.open(O_RDONLY); Process::chdir("/tmp"); Process::getcwd(dcwd); cout << dcwd << endl; Process::chdir(d.get_fd()); d.close(); Process::getcwd(dcwd); cout << dcwd << endl; dcwd.free(); struct rlimit rl; Process::getrlimit(RLIMIT_DATA, &rl); cout << "RLIMIT_DATA: cur = " << rl.rlim_cur << "; max = " << rl.rlim_max << endl; struct rusage usage; Process::getrusage(RUSAGE_SELF, &usage); cout << "Time: " << usage.ru_utime << endl; Process::nice(10); rl.rlim_cur = 0; Process::setrlimit(RLIMIT_CORE, &rl); try { Process::chroot("/tmp"); } catch (const Error& e) { cout << e << endl; } cout << "+++ End of process_test +++" << endl;}static void process_env_test(void){ try { cout << "HOME = " << getenv("HOME") << endl; Process::putenv("NEWVAR=newvalue"); cout << "NEWVAR = " << getenv("NEWVAR") << endl; Process::unsetenv("NEWVAR"); cout << "NEWVAR = " << getenv("NEWVAR") << endl; Process::setenv("NEWVAR", "another value", true); cout << "NEWVAR = " << getenv("NEWVAR") << endl; } catch (const exception& e) { cout << e.what() << endl; }/* catch (const Error& e) { cout << "process_env_test caught an error: " << e << endl; }*/ cout << "+++ End of process_env_test +++" << endl;}static void pipe_test(void){ File pf[2]; char buf[100]; File::pipe(pf); pf[1].write("Something", 10); pf[0].read(buf, sizeof(buf)); File(STDOUT_FILENO).write(buf, strlen(buf)); File(STDOUT_FILENO).write("\n", 1); cout << "+++ End of pipe_test +++" << endl;}#define PATH "/aup/aup2ex.tar"#define FREQ 8000static void aio_test(void){ File f(PATH); int count = 0, e; char buf1[512], buf2[512]; Aio cb; const struct aiocb *list[1] = { &cb }; cb.aio_fildes = STDIN_FILENO; cb.aio_buf = buf2; cb.aio_nbytes = sizeof(buf2); cb.aio_sigevent.sigev_notify = SIGEV_NONE; f.open(O_RDONLY); timestart(); while (f.read(buf1, sizeof(buf1)) > 0) { if (count % FREQ == 0) { if (count > 1) { Aio::suspend(list, 1); if ((e = cb.error()) != 0) throw Error(e); } cb.read(); } count++; } timestop("asynchronous"); printf("read %d blocks\n", count);}int main(void){ File file("tmp"), file2; ssize_t n; char buf[100]; struct stat sbuf; try { sigset_test(); } catch (const Error& e) { EC_EXIT(e) } try { process_test(); } catch (const Error& e) { EC_EXIT(e) } process_env_test(); try { aio_test(); } catch (const Error& e) { if (e == ENOSYS) cout << e << endl; else EC_EXIT(e) } try { file.open(O_RDWR | O_CREAT | O_TRUNC); if (file.write("Hello", 5) != 5) throw Error(EIO); file2 = file.dup(); if (file2.seek(0, SEEK_SET) != 0) throw Error(EIO); n = file2.read(buf, sizeof(buf) - 1); buf[n] = '\n'; buf[n + 1] = '\0'; File(STDOUT_FILENO).write(buf, n + 1); file2.stat(&sbuf); cout << "Size: " << sbuf.st_size << endl; file2.close(); file2 = file.dup2(10); file2.stat(&sbuf); cout << "Size: " << sbuf.st_size << endl; file.stat(&sbuf); cout << "Size: " << sbuf.st_size << endl; File("./tmp").stat(&sbuf); cout << "Size: " << sbuf.st_size << endl; pipe_test(); } catch (const Error& e) { EC_EXIT(e) } try { File myfile_in("myfifo"); File myfile_out = myfile_in; try { myfile_in.unlink(); } catch (...) { }; myfile_in.mkfifo(); myfile_in.open(O_RDONLY | O_NONBLOCK); myfile_out.open(O_WRONLY); myfile_out.fcntl(F_SETFL, myfile_out.fcntl(F_GETFL) & ~O_NONBLOCK); char *s = "Stuff to FIFO"; myfile_out.write(s, strlen(s) + 1); myfile_in.read(buf, sizeof(buf)); cout << "Read from FIFO: " << buf << endl; Dir d("barfdir"); cout << d << endl; EC_CATCH( d.mkdir() ) cout << "dir access = " << d.access(F_OK, false) << endl; EC_CATCH( d.rmdir() ) EC_CATCH( cout << "dir access = " << d.access(F_OK, false) << endl ) //maybe bool with 3 errnos collected into false? //symlink subclass of file } catch (const Error& e) { EC_CAUGHT(e) exit(EXIT_FAILURE); } try { //dirstream_test(); } catch (const Error& e) { EC_EXIT(e) } try { size_t n; char buf[200]; n = System::confstr(_CS_PATH, buf, sizeof(buf)); cout << "_CS_PATH: " << buf << " (" << n << ")" << endl; cout << "_SC_ARG_MAX: " << System::sysconf(_SC_ARG_MAX) << endl; struct utsname name; System::uname(&name); cout << "sysname: " << name.sysname << endl; cout << "nodename: " << name.nodename << endl; cout << "release: " << name.release << endl; cout << "version: " << name.version << endl; cout << "machine: " << name.machine << endl; } catch (const Error& e) { EC_EXIT(e) } try { time_test(); } catch (const Error& e) { cout << e << endl; EC_EXIT(e) } try { new_time_test(); } catch (const Error& e) { cout << e << endl; EC_EXIT(e) } try { socket_test_unix(); socket_test_inet(); sockip_test(); browser_test(); } catch (const Error& e) { cout << e << endl; EC_EXIT(e) } try { sysvmsg_test(); } catch (const Error& e) { EC_EXIT(e) } try { sysvshm_test(); } catch (const Error& e) { EC_EXIT(e) } try { sysvsem_test(); } catch (const Error& e) { EC_EXIT(e) } try { posixmsg_test(); } catch (const Error& e) { cout << e << endl; } try { posixsem_test(); } catch (const Error& e) { cout << e << endl; } try { posixshm_test(); } catch (const Error& e) { cout << e << endl; } try { terminal_test(); } catch (const Error& e) { cout << e << endl; EC_EXIT(e) } exit(EXIT_SUCCESS);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -