⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uxtestfile.cpp

📁 Advanced UNIX Programming is the long-awaited (19 years!) update to the 1985 original. Maybe "update
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*	Copyright 2003 by Marc J. Rochkind. All rights reserved.	May be copied only for purposes and under conditions described	on the Web page www.basepath.com/aup/copyright.htm.	The Example Files are provided "as is," without any warranty;	without even the implied warranty of merchantability or fitness	for a particular purpose. The author and his publisher are not	responsible for any damages, direct or incidental, resulting	from the use or non-use of these Example Files.	The Example Files may contain defects, and some contain deliberate	coding mistakes that were included for educational reasons.	You are responsible for determining if and how the Example Files	are to be used.*/#include "ux.hpp"using namespace Ux;static void terminal_test(void){	Terminal trm(STDIN_FILENO);	char buf[1000];	cout << endl << "terminal test" << endl;	cout << "isatty: " << trm.isatty() << endl;	trm.ttyname(buf, sizeof(buf));	cout << "ttyname: " << buf << endl;	Termios tios;	trm.tcgetattr(tios);	cout << "i speed: " << tios.cfgetispeed() << endl;	cout << "o speed: " << tios.cfgetospeed() << endl;}static void posixshm_test(void){	const char *shm_path = "/posix_shm";	PosixShm m;	cout << endl << "POSIX shared memory test" << endl;		try {		m.unlink(shm_path);	}	catch (Error& e) {		if (e == ENOSYS) {			cout << "*** POSIX shared memory not supported." << endl;			return;		}		// otherwise ignore the error	}	cout << "Getting ready to open..." << endl;	EC_CATCH( m.open(shm_path, O_RDWR | O_CREAT) )	cout << "... opened" << endl;	m.truncate(10000);	char *p = (char *)m.mmap(10000);	strcpy(p, "This is data for POSIX shared memory.");	m.munmap(p, 10000);	p = (char *)m.mmap(10000);	cout << "Got: \"" << p << "\"" << endl;		m.unlink(shm_path);}static void posixmsg_test(void){	const char *msg_path = "/posix_mq2";	PosixMsg m;	const char *s = "A POSIX message.";	char buf[2000];	ssize_t n;	struct mq_attr attr;	cout << endl << "POSIX message test" << endl;		try {		m.unlink(msg_path);	}	catch (Error& e) {		if (e == ENOSYS) {			cout << "*** POSIX messages not supported." << endl;			return;		}		// otherwise ignore the error	}	cout << "Getting ready to open..." << endl;	EC_CATCH( m.open(msg_path, O_RDWR | O_CREAT) )	cout << "... opened" << endl;	EC_CATCH( m.getattr(&attr) )	cout << "maxmsg: " << attr.mq_maxmsg << "; msgsize: " << attr.mq_msgsize << endl;	EC_CATCH( m.send(s, strlen(s)) )	EC_CATCH( n = m.receive(buf, sizeof(buf) - 1) )	buf[n] = '\0';	cout << "Got: \"" << buf << "\"" << endl;	EC_CATCH( m.close() )	EC_CATCH( m.unlink(msg_path) )	cout << "POSIX message text finished" << endl;}static void handler(int signum){	cout << "Got signal " << signum << endl;}static void sysvsem_test(void){	const char *sem_path = "sysv_sem";	union semun arg;	struct sembuf sbuf;	cout << endl << "SysV semaphore test" << endl;	File f(sem_path);	EC_CATCH( f.open(O_WRONLY | O_CREAT) )	f.close();	key_t k = SysVIPC::ftok(sem_path, 1);	SysVSem s;	EC_CATCH( s.get(k, 1) )	arg.val = 0;	EC_CATCH( s.ctl(0, SETVAL, arg) )	sbuf.sem_num = 0;	sbuf.sem_op = 1;	sbuf.sem_flg = 0;	EC_CATCH( s.op(&sbuf, 1) )	struct sigaction act;	memset(&act, 0, sizeof(act));	act.sa_handler = handler;	Process::sigaction(SIGALRM, &act);	Timer::alarm(2);	sbuf.sem_op = 0;	try {		s.op(&sbuf, 1);	}	catch (const Error& e) {		if (e == EINTR)			cout << "op interrupted, as expected" << endl;		else			throw;	}	EC_CATCH( s.ctl(0, IPC_RMID, arg) )	cout << "SysV semaphore test finished" << endl;}static void posixsem_test(void){	const char *sem_path = "/posix_sem";	cout << endl << "Posix semaphore test" << endl;	PosixSem s;	try {		s.open(sem_path, O_RDWR | O_CREAT);	}	catch (Error& e) {		if (e == ENOSYS) {			cout << "*** POSIX semaphores not supported." << endl;			return;		}		else			EC_EXIT(e)	}	struct sigaction act;	memset(&act, 0, sizeof(act));	act.sa_handler = handler;	Process::sigaction(SIGALRM, &act);	Timer::alarm(2);	try {		s.wait();	}	catch (const Error& e) {		if (e == EINTR)			cout << "wait interrupted, as expected" << endl;		else			throw;	}	EC_CATCH( s.post() )	EC_CATCH( s.wait() ) // should not block	EC_CATCH( s.close() )	EC_CATCH( s.unlink(sem_path) )	cout << "POSIX semaphore test finished" << endl;}static void sysvshm_test(void){	const char *shm_path = "sysv_shm";	cout << endl << "SysV shared memory test" << endl;	File f(shm_path);	EC_CATCH( f.open(O_WRONLY | O_CREAT) )	f.close();	key_t k = SysVIPC::ftok(shm_path, 1);	SysVShm m;	EC_CATCH( m.get(k, 1000) )	char *p = (char *)m.at();	strcpy(p, "Some data for the shared memory segment.");	m.dt(p);	p = (char *)m.at();	cout << "Got: \"" << p << "\"" << endl;		m.ctl(IPC_RMID);}static void sysvmsg_test(void){	const char *msgq_path = "sysv_msgq";	struct {		long mtype;		char mtext[1000];	} msg = { 1 }, msg2;	cout << endl << "SysV message test" << endl;	File f(msgq_path);	EC_CATCH( f.open(O_WRONLY | O_CREAT) )	f.close();	key_t k = SysVIPC::ftok(msgq_path, 1);	cout << "key: " << k << endl;	SysVMsg q;	EC_CATCH( q.get(k) )	strcpy(msg.mtext, "SysV message");	EC_CATCH( q.snd(&msg, strlen(msg.mtext)) )	ssize_t n;	EC_CATCH( n = q.rcv(&msg2, sizeof(msg2.mtext) - 1) )	msg2.mtext[n] = '\0';	cout << "Got: \"" << msg2.mtext << "\"" << endl;	q.ctl(IPC_RMID);}static void socket_test_unix(void){	Socket s("MySocket");	Process p;	File pfd[2];		try {		s.unlink();	}	catch (...) {	};	SockAddrUn sa(s);	File::pipe(pfd);	if ((p = Process::fork()) == 0) {		char c;		pfd[1].close();		pfd[0].read(&c, 1);		pfd[0].close();		EC_CATCH( s.socket() )		EC_CATCH( s.connect(sa) )		cout << "Child connected!" << endl;		EC_CATCH( s.write("Hello", 6) )		s.close();		Process::exit();	}	else {		Socket client;		char msg[100];			EC_CATCH( s.socket() )		EC_CATCH( s.bind(sa) )		EC_CATCH( s.listen() )		pfd[0].close();		pfd[1].close();		EC_CATCH( client = s.accept() )		cout << "Parent accepted!" << endl;		EC_CATCH( client.read(msg, sizeof(msg)) )		client.close();		s.close();		cout << "Parent got message: " << msg << endl;	}}static void socket_test_inet(void){	Socket s;	Process p;	File pfd[2];cout << 1 << endl;#if defined(SOLARIS)	SockAddrIn sa(1001, "192.168.0.10"); // Sol#elif defined(LINUX)	SockAddrIn sa(1001, "192.168.0.19"); // Suse#endifcout << 2 << endl;	File::pipe(pfd);	if ((p = Process::fork()) == 0) {		char c;		pfd[1].close();		pfd[0].read(&c, 1);		pfd[0].close();		EC_CATCH( s.socket(AF_INET) )		EC_CATCH( s.connect(sa) )		cout << "Child connected!" << endl;		EC_CATCH( s.write("Hello", 6) )		s.close();		Process::exit();	}	else {		Socket client;		char msg[100];			EC_CATCH( s.socket(AF_INET) )		EC_CATCH( s.bind(sa) )		EC_CATCH( s.listen() )		pfd[0].close();		pfd[1].close();		EC_CATCH( client = s.accept() )		cout << "Parent accepted!" << endl;		EC_CATCH( client.read(msg, sizeof(msg)) )		client.close();		s.close();		cout << "Parent got message: " << msg << endl;	}}static void sockip_test(void){	char buf[100];	SockIPv4 si4("64.81.102.0");	cout << "As IPv4: " << si4.get_ipv4() << endl;	cout << "As dotted: " << si4.get_string(buf, sizeof(buf)) << endl;	SockIPv6 si6("FEDC:BA98:7654:3210:FEDC:BA98:7654:3210");	cout << "As IPv6:";	for (int i = 0; i < 16; i++)		cout << " " << (int)si6.get_ipv6()[i];	cout << endl;	cout << "As dotted: " << si6.get_string(buf, sizeof(buf)) << endl;}static void browser_test(void){	SockAddr sa;	Socket s;	char buf[512];	const char *request = "GET / HTTP/1.0\n\n";	sa.set_server("www.basepath.com");	s.socket(AF_INET, SOCK_STREAM, sa.get_protocol());	s.connect(sa);	s.write(request, strlen(request));	ssize_t n = s.read(buf, sizeof(buf) - 1);	buf[n] = '\0';	cout << endl << endl << buf << endl<< endl;}static void new_time_test(void){	TimeParts tm;	TimeString ts;	TimeSec tnow(now);	TimeString snow(tnow);	TimeString snow2((time_t)TimeSec());	TimeString snow3((time_t)TimeSec(now));	TimeMsec tnowmsec(now), tepochmsec(epoch);	TimeNsec tnownsec(now), tepochnsec(epoch);	cout << endl << "new_time_test" << endl;	cout << "now: " << snow;	cout << "now: " << snow3;	cout << "epoch (TimeString): " << snow2;	cout << "epoch (TimeParts): " << tm << endl;;	tm.set_fmt("12-June-2003", "%d-%b-%Y");	cout << "My Birthday (wrong wday): " << tm << endl;	cout << "My Birthday (right wday): " << TimeParts(tm.get_secs()) << endl;	char sbuf[100];	tm.get_fmt(sbuf, sizeof(sbuf), "%x %X");	cout << "My Birthday (right wday, strftime): " << sbuf << endl;	try {		tm.set("12-June-2003");		cout << "My Birthday: " << tm << endl;	}	catch (const Error& e) {		cout << "set(const char *) failed, as expected: " << e << endl;	}	cout << "ts as constructed: " << ts << endl;	ts.set(INT_MIN);	cout << "bgn of time: " << ts;	ts.set(INT_MAX);	cout << "end of time: " << ts;	cout << "now in msecs: " << tnowmsec.get_string(sbuf, sizeof(sbuf)) << endl;	cout << "now in msecs: " << tnowmsec << endl;	cout << "epoch in msecs: " << tepochmsec << endl;	cout << "now in nsecs: " << tnownsec.get_string(sbuf, sizeof(sbuf)) << endl;	cout << "now in nsecs: " << tnownsec << endl;	cout << "epoch in nsecs: " << tepochnsec << endl;#if 0	cout << "Epoch: " << tm << endl;	cout << "Now: " << Timestr().ctime();	tm.tm_year = 98;	cout << "1998: " << tm << endl;#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -