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


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

Re: Another interesting program.

From wij <wyniijj5@gmail.com>
Newsgroups comp.lang.c++
Subject Re: Another interesting program.
Date 2024-11-29 14:52 +0800
Organization A noiseless patient Spider
Message-ID <cd35e0d7fe739f0184bbed4ffc7c328ea11807eb.camel@gmail.com> (permalink)
References <00f9c93d346ff53ea9144627a50d12e8bcd97c76.camel@gmail.com> <e82667d23848fd8b48dbe3270d4b635cb6c90c41.camel@gmail.com>

Show all headers | View raw


> 
> int main(int argc, const char* argv[])
> try {
>  Errno r;
>  FifoFile pipe_dc_rd, pipe_dc_wr;
>  FifoFile pipe_bc_rd, pipe_bc_wr;
>  ProcessID cpid;
> 
>  cout << "Simple dc calculator" WY_ENDL;
> 
>  if((r=mkpipe(pipe_dc_rd, pipe_bc_wr))!=Ok) {
>    WY_THROW(r);
>  }
>  if((r=mkpipe(pipe_bc_rd, pipe_dc_wr))!=Ok) {
>    WY_THROW(r);
>  }
>  if((r=fork(cpid))!=Ok) {
>    WY_THROW(r);
>  }
> 
>  if(cpid==0) {
>    pipe_bc_rd.reset();
>    pipe_bc_wr.reset();
>    process_dc(pipe_dc_rd,pipe_dc_wr);
>    WY_THROW( Errno() );
>  }
> 
>  pipe_dc_rd.reset();
>  pipe_dc_wr.reset();
>  process_bc(pipe_bc_rd,pipe_bc_wr);
>  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;
> };
> ---------------

This is routine codes, so made it a function (popen):

Errno popen(ProcessID& cpid, FifoFile& rdf, FifoFile& wrf, const char* fpath,
            char* const argv[], char* const envp[]) {
 Errno r;
 FifoFile pipe0_rd, pipe0_wr;
 FifoFile pipe1_rd, pipe1_wr;

 if((r=mkpipe(pipe1_rd, pipe0_wr))!=Ok) { // fd FD_CLOEXEC ?
   WY_RETURN(r);
 }
 if((r=mkpipe(pipe0_rd, pipe1_wr))!=Ok) {
   WY_RETURN(r);
 }
 if((r=fork(cpid))!=Ok) {
   WY_RETURN(r);
 }

 if(cpid==0) {
   ByteFlow nulf("/dev/null",O_WRONLY);
   pipe1_rd.reset();
   pipe1_wr.reset();
   if((r=cin.reset(pipe0_rd))!=Ok) {
     WY_THROW(r);  // go where ???
   }
   if((r=cout.reset(pipe0_wr))!=Ok) {
     WY_THROW(r);
   }
   if((r=cerr.reset(nulf))!=Ok) {
     WY_THROW(r);
   }
   pipe0_rd.reset();
   pipe0_wr.reset();

   if(envp==NULL) {
     if(::execvp(fpath,argv)==-1) {
       WY_THROW( Errno(errno) );
     }
   } else {
     if(::execve(fpath,argv,envp)==-1) {
       WY_THROW( Errno(errno) );
     }
   }

   WY__UNREACHABLE__;
 }

 pipe0_rd.reset();
 pipe0_wr.reset();
 if((r=rdf.reset(pipe1_rd))!=Ok) {
   WY_RETURN(r);
 }
 if((r=wrf.reset(pipe1_wr))!=Ok) {
   WY_RETURN(r);
 }
 pipe1_rd.reset();
 pipe1_wr.reset();

 return Ok;
};
----------

Q1. size of argv[]
   As I know, no way to know the number of elements in argv[]

Q2. A measure of a wrapper library: Is it still efficient for implementing
   popen or other wrapper functions in it own library?

   For the popen above:
   wrt compiled size: should be fine (real difference is minor).
   wrt speed: a number of protocol adjustment codes should be fine (mostly 
       are about error handling).
   wrt c++std library: I don't think it can implement popen using its own
       library facilities. This is generally true, particularly std::vector
       has nothing to do with malloc(3).

Q3. What the exit code of a program should be?
   This may be an example exit code is better errno (or compatible)

Back to comp.lang.c++ | Previous | Next — Previous in thread | 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