{"id":249,"date":"2015-07-21T16:13:54","date_gmt":"2015-07-21T20:13:54","guid":{"rendered":"http:\/\/72.167.111.237\/wpecsdump\/?page_id=249"},"modified":"2015-07-21T16:13:54","modified_gmt":"2015-07-21T20:13:54","slug":"notes-on-open-watcom-_popen","status":"publish","type":"page","link":"https:\/\/www.ecsdump.net\/?page_id=249","title":{"rendered":"Notes on Open Watcom _popen"},"content":{"rendered":"<h3>The synopsis is at the bottom of page and I am only concern with OS2 __386__.<\/h3>\n<p><strong>Create pipe using OW _pipe:<\/strong><\/p>\n<div class=\"indent\">_pipe( handles, 0, textOrBinary == &#8216;t&#8217; ? _O_TEXT : _O_BINARY)<\/div>\n<p><strong>Make handle non-inheritable if read or write:<\/strong><\/p>\n<div class=\"indent\">rc = DosQFHandState( (HFILE)<a href=\"http:\/\/www.mgreene.org\/wikka\/OsHandle\">_os_handle<\/a>(handles[<strong>0 read or 1 for write<\/strong>]), &amp;handleState );<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\"><em><strong>NOTE:<\/strong><\/em><br \/><em><strong>Search in API for DosQueryFHState instead of DosQFHandState<\/strong><\/em><br \/>16-bit\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosQFHandState\/edit\">DosQFHandState<\/a>\u00a032-bit\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosQueryFHState\/edit\">DosQueryFHState<\/a>\u00a0<br \/>They are both the same function, see bsedos.h.<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\">handleState |= OPEN_FLAGS_NOINHERIT;<br \/>handleState &amp;= 0x00007F88; \/* some bits must be zero *\/ 0111111110001000<\/p>\n<p>rc =\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosSetFHandState\/edit\">DosSetFHandState<\/a>( (HFILE)_os_handle(handles[0]), handleState );<\/p>\n<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\"><em><strong>NOTE:<\/strong><\/em><br \/><em><strong>Search in API for DosSetFHState instead of DosSetFHandState<\/strong><\/em><br \/>16-bit\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosSetFHandState\/edit\">DosSetFHandState<\/a>\u00a032-bit\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosSetFHState\/edit\">DosSetFHState<\/a>\u00a0<br \/>They are both the same function, see bsedos.h.<\/div>\n<p><strong>Create the pipe&#8217;s FILE:<\/strong><\/p>\n<div class=\"indent\">fp = __F_NAME(fdopen,_wfdopen)( handles[readOrWrite == &#8216;r&#8217; ? 0 : 1], mode );<br \/>_FP_PIPEDATA(fp).isPipe = 1;<br \/>_FP_PIPEDATA(fp).pid = -1;<br \/><em>\/lib_misc\/h\/rtdata.h: #define _FP_PIPEDATA(__<a href=\"http:\/\/ecsdump.net\/content\/oshandle#overlay-context=content\/ow-misc-links\">fp<\/a>) ((__<a href=\"http:\/\/ecsdump.net\/content\/oshandle#overlay-context=content\/ow-misc-links\">fp<\/a>)-&gt;_link-&gt;pipeInfo)<\/em><\/div>\n<p><strong>Spawn the process:<\/strong><\/p>\n<div class=\"indent\">if( connect_pipe( fp, command, handles, readOrWrite, textOrBinary ) )\u00a0<br \/>\u00a0<\/div>\n<div class=\"indent\"><strong>connect_pipe is static function:<\/strong><br \/><em>static int connect_pipe( FILE *<a href=\"http:\/\/ecsdump.net\/content\/oshandle#overlay-context=content\/ow-misc-links\">fp<\/a>, const CHAR_TYPE *command, int *handles,<\/em><br \/><em>int readOrWrite, int textOrBinary )<\/em><\/p>\n<p>If write &#8211;\u00a0<br \/>oldHandle = 0xFFFFFFFF; \/* duplicate standard input *\/<br \/>rc =\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosDupHandle\/edit\">DosDupHandle<\/a>( STDIN_HANDLE, &amp;oldHandle );<br \/>if( rc != NO_ERROR ) {<br \/>return( 0 );<br \/>}\u00a0<br \/>osHandle = STDIN_HANDLE; \/* use new standard input *\/<br \/>rc =\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosDupHandle\/edit\">DosDupHandle<\/a>( (HFILE)_os_handle(handles[0]), &amp;osHandle );<br \/>if( rc != NO_ERROR ) {<br \/><a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosClose\/edit\">DosClose<\/a>( oldHandle );<br \/>return( 0 );<br \/>}<\/p>\n<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\">if read &#8211;<br \/>oldHandle = 0xFFFFFFFF; \/* duplicate standard input *\/<br \/>rc =\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosDupHandle\/edit\">DosDupHandle<\/a>( STDOUT_HANDLE, &amp;oldHandle );<br \/>if( rc != NO_ERROR ) {<br \/>return( 0 );<br \/>}<br \/>osHandle = STDOUT_HANDLE; \/* use new standard input *\/<br \/>rc =\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosDupHandle\/edit\">DosDupHandle<\/a>( (HFILE)_os_handle(handles[1]), &amp;osHandle );<br \/>if( rc != NO_ERROR ) {<br \/><a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosClose\/edit\">DosClose<\/a>( oldHandle );<br \/>return( 0 );<br \/>}<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\">\/<strong>* Spawn the process and go home\u00a0<\/strong>*\/<br \/>if( spawn_it(\u00a0<a href=\"http:\/\/ecsdump.net\/content\/oshandle#overlay-context=content\/ow-misc-links\">fp<\/a>, command ) == 0 ) {<br \/>return( 0 );<br \/>}<br \/>spawn_it summary:<br \/>Sets up spawnvp (P_NOWAIT) for execution. Problem here is that uses CMD.EXE under NT and OS\/2, and COMMAND.COM under Win95.\u00a0<br \/>After executing spawnvp _FP_PIPEDATA(fp).pid to set pid.<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\">back to connect_pipe &#8211;&gt;\u00a0<br \/>osHandle = STDOUT_HANDLE;<br \/>rc =\u00a0<a class=\"missingpage\" title=\"Create this page\" href=\"http:\/\/www.mgreene.org\/wikka\/DosDupHandle\/edit\">DosDupHandle<\/a>( oldHandle, &amp;osHandle );<br \/>close( handles[1] ); \/* parent process should close this *\/<\/div>\n<div class=\"indent\">\u00a0<\/div>\n<div class=\"indent\">return from _popen<br \/>return(\u00a0<a href=\"http:\/\/ecsdump.net\/content\/oshandle#overlay-context=content\/ow-misc-links\">fp<\/a>\u00a0)<\/div>\n<p>&nbsp;<\/p>\n<hr \/>\n<p><strong>Synopsis:<\/strong>\u00a0<\/p>\n<p>#include &lt;stdio.h&gt;\u00a0<br \/>FILE *_popen( const char *command, const char *mode );\u00a0<br \/>FILE *_wpopen( const wchar_t *command, const wchar_t *mode );\u00a0<\/p>\n<p><strong>Description:<\/strong>\u00a0<br \/>The _popen function executes the command specified by command and creates a pipe between the calling process and the executed command.\u00a0<\/p>\n<p>Depending on the mode argument, the stream pointer returned may be used to read from or write to the pipe.\u00a0<\/p>\n<p>The executed command has an environment the same as its parents. The command will be started as follows: spawnl(&lt;shell_path&gt;, &lt;shell&gt;, &#8220;-c&#8221;, command, (char *)NULL);\u00a0<\/p>\n<p>where &lt;shell_path&gt; is an unspecified path for the shell utility and &lt;shell&gt; is one of &#8220;command.com&#8221; (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Disk_operating_system\" data-internallinksmanager029f6b8e52c=\"2\" title=\"DOS\" target=\"_blank\" rel=\"noopener\">DOS<\/a>, Windows 95) or &#8220;cmd.exe&#8221; (Windows NT\/2000, OS\/2).\u00a0<\/p>\n<p>The mode argument to _popen is a string that specifies an I\/O mode for the pipe.\u00a0<\/p>\n<p>Mode Meaning\u00a0<\/p>\n<p>&#8220;r&#8221;\u00a0<br \/>The calling process will read from the standard output of the child process using the stream pointer returned by _popen.\u00a0<\/p>\n<p>&#8220;w&#8221;\u00a0<br \/>The calling process will write to the standard input of the child process using the stream pointer returned by _popen.\u00a0<br \/>The letter &#8220;t&#8221; may be added to any of the above modes to indicate that the file is (or must be) a text file (i.e., CR\/LF pairs are converted to newline characters).\u00a0<\/p>\n<p>The letter &#8220;b&#8221; may be added to any of the above modes to indicate that the file is (or must be) a binary file (an ANSI requirement for portability to systems that make a distinction between text and binary files).\u00a0<\/p>\n<p>When default file translation is specified (i.e., no &#8220;t&#8221; or &#8220;b&#8221; is specified), the value of the global variable _fmode establishes whether the file is to treated as a binary or a text file. Unless this value is changed by the program, the default will be text mode.\u00a0<\/p>\n<p>A stream opened by _popen should be closed by the pclose function.\u00a0<\/p>\n<p><strong>Returns:<\/strong>\u00a0<br \/>The _popen function returns a non-NULL stream pointer upon successful completion. If _popen is unable to create either the pipe or the subprocess, a NULL stream pointer is returned and errno is set appropriately.\u00a0<\/p>\n<p><strong>Errors:<\/strong>\u00a0<br \/>When an error has occurred, errno contains a value indicating the type of error that has been detected.\u00a0<\/p>\n<p>Constant\u00a0<br \/>Meaning\u00a0<\/p>\n<p>EINVAL\u00a0<br \/>The mode argument is invalid.\u00a0<br \/>_popen may also set errno values as described by the _pipe and spawnl functions.\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The synopsis is at the bottom of page and I am only concern with OS2 __386__. Create pipe using OW _pipe: _pipe( handles, 0, textOrBinary == &#8216;t&#8217; ? _O_TEXT : _O_BINARY) Make handle non-inheritable if read or write: rc = DosQFHandState( (HFILE)_os_handle(handles[0 read or 1 for write]), &amp;handleState ); \u00a0 NOTE:Search in API for DosQueryFHState&hellip;<\/p>\n<p><a class=\"more-link\" href=\"https:\/\/www.ecsdump.net\/?page_id=249\" title=\"Continue reading &lsquo;Notes on Open Watcom _popen&rsquo;\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":243,"menu_order":3,"comment_status":"open","ping_status":"open","template":"page-templates\/info_static.php","meta":{"footnotes":""},"categories":[],"tags":[],"wf_page_folders":[79],"class_list":["post-249","page","type-page","status-publish","hentry"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=\/wp\/v2\/pages\/249","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=249"}],"version-history":[{"count":0,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=\/wp\/v2\/pages\/249\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=\/wp\/v2\/pages\/243"}],"wp:attachment":[{"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=249"},{"taxonomy":"wf_page_folders","embeddable":true,"href":"https:\/\/www.ecsdump.net\/index.php?rest_route=%2Fwp%2Fv2%2Fwf_page_folders&post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}