This is the mail archive of the cygwin mailing list for the Cygwin 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]

/dev/clipboard: No O_TEXT conversion if write() is used


fopen("/dev/clipboard", "wt") sets O_TEXT, fprintf() does LF->CRLF conversion but write() does not. The latter is IMO a bug.

fopen("/dev/clipboard", "wb") sets both O_TEXT and O_BINARY, fwrite() and write() do no conversion. The set_flags() call in fhandler_dev_clipboard::open() should possibly clear O_BINARY.

With regular files, everything works as expected.

Testcase:

$ uname -srvm
CYGWIN_NT-6.1-WOW64 1.7.25(0.270/5/3) 2013-08-31 20:39 i686

$ cat testmode.c
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  const char * name = argv[1], * flags = argv[2];
  FILE * f = fopen(name, flags);
  if (!f) {
    perror(name); return 1;
  }
  int fd = fileno(f), mode = getmode(fd);
  printf("fd=%d, mode=%x%s%s\n", fd, mode,
    (mode & O_BINARY ? " O_BINARY":""),
    (mode & O_TEXT ? " O_TEXT":""));
  fprintf(f, "printf1\nprintf2\n"); fflush(f);
  write(fd, "write1\nwrite2\n", 14);
  fprintf(f, "printf3\nprintf4\n"); fflush(f);
  write(fd, "write3\nwrite4\n", 14);
  fclose(f);
  return 0;
}

$ gcc -o testmode testmode.c

$ ./testmode /dev/clipboard wt ; cat -A /dev/clipboard
fd=3, mode=20000 O_TEXT
printf1^M$
printf2^M$
write1$
write2$
printf3^M$
printf4^M$
write3$
write4$

$ ./testmode /dev/clipboard wb ; cat -A /dev/clipboard
fd=3, mode=30000 O_BINARY O_TEXT
printf1$
printf2$
write1$
write2$
printf3$
printf4$
write3$
write4$

$ ./testmode out wt ; cat -A out
fd=3, mode=20000 O_TEXT
printf1^M$
printf2^M$
write1^M$
...

$ ./testmode out wb ; cat -A out
fd=3, mode=10000 O_BINARY
printf1$
printf2$
write1$
...


This is likely the reason for the following inconsistent behavior:

$ echo test1 >/dev/clipboard ; cat -A /dev/clipboard
test1$

$ /bin/echo test2 >/dev/clipboard ; cat -A /dev/clipboard
test2^M$

$ /bin/echo test3 | cat > /dev/clipboard ; cat -A /dev/clipboard
test3$

$ echo test4 | sed s,x,x, > /dev/clipboard ; cat -A /dev/clipboard
test4^M$


Christian


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple


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