
{"id":5901,"date":"2009-12-28T17:09:33","date_gmt":"2009-12-28T11:39:33","guid":{"rendered":"http:\/\/froisa.com\/?p=182"},"modified":"2009-12-28T17:09:33","modified_gmt":"2009-12-28T11:39:33","slug":"tee-2","status":"publish","type":"post","link":"https:\/\/www.trueangle.org\/index.php\/2009\/12\/28\/tee-2\/","title":{"rendered":"tee &#8211; read from standard input and write to standard output and files"},"content":{"rendered":"<h4><u> A UNIX Command <\/u><\/h4>\n<pre>\n$tee name\nmy name is nice.\nmy name is nice.\n$cat name\nmy name is nice.\n$\n\n<\/pre>\n<p><\/p>\n<h4><u> UNIX Explanation <\/u><\/h4>\n<pre>\nCopy standard  input to each  FILE, and also  to standard\noutput.  A  special variant of  the tee for the  shell is\ncalled script and  permits duplicating all input commands\nsubmitted to a shell into a file.\n<\/pre>\n<p><\/p>\n<h4><u> Related Source Code Exposition <\/u><\/h4>\n<p><code><br \/>\nstatic bool<br \/>\ntee_files (int nfiles, const char **files)<br \/>\n{<br \/>\n  FILE **descriptors;<br \/>\n  char buffer[BUFSIZ];<br \/>\n  ssize_t bytes_read;<br \/>\n  int i;<br \/>\n  bool ok = true;<br \/>\n  char const *mode_string =<br \/>\n    (O_BINARY<br \/>\n     ? (append ? \"ab\" : \"wb\")<br \/>\n     : (append ? \"a\" : \"w\"));<\/p>\n<p>  descriptors = xnmalloc (nfiles + 1, sizeof *descriptors);<\/p>\n<p>  \/* Move all the names `up' one in the argv array to make room for<br \/>\n     the entry for standard output.  This writes into argv[argc].  *\/<br \/>\n  for (i = nfiles; i &gt;= 1; i--)<br \/>\n    files[i] = files[i - 1];<\/p>\n<p>  if (O_BINARY &amp;&amp; ! isatty (STDIN_FILENO))<br \/>\n    xfreopen (NULL, \"rb\", stdin);<br \/>\n  if (O_BINARY &amp;&amp; ! isatty (STDOUT_FILENO))<br \/>\n    xfreopen (NULL, \"wb\", stdout);<\/p>\n<p>  \/* In the array of NFILES + 1 descriptors, make<br \/>\n     the first one correspond to standard output.   *\/<br \/>\n  descriptors[0] = stdout;<br \/>\n  files[0] = _(\"standard output\");<br \/>\n  setvbuf (stdout, NULL, _IONBF, 0);<\/p>\n<p>  for (i = 1; i &lt;= nfiles; i++)<br \/>\n    {<br \/>\n      descriptors[i] = (STREQ (files[i], &quot;-&quot;)<br \/>\n                        ? stdout<br \/>\n                        : fopen (files[i], mode_string));<br \/>\n      if (descriptors[i] == NULL)<br \/>\n        {<br \/>\n          error (0, errno, &quot;%s&quot;, files[i]);<br \/>\n          ok = false;<br \/>\n        }<br \/>\n      else<br \/>\n        setvbuf (descriptors[i], NULL, _IONBF, 0);<br \/>\n    }<\/p>\n<p>  while (1)<br \/>\n    {<br \/>\n      bytes_read = read (0, buffer, sizeof buffer);<br \/>\n#ifdef EINTR<br \/>\n      if (bytes_read &lt; 0 &amp;&amp; errno == EINTR)<br \/>\n        continue;<br \/>\n#endif<br \/>\n      if (bytes_read &lt;= 0)<br \/>\n        break;<\/p>\n<p>      \/* Write to all NFILES + 1 descriptors.<br \/>\n         Standard output is the first one.  *\/<br \/>\n      for (i = 0; i &lt;= nfiles; i++)<br \/>\n        if (descriptors[i]<br \/>\n            &amp;&amp; fwrite (buffer, bytes_read, 1, descriptors[i]) != 1)<br \/>\n          {<br \/>\n            error (0, errno, &quot;%s&quot;, files[i]);<br \/>\n            descriptors[i] = NULL;<br \/>\n            ok = false;<br \/>\n          }<br \/>\n    }<\/p>\n<p>  if (bytes_read == -1)<br \/>\n    {<br \/>\n      error (0, errno, _(&quot;read error&quot;));<br \/>\n      ok = false;<br \/>\n    }<\/p>\n<p>  \/* Close the files, but not standard output.  *\/<br \/>\n  for (i = 1; i &lt;= nfiles; i++)<br \/>\n    if (!STREQ (files[i], &quot;-&quot;)<br \/>\n        &amp;&amp; descriptors[i] &amp;&amp; fclose (descriptors[i]) != 0)<br \/>\n      {<br \/>\n        error (0, errno, &quot;%s&quot;, files[i]);<br \/>\n        ok = false;<br \/>\n      }<\/p>\n<p>  free (descriptors);<\/p>\n<p>  return ok;<br \/>\n}<br \/>\n<\/code><br \/>\n<\/p>\n<h4><u> Source Code Highlight<\/u><\/h4>\n<pre>\nCopy the standard input into  each of the NFILES files in\nFILES  and  into the  standard  output.   Return true  if\nsuccessful.\n\n<\/pre>\n<h4><u> Featured Image <\/u><\/h4>\n<p><a href=\"http:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/f3f5d-tee-may-9-2.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/f3f5d-tee-may-9-2.png\" alt=\"\" title=\"tee-may-9\" width=\"406\" height=\"226\" class=\"alignnone size-full wp-image-5176\" srcset=\"https:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/f3f5d-tee-may-9-2.png 406w, https:\/\/www.trueangle.org\/wp-content\/uploads\/2011\/05\/f3f5d-tee-may-9-2-300x167.png 300w\" sizes=\"(max-width: 406px) 100vw, 406px\" \/><\/a><br \/>\n<\/p>\n<h4><u> Related Knowledge <\/u><\/h4>\n<pre>\nThe  `tee'  command  copies  standard input  to  standard\noutput and also to any files given as arguments.  This is\nuseful when  you want not only  to send some  data down a\npipe, but also to save a copy.\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A UNIX Command $tee name my name is nice. my name is nice. $cat name my name is nice. $ UNIX Explanation Copy standard input to each FILE, and also to standard output. A special variant of the tee for the shell is called script and permits duplicating all input commands submitted to a shell &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.trueangle.org\/index.php\/2009\/12\/28\/tee-2\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;tee &#8211; read from standard input and write to standard output and files&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":-1,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[457,1554],"_links":{"self":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/5901"}],"collection":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/comments?post=5901"}],"version-history":[{"count":0,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/posts\/5901\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/media?parent=5901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/categories?post=5901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.trueangle.org\/index.php\/wp-json\/wp\/v2\/tags?post=5901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}