This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug stdio/20677] perror() changes orientation of stream when it is redirected


https://sourceware.org/bugzilla/show_bug.cgi?id=20677

--- Comment #8 from Igor Liferenko <igor.liferenko at gmail dot com> ---
This little program demonstrates that the idea with "write()" works perfectly:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

int main(void)
{
  int fd;
  int fd2;
  char buf[200];
  int n;
  char fname[] = "/tmp/tst-perror.XXXXXX";
  fd = mkstemp (fname);
  fclose (stderr);
  dup2 (fd, 2);
  stderr = fdopen (2, "w");
  fd2 = fileno(stderr);
  char *s = "this is a test\n";
  n = write(fd2, s, strlen(s));
  fprintf(stderr, "multibyte string\n");
  fclose (stderr);
  lseek (fd, 0, SEEK_SET);
  n = read (fd, buf, sizeof (buf));
  printf("%.*s", (int) n, buf);
  close (fd);
  return 0;
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]