From cc6e7cb3477cdb34c23be8ce246203d2b7f002de Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Thu, 24 Apr 2008 21:30:38 +0000 Subject: [PATCH] Added the BSDish openpty(). git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@25135 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/compatibility/bsd/pty.h | 19 ++++++++++++++ src/libs/bsd/Jamfile | 1 + src/libs/bsd/pty.cpp | 44 +++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 headers/compatibility/bsd/pty.h create mode 100644 src/libs/bsd/pty.cpp diff --git a/headers/compatibility/bsd/pty.h b/headers/compatibility/bsd/pty.h new file mode 100644 index 0000000000..dbb7334caa --- /dev/null +++ b/headers/compatibility/bsd/pty.h @@ -0,0 +1,19 @@ +/* + * Copyright 2008, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _BSD_PTY_H_ +#define _BSD_PTY_H_ + +#include +#include + + +__BEGIN_DECLS + +extern int openpty(int* master, int* slave, char* name, + struct termios* termAttrs, struct winsize* windowSize); + +__END_DECLS + +#endif // _BSD_PTY_H_ diff --git a/src/libs/bsd/Jamfile b/src/libs/bsd/Jamfile index c5b6c2b53d..0e54903cfd 100644 --- a/src/libs/bsd/Jamfile +++ b/src/libs/bsd/Jamfile @@ -11,6 +11,7 @@ SharedLibrary libbsd.so : getpass.c issetugid.c progname.c + pty.cpp signal.c stringlist.c unvis.c diff --git a/src/libs/bsd/pty.cpp b/src/libs/bsd/pty.cpp new file mode 100644 index 0000000000..1cf6a98eae --- /dev/null +++ b/src/libs/bsd/pty.cpp @@ -0,0 +1,44 @@ +/* + * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ + +#include + +#include +#include +#include + + +int +openpty(int* _master, int* _slave, char* name, struct termios* termAttrs, + struct winsize* windowSize) +{ + int master = posix_openpt(O_RDWR); + if (master < 0) + return -1; + + int slave; + const char *ttyName; + if (grantpt(master) != 0 || unlockpt(master) != 0 + || (ttyName = ptsname(master)) == NULL + || (slave = open(ttyName, O_RDWR | O_NOCTTY)) < 0) { + close(master); + return -1; + } + + if (termAttrs != NULL && tcsetattr(master, TCSANOW, termAttrs) != 0 + || windowSize != NULL + && ioctl(master, TIOCSWINSZ, windowSize, sizeof(winsize)) != 0) { + close(slave); + close(master); + } + + *_master = master; + *_slave = slave; + + if (name != NULL) + strcpy(name, ttyName); + + return 0; +}