wrap gets on Windows, replacing '\r\n' with '\n'

This commit is contained in:
Brent Cook 2015-12-05 13:58:37 -06:00
parent d7317353a9
commit 75ef5bb160
2 changed files with 16 additions and 0 deletions

View File

@ -38,6 +38,20 @@ posix_fopen(const char *path, const char *mode)
return fopen(path, mode);
}
char *
posix_fgets(char *s, int size, FILE *stream)
{
char *ret = fgets(s, size, stream);
if (ret != NULL) {
size_t end = strlen(ret);
if (end >= 2 && ret[end - 2] == '\r' && ret[end - 1] == '\n') {
ret[end - 2] = '\n';
ret[end - 1] = '\0';
}
}
return ret;
}
int
posix_rename(const char *oldpath, const char *newpath)
{

View File

@ -28,11 +28,13 @@ int asprintf(char **str, const char *fmt, ...);
void posix_perror(const char *s);
FILE * posix_fopen(const char *path, const char *mode);
char * posix_fgets(char *s, int size, FILE *stream);
int posix_rename(const char *oldpath, const char *newpath);
#ifndef NO_REDEF_POSIX_FUNCTIONS
#define perror(errnum) posix_perror(errnum)
#define fopen(path, mode) posix_fopen(path, mode)
#define fgets(s, size, stream) posix_fgets(s, size, stream)
#define rename(oldpath, newpath) posix_rename(oldpath, newpath)
#endif