Skip to content

Commit

Permalink
Simplify isatty()
Browse files Browse the repository at this point in the history
  • Loading branch information
th-otto committed Apr 28, 2020
1 parent 7a1b4e1 commit 329612c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 42 deletions.
13 changes: 5 additions & 8 deletions sources/fileno.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#include <stdio.h>
#include <errno.h>
#include "lib.h"

int fileno(FILE *stream)
int (fileno)(FILE *stream)
{
if (stream != NULL)
return FILE_GET_HANDLE(stream);
else
{
// errno = EBADF;
return -1;
}

}
__set_errno(EBADF);
return -1;
}
40 changes: 6 additions & 34 deletions sources/isatty.c
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
#include <mint/mintbind.h>
#include <mint/errno.h>
#include <unistd.h>

#ifndef TIOCGPGRP
#define TIOCGPGRP (('T'<< 8) | 6)
#endif
int isatty (int handle) {
long r, pos;

int isatty (int fd) {
long dummy, retval;
int rc;

retval = Fcntl (fd, &dummy, TIOCGPGRP);
if (retval == -ENOSYS) {
long oldloc;

/* save current location */
oldloc = Fseek (0L, fd, SEEK_CUR);

/* try to seek ahead one byte */
if (Fseek (1L, fd, SEEK_CUR) != 0) {
/* got either a file position or an error (usually
* EBADARG indicating a range error from trying to
* seek past EOF), so it is not a tty
*/
rc = 0;

/* seek back to original location */
(void) Fseek (oldloc, fd, SEEK_SET);
}
else {
rc = 1; /* yes, tty */
}
}
else {
rc = (retval == 0);
}
return rc;
pos = Fseek(0, handle, SEEK_CUR);
r = Fseek(1, handle, SEEK_SET);
Fseek(pos, handle, SEEK_SET);
return r == 0;
}

0 comments on commit 329612c

Please sign in to comment.