Skip to content

Commit be591fd

Browse files
authored
Merge pull request #262 from akgrant43/unixFileDescriptorType
sqFilePluginBasicPrims.c add sqFileDescriptorType() Add the Unix implementation of sqFileDescriptorType() to answer an enumerated type indicating whether the supplied file descriptor is a terminal, pipe or file. The Windows implementation was added 15 May 2018 in commit 858bed2.
2 parents d0c7f21 + d997f35 commit be591fd

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

platforms/Cross/plugins/FilePlugin/sqFilePluginBasicPrims.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,8 +582,19 @@ sqFileStdioHandlesInto(SQFile files[])
582582
* 4 - cygwin terminal (windows only)
583583
*/
584584
sqInt sqFileDescriptorType(int fdNum) {
585-
/* TODO Implement the unix version */
586-
return isatty(fdNum);
585+
int status;
586+
struct stat statBuf;
587+
588+
/* Is this a terminal? */
589+
if (isatty(fdNum)) return 1;
590+
591+
/* Is this a pipe? */
592+
status = fstat(fdNum, &statBuf);
593+
if (status) return -1;
594+
if (S_ISFIFO(statBuf.st_mode)) return 2;
595+
596+
/* Must be a normal file */
597+
return 3;
587598
}
588599

589600

0 commit comments

Comments
 (0)