delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/02/27/09:37:49

Date: Thu, 27 Feb 2003 14:40:28 +0000
From: "Richard Dawe" <rich AT phekda DOT freeserve DOT co DOT uk>
Sender: rich AT phekda DOT freeserve DOT co DOT uk
To: djgpp-workers AT delorie DOT com
X-Mailer: Emacs 21.3.50 (via feedmail 8.3.emacs20_6 I) and Blat ver 1.8.6
Subject: Use _get_dev_info & constants in isatty; add test [PATCH]
Message-Id: <E18oPAP-0001Aw-00@phekda.freeserve.co.uk>
Reply-To: djgpp-workers AT delorie DOT com

Hello.

I noticed the other day that isatty uses values rather than using
the constants from <libc/getdinfo.h>. So I fixed that. I also made
it use a _get_dev_info call, rather than duplicating that code.
Using _get_dev_info means that it will detect bad file descriptors
and return errno == EBADF and -1 (as required by POSIX).
I also added a test program. See the patch below.

OK to commit?

Bye, Rich =]

Index: src/libc/posix/unistd/isatty.c
===================================================================
RCS file: /cvs/djgpp/djgpp/src/libc/posix/unistd/isatty.c,v
retrieving revision 1.1
diff -p -c -3 -r1.1 isatty.c
*** src/libc/posix/unistd/isatty.c	10 May 1995 04:03:22 -0000	1.1
--- src/libc/posix/unistd/isatty.c	27 Feb 2003 14:34:42 -0000
***************
*** 1,16 ****
  /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  #include <libc/stubs.h>
  #include <unistd.h>
! #include <dpmi.h>
  
  int
  isatty(int fd)
  {
!   __dpmi_regs r;
!   r.x.ax = 0x4400;
!   r.x.bx = fd;
!   __dpmi_int(0x21, &r);
!   if ((r.x.ax & 0x83) == 0x83)
      return 1;
    return 0;
  }
--- 1,21 ----
+ /* Copyright (C) 2003 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  #include <libc/stubs.h>
  #include <unistd.h>
! #include <io.h>
! #include <libc/getdinfo.h>
! 
! #define ISATTY_BITS (_DEV_CDEV|_DEV_STDIN|_DEV_STDOUT)
  
  int
  isatty(int fd)
  {
!   const int dev_info = _get_dev_info(fd);
! 
!   if (dev_info == -1)
!     return -1;
! 
!   if ((dev_info & ISATTY_BITS) == ISATTY_BITS)
      return 1;
    return 0;
  }
Index: tests/libc/posix/unistd/makefile
===================================================================
RCS file: /cvs/djgpp/djgpp/tests/libc/posix/unistd/makefile,v
retrieving revision 1.3
diff -p -c -3 -r1.3 makefile
*** tests/libc/posix/unistd/makefile	22 May 2001 20:52:27 -0000	1.3
--- tests/libc/posix/unistd/makefile	27 Feb 2003 14:34:42 -0000
*************** SRC += getpid.c
*** 8,12 ****
--- 8,13 ----
  SRC += sleep.c
  SRC += tread.c
  SRC += write.c
+ SRC += t-isatty.c
  
  include $(TOP)/../makefile.inc
*** /dev/null	Thu Feb 27 14:37:44 2003
--- tests/libc/posix/unistd/t-isatty.c	Thu Feb 27 14:35:40 2003
***************
*** 0 ****
--- 1,19 ----
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <unistd.h>
+ #include <assert.h>
+ 
+ int
+ main (void)
+ {
+   /* Check the standard handles */
+   assert(isatty(fileno(stdin)));
+   assert(isatty(fileno(stdout)));
+   assert(!isatty(fileno(stdprn)));
+ 
+   /* Check an invalid file descriptor. */
+   assert(isatty(99) < 0);
+ 
+   puts("PASS");
+   return(EXIT_SUCCESS);
+ }

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019