Skip to content

Commit

Permalink
update to avoid test failure in windows
Browse files Browse the repository at this point in the history
  • Loading branch information
vasudeva8 committed Aug 16, 2023
1 parent e3a614d commit 0832d64
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions bgzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <io.h> //for _dup method
# undef _fileno //to use _fileno method instead of macro
#endif

static const int WINDOW_SIZE = BGZF_BLOCK_SIZE;
Expand Down Expand Up @@ -127,6 +129,37 @@ static int bgzip_main_usage(FILE *fp, int status)
return status;
}

/// getstdinout - to get stdin/stdout duplicated, abtracting OS dependencies
/** @param fd file descriptor to be duplicated
* returns duplicated descriptor on success -1 on failure
* the descriptor is set to BINARY mode in windows to avoid errors - both input as well as output
*/
int getstdinout(int fd)
{
int dupfd = -1;
#ifdef _WIN32
dupfd = _dup(fd); //windows specific dup api
_setmode(dupfd, O_BINARY); //set to binary mode
#else
dupfd = dup(fd);
#endif //_WIN32

return dupfd;
}

/// getfileno - to get file descriptor from FILE pointer, abtracting OS dependencies
/** @param fp FILE pointer
* returns file descriptor
*/
int getfileno(FILE *fp)
{
#ifdef _WIN32
return _fileno(fp); //windows specific api
#else
return fileno(fp);
#endif //_WIN32
}

int main(int argc, char **argv)
{
int c, compress, compress_level = -1, pstdout, is_forced, test, index = 0, rebgzip = 0, reindex = 0, keep, binary;
Expand Down Expand Up @@ -207,14 +240,14 @@ int main(int argc, char **argv)
out_mode_exclusive[2] = compress_level + '0';
}

if (!(f_src = !isstdin ? hopen(argv[optind], "r") : hdopen(dup(STDIN_FILENO), "r"))) {
if (!(f_src = !isstdin ? hopen(argv[optind], "r") : hdopen(getstdinout(STDIN_FILENO), "r"))) {
fprintf(stderr, "[bgzip] %s: %s\n", strerror(errno), argv[optind]);
return 1;
}
if ( argc>optind && !isstdin )
{
if (pstdout)
fp = bgzf_dopen(dup(STDOUT_FILENO), out_mode);
fp = bgzf_dopen(getstdinout(STDOUT_FILENO), out_mode);
else
{
char *name = malloc(strlen(argv[optind]) + 5);
Expand All @@ -241,15 +274,15 @@ int main(int argc, char **argv)
free(name);
}
}
else if (!pstdout && isatty(fileno((FILE *)stdout)) )
else if (!pstdout && isatty(getfileno((FILE *)stdout)) )
return bgzip_main_usage(stderr, EXIT_FAILURE);
else if ( index && !index_fname )
{
fprintf(stderr, "[bgzip] Index file name expected when writing to stdout\n");
return 1;
}
else
fp = bgzf_dopen(dup(STDOUT_FILENO), out_mode);
fp = bgzf_dopen(getstdinout(STDOUT_FILENO), out_mode);

if ( index && rebgzip )
{
Expand Down Expand Up @@ -398,7 +431,7 @@ int main(int argc, char **argv)
else
{
if ( !index_fname ) error("[bgzip] Index file name expected when reading from stdin\n");
fp = bgzf_dopen(dup(STDIN_FILENO), "r");
fp = bgzf_dopen(getstdinout(STDIN_FILENO), "r");
if ( !fp ) error("[bgzip] Could not read from stdin: %s\n", strerror(errno));
}

Expand Down Expand Up @@ -441,7 +474,7 @@ int main(int argc, char **argv)
}

if (pstdout || test) {
f_dst = dup(STDOUT_FILENO);
f_dst = getstdinout(STDOUT_FILENO);
}
else {
const int wrflags = O_WRONLY | O_CREAT | O_TRUNC;
Expand Down Expand Up @@ -484,12 +517,12 @@ int main(int argc, char **argv)
free(name);
}
}
else if (!pstdout && isatty(fileno((FILE *)stdin)))
else if (!pstdout && isatty(getfileno((FILE *)stdin)))
return bgzip_main_usage(stderr, EXIT_FAILURE);
else
{
f_dst = dup(STDOUT_FILENO);
fp = bgzf_dopen(dup(STDIN_FILENO), "r");
f_dst = getstdinout(STDOUT_FILENO);
fp = bgzf_dopen(getstdinout(STDIN_FILENO), "r");
if (fp == NULL) {
fprintf(stderr, "[bgzip] Could not read from stdin: %s\n", strerror(errno));
return 1;
Expand Down

0 comments on commit 0832d64

Please sign in to comment.