Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #121022

Another interesting program.

From wij <wyniijj5@gmail.com>
Newsgroups comp.lang.c++
Subject Another interesting program.
Date 2024-11-26 13:25 +0800
Organization A noiseless patient Spider
Message-ID <00f9c93d346ff53ea9144627a50d12e8bcd97c76.camel@gmail.com> (permalink)

Show all headers | View raw


C++ was once? an advanced language for system programming. Now what? As usual, it
wants more, so the answer should be yes it still is.

I just rewrote an example program:

---- a_ptytest.cpp
/* Copyright is licensed by GNU LGPL, see file COPYING.       by I.J.Wang 2008

  Convert the example program ptytest.c in Linux Application Development
  [Michael K.Johnson, Erik W. Troan] p400. See the book for details.
  See also
  [Advanced Programming in the UNIX(r) Environment 2nd edition, W.R.S, S.A.R.]

  Type 'exit' (or Cntrl-C) in the new pty to exit program.

  Build: make a_ptytest
*/
#include <Wy.stdio.h>
#include <Wy.unistd.h>
#include <Wy.termios.h>
#include <Wy.select.h>
#include <Wy.signal.h>
#include <Wy.pty.h>

using namespace Wy;

void sigwinch_handler(int,::siginfo_t*,void*)
{
};

void t0() {
 Errno r;
 SigAct act(sigwinch_handler,Wy::SigSet(),SA_NOCLDWAIT);
 ByteFlow master;
 ProcessID cpid;
 String name;

 if((r=forkpty(master,cpid,name))!=Ok) {
   WY_THROW(r);
 }

 if(cpid==0) {
   ::execl("/bin/sh", "/bin/sh", NULL);
   ::exit(-1);
 }

 // Parent process
 FdSet rdset,errset;
 rdset.set(cin);
 rdset.set(master);
 errset.set(master);

for(;;) {
   FdSet rset(rdset),eset(errset);
   char buf[512];
   size_t n_rw;

   r=pselect(&rset,NULL,&eset,NULL,NULL);
   if((r!=Ok)&&(r!=EINTR)) {
     cerr << wrd(r) << WY_ENDL;
     break;
   }
   if(eset.contain(master)) {
     WY_THROW( Errno() );
   }
   if(rset.contain(master)) {
     if((r=master.read(buf,WY_CARRLEN(buf),n_rw))!=Ok) {
       // may cause EIO while reading controllong terminal
       if(r!=EIO) {
         WY_THROW(r);
       }
     }
     if(n_rw==0) {
       break;
     }
     if((r=cout.write(buf,n_rw,n_rw))!=Ok) {
       WY_THROW(r);
     }
   }

   if(rset.contain(cin)) {
     String str;
     cin >> str;
     master << str;
   };
 };

};

int main(int argc, const char* argv[])
try {
 t0();
 cout << "OK" WY_ENDL;
 return 0;
}
catch(const Errno& e) {
 cerr << wrd(e) << WY_ENDL;
 return -1;  // e.c_errno();
}
catch(...) {
 cerr << "main() caught(...)" WY_ENDL;
 throw;
};
--------------

[]$ g++ a_ptytest.cpp -lwy
[]$ ./a.out 
sh-5.2$ hello
hello
sh: hello: command not found
sh-5.2$ sudo ls /root
sudo ls /root
[sudo] password for wij: PasswdIsAccessible

anaconda-ks.cfg
sh-5.2$ exit
exit
exit
OK

What's the significance of C++std library? Probably just demo. of flexibility
and support of AT%T's old software. There is no free lunch.


Back to comp.lang.c++ | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Another interesting program. wij <wyniijj5@gmail.com> - 2024-11-26 13:25 +0800
  Re: Another interesting program. wij <wyniijj5@gmail.com> - 2024-11-27 13:24 +0800
    Re: Another interesting program. wij <wyniijj5@gmail.com> - 2024-11-29 14:52 +0800
      Re: Another interesting program. geodandw <geodandw@gmail.com> - 2024-12-03 22:38 -0500

csiph-web