Skip to content

Commit

Permalink
getline() must handle stdin as well.
Browse files Browse the repository at this point in the history
Signed-off-by: Jorgen Lundman <[email protected]>
  • Loading branch information
lundman committed Apr 10, 2024
1 parent 76773e5 commit d09e272
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lib/libspl/include/os/windows/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ extern size_t strlcpy(register char *s, register const char *t,
extern size_t strlcat(register char *s, register const char *t,
register size_t n);

extern ssize_t getline_impl(char **linep, size_t *linecapp, FILE *stream,
boolean_t internal);
extern ssize_t getline(char **linep, size_t *linecapp, FILE *stream);

// int pread_win(HANDLE h, void *buf, size_t nbyte, off_t offset);
Expand Down
6 changes: 4 additions & 2 deletions lib/libspl/include/os/windows/wfunopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,12 @@ static inline ssize_t wosix_getline(char **linep, size_t *linecap, FILE *f)
fakeFILE *fFILE = (fakeFILE *)f;
int result;

if (fFILE->realFILE)
if (f == stdin)
result = getline_impl(linep, linecap, f, FALSE);
else if (fFILE->realFILE)
result = getline(linep, linecap, fFILE->realFILE);
else
result = fFILE->readfn(fFILE->cookie, *linep, *linecap);
result = getline_impl(linep, linecap, (FILE *)fFILE, TRUE);

return (result);
}
Expand Down
27 changes: 20 additions & 7 deletions lib/libspl/os/windows/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <langinfo.h>
#include <os/windows/zfs/sys/zfs_ioctl_compat.h>
#include <sys/mman.h>
#include <wfunopen.h>

/* Magic instruction to compiler to add library */
#pragma comment(lib, "ws2_32.lib")
Expand Down Expand Up @@ -796,10 +797,10 @@ strlcpy(register char *s, register const char *t, register size_t n)
break;
}
} while ((*s++ = *t++));
if (!n)
while (*t++)
;
return (t - o - 1);
if (!n)
while (*t++)
;
return (t - o - 1);
}

extern size_t
Expand Down Expand Up @@ -889,17 +890,21 @@ console_echo(boolean_t willecho)
// Not really getline, just used for password input in libzfs_crypto.c
#define MAX_GETLINE 128
ssize_t
getline(char **linep, size_t *linecapp,
FILE *stream)
getline_impl(char **linep, size_t *linecapp,
FILE *stream, boolean_t internal)
{
static char getpassbuf[MAX_GETLINE + 1];
size_t i = 0;
fakeFILE *fFILE = (fakeFILE *)stream;

console_echo(FALSE);

int c;
for (;;) {
c = getc(stream);
if (internal)
fFILE->readfn(fFILE->cookie, (char *)&c, 1);
else
c = getc(stream);
if ((c == '\r') || (c == '\n')) {
getpassbuf[i] = '\0';
break;
Expand All @@ -920,6 +925,14 @@ getline(char **linep, size_t *linecapp,
return (i);
}

#undef getline
ssize_t
getline(char **linep, size_t *linecapp, FILE *stream)
{
return (getline_impl(linep, linecapp,
stream, FALSE));
}


/* Windows POSIX wrappers */

Expand Down

0 comments on commit d09e272

Please sign in to comment.