DrKLO/Telegram · error

%s: can't open stdin\n

Error message

%s: can't open stdin\n

What it means

When no input file name is given, wrjpgcom defaults to stdin and, if USE_FDOPEN is compiled in, re-opens stdin in binary mode via fdopen(fileno(stdin), READ_BINARY). If fdopen returns NULL the tool prints `%s: can't open stdin` and exits EXIT_FAILURE. This is a low-level stdio re-attach failure, not a missing-file error.

Source

Thrown at TMessagesProj/jni/mozjpeg/wrjpgcom.c:505

   * from stdin; in this case there MUST be an input JPEG file name.
   */
  if (comment_arg == NULL && comment_file == NULL && argn >= argc)
    usage();

  /* Open the input file. */
  if (argn < argc) {
    if ((infile = fopen(argv[argn], READ_BINARY)) == NULL) {
      fprintf(stderr, "%s: can't open %s\n", progname, argv[argn]);
      exit(EXIT_FAILURE);
    }
  } else {
    /* default input file is stdin */
#ifdef USE_SETMODE              /* need to hack file mode? */
    setmode(fileno(stdin), O_BINARY);
#endif
#ifdef USE_FDOPEN               /* need to re-open in binary mode? */
    if ((infile = fdopen(fileno(stdin), READ_BINARY)) == NULL) {
      fprintf(stderr, "%s: can't open stdin\n", progname);
      exit(EXIT_FAILURE);
    }
#else
    infile = stdin;
#endif
  }

  /* Open the output file. */
#ifdef TWO_FILE_COMMANDLINE
  /* Must have explicit output file name */
  if (argn != argc - 2) {
    fprintf(stderr, "%s: must name one input and one output file\n", progname);
    usage();
  }
  if ((outfile = fopen(argv[argn + 1], WRITE_BINARY)) == NULL) {
    fprintf(stderr, "%s: can't open %s\n", progname, argv[argn + 1]);
    exit(EXIT_FAILURE);
  }

View on GitHub (pinned to 45ab8f4308)

Solutions

  1. Provide an explicit input JPEG file name so stdin is not used.
  2. Ensure fd 0 is a valid open descriptor before launching the tool.
  3. Redirect stdin from a real file: `wrjpgcom ... < input.jpg`.

Example fix

// before
wrjpgcom -comment hi   # stdin closed in calling process
// after
wrjpgcom -comment hi input.jpg
Defensive patterns

Strategy: validation

Validate before calling

#include <unistd.h>
/* ensure stdin (fd 0) is valid before relying on default input */
if (isatty(0) == 0 && fcntl(0, F_GETFD) == -1) {
    /* stdin not usable; require an explicit input file */
}

Prevention

When it happens

Trigger: Running wrjpgcom with stdin redirected but the descriptor unavailable/closed (e.g. stdin closed before exec, or fd 0 invalid), under a build that defines USE_FDOPEN.

Common situations: Invoking the tool from a context where fd 0 was closed or redirected to a bad descriptor, running under a sandbox/daemon that detached stdio, or a misconfigured JNI spawn that did not wire stdin.

Related errors


AI-assisted analysis of DrKLO/Telegram@45ab8f4308 (2026-08-14). Data as JSON: /api/errors/f1af526174263055. Report an issue: GitHub.