Fix bugs in <termios.h>; add basic pty functionality

diff --git a/include/stdlib.h b/include/stdlib.h
index 4d885a9..bcff2a0 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -80,4 +80,14 @@
   srand48(__s);
 }
 
+/* Basic PTY functions.  These only work if devpts is mounted! */
+
+static __inline__ int grantpt(int __fd)
+{
+  (void)__fd;
+  return 0;			/* devpts does this all for us! */
+}
+__extern int unlockpt(int);
+__extern char *ptsname(int);
+
 #endif /* _STDLIB_H */
diff --git a/include/termios.h b/include/termios.h
index d1f58ba..08a5e56 100644
--- a/include/termios.h
+++ b/include/termios.h
@@ -5,7 +5,7 @@
 #ifndef _TERMIOS_H
 #define _TERMIOS_H
 
-#include <extern.h>
+#include <klibc/extern.h>
 #include <stdint.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
@@ -63,23 +63,23 @@
 
 static __inline__ speed_t cfgetospeed(const struct termios *__s)
 {
-  return (speed_t)(__s->c_cflags & CBAUD);
+  return (speed_t)(__s->c_cflag & CBAUD);
 }
 
 static __inline__ speed_t cfgetispeed(const struct termios *__s)
 {
-  return (speed_t)(__s->c_cflags & CBAUD);
+  return (speed_t)(__s->c_cflag & CBAUD);
 }
 
 static __inline__ int cfsetospeed(struct termios *__s, speed_t __v)
 {
-  __s->c_cflags = (__s->c_cflags & ~CBAUD) | (__v & CBAUD);
+  __s->c_cflag = (__s->c_cflag & ~CBAUD) | (__v & CBAUD);
   return 0;
 }
 
 static __inline__ int cfsetispeed(struct termios *__s, speed_t __v)
 {
-  __s->c_cflags = (__s->c_cflags & ~CBAUD) | (__v & CBAUD);
+  __s->c_cflag = (__s->c_cflag & ~CBAUD) | (__v & CBAUD);
   return 0;
 }
 
diff --git a/klibc/Makefile b/klibc/Makefile
index c507118..33c5c20 100644
--- a/klibc/Makefile
+++ b/klibc/Makefile
@@ -28,7 +28,7 @@
 	  strsep.o strtok.o \
 	  gethostname.o getdomainname.o getcwd.o seteuid.o setegid.o \
 	  getenv.o setenv.o unsetenv.o getopt.o readdir.o \
-	  syslog.o closelog.o \
+	  syslog.o closelog.o pty.o \
 	  time.o fdatasync.o llseek.o select.o nice.o getpriority.o \
 	  qsort.o lrand48.o srand48.o seed48.o \
 	  inet/inet_ntoa.o inet/inet_aton.o inet/inet_addr.o \
diff --git a/klibc/include/stdlib.h b/klibc/include/stdlib.h
index 4d885a9..bcff2a0 100644
--- a/klibc/include/stdlib.h
+++ b/klibc/include/stdlib.h
@@ -80,4 +80,14 @@
   srand48(__s);
 }
 
+/* Basic PTY functions.  These only work if devpts is mounted! */
+
+static __inline__ int grantpt(int __fd)
+{
+  (void)__fd;
+  return 0;			/* devpts does this all for us! */
+}
+__extern int unlockpt(int);
+__extern char *ptsname(int);
+
 #endif /* _STDLIB_H */
diff --git a/klibc/include/termios.h b/klibc/include/termios.h
index d1f58ba..08a5e56 100644
--- a/klibc/include/termios.h
+++ b/klibc/include/termios.h
@@ -5,7 +5,7 @@
 #ifndef _TERMIOS_H
 #define _TERMIOS_H
 
-#include <extern.h>
+#include <klibc/extern.h>
 #include <stdint.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
@@ -63,23 +63,23 @@
 
 static __inline__ speed_t cfgetospeed(const struct termios *__s)
 {
-  return (speed_t)(__s->c_cflags & CBAUD);
+  return (speed_t)(__s->c_cflag & CBAUD);
 }
 
 static __inline__ speed_t cfgetispeed(const struct termios *__s)
 {
-  return (speed_t)(__s->c_cflags & CBAUD);
+  return (speed_t)(__s->c_cflag & CBAUD);
 }
 
 static __inline__ int cfsetospeed(struct termios *__s, speed_t __v)
 {
-  __s->c_cflags = (__s->c_cflags & ~CBAUD) | (__v & CBAUD);
+  __s->c_cflag = (__s->c_cflag & ~CBAUD) | (__v & CBAUD);
   return 0;
 }
 
 static __inline__ int cfsetispeed(struct termios *__s, speed_t __v)
 {
-  __s->c_cflags = (__s->c_cflags & ~CBAUD) | (__v & CBAUD);
+  __s->c_cflag = (__s->c_cflag & ~CBAUD) | (__v & CBAUD);
   return 0;
 }
 
diff --git a/klibc/pty.c b/klibc/pty.c
new file mode 100644
index 0000000..5907ca2
--- /dev/null
+++ b/klibc/pty.c
@@ -0,0 +1,31 @@
+/*
+ * pty.c
+ *
+ * Basic Unix98 PTY functionality; assumes devpts
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+
+char *ptsname(int fd)
+{
+  static char buffer[32];	/* Big enough to hold even a 64-bit pts no */
+  unsigned int ptyno;
+
+  if ( ioctl(fd, TIOCGPTN, &ptyno) )
+    return NULL;
+  
+  snprintf(buffer, sizeof buffer, "/dev/pts/%u", ptyno);
+  
+  return buffer;
+}
+
+int unlockpt(int fd)
+{
+  int unlock = 0;
+
+  return ioctl(fd, TIOCSPTLCK, &unlock);
+}
diff --git a/pty.c b/pty.c
new file mode 100644
index 0000000..5907ca2
--- /dev/null
+++ b/pty.c
@@ -0,0 +1,31 @@
+/*
+ * pty.c
+ *
+ * Basic Unix98 PTY functionality; assumes devpts
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <termios.h>
+#include <sys/ioctl.h>
+
+char *ptsname(int fd)
+{
+  static char buffer[32];	/* Big enough to hold even a 64-bit pts no */
+  unsigned int ptyno;
+
+  if ( ioctl(fd, TIOCGPTN, &ptyno) )
+    return NULL;
+  
+  snprintf(buffer, sizeof buffer, "/dev/pts/%u", ptyno);
+  
+  return buffer;
+}
+
+int unlockpt(int fd)
+{
+  int unlock = 0;
+
+  return ioctl(fd, TIOCSPTLCK, &unlock);
+}