/*** vidread.c */
/** Read from fd 0 (stdin) until one short read, copy to stdout */

/*** Ivan Shmakov, 2026 */

/** To the extent possible under law, the author(s) have dedicated
 ** all copyright and related and neighboring rights to this software
 ** to the public domain worldwide.  This software is distributed
 ** without any warranty.
 **
 ** You should have received a copy of the CC0 Public Domain Dedication
 ** along with this software.  If not, see
 ** <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

/*** Commentary: */
/**
 ** Reading video(4) device is stated to result in a short read
 ** once the frame is fully read.
 **
 ** Hence, use a command like:
 **
 **   $ vidread < /dev/video2 > camera.jpeg 
 **/

/*** History: */

/** 0.2 2026-07-08
 **     Add CC0 notice and this history.
 **
 ** 0.1 2026-07-04 11:31:44Z
 **     (sfn.eqR6qQ32k0VnK3D3dSrFoKX8PEwXr5oXimM3_mld7CY.c)
 **     Initial revision.
 */

/*** Code: */
#include <stdio.h>
#include <unistd.h>

#ifndef BUFZ
#define BUFZ (0x4000)
#endif

#define ELTS(a) (sizeof (a) / sizeof (*(a)))

int
main ()
{
  char buf[BUFZ];

  ssize_t r;
  do {
    r = read (0, buf, sizeof (buf));
    if (r < 0) {
      perror ("error reading stdin");
      /* . */
      return 2;
    }

    {
      size_t w = fwrite (buf, 1, r, stdout);
      if (w != r) {
        perror ("error writing stdout");
        /* . */
        return 3;
      }
    }
  } while (r == sizeof (buf));

  /* . */
  return 0;
}

/*** vidread.c ends here */
