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 8e21206 commit e102fe4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/libspl/include/os/windows/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ 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(char **linep, size_t *linecapp, FILE *stream);
extern ssize_t getline_impl(char **linep, size_t *linecapp, FILE *stream,
boolean_t internal);
#define getline(L, C, S) getline_impl((L), (C), (S), FALSE)

// int pread_win(HANDLE h, void *buf, size_t nbyte, off_t offset);
extern int pipe(int fildes[2]);
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, fFILE, TRUE);

return (result);
}
Expand Down
11 changes: 8 additions & 3 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 @@ -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, &c, 1);
else
c = getc(stream);
if ((c == '\r') || (c == '\n')) {
getpassbuf[i] = '\0';
break;
Expand Down

0 comments on commit e102fe4

Please sign in to comment.