changeset: 45:f5aec9cf9188
tag: tip
user: Anselm R. Garbe <arg_AT_suckless.org>
date: Thu Mar 01 16:42:13 2007 +0100
summary: continued work on st
diff -r d40a37bf5c76 -r f5aec9cf9188 main.c
--- a/main.c Sat Feb 24 15:30:34 2007 +0100
+++ b/main.c Thu Mar 01 16:42:13 2007 +0100
@@ -20,6 +20,10 @@
#include <unistd.h>
#define TIMEOUT_USEC 10000
+
+extern int getpt(void);
+extern int grantpt(int fd);
+extern int unlockpt(int fd);
/* static */
@@ -47,7 +51,7 @@ getpty(void) {
return -1;
}
-static int
+void
getty(void) {
int i;
@@ -59,15 +63,10 @@ getty(void) {
dup2(master, 0);
dup2(master, 1);
dup2(master, 2);
- if(ioctl(master, TIOCSCTTY, NULL) < 0) {
- fputs("cannot set controlling terminal\n", stderr);
- return -1;
- }
- if(ioctl(master, TIOCSWINSZ, &wsz) < 0) {
- fputs("cannot set window size\n", stderr);
- return -1;
- }
- return 0;
+ if(ioctl(master, TIOCSCTTY, NULL) < 0)
+ eprint("cannot set controlling terminal\n");
+ if(ioctl(master, TIOCSWINSZ, &wsz) < 0)
+ eprint("cannot set window size\n");
}
static void
@@ -87,40 +86,23 @@ sigchld(int sig) {
exit(EXIT_SUCCESS);
}
-static int
-rc(int argc, char *argv[]) {
- uid_t uid;
- struct passwd *pw;
- char *args[3];
-
- if((uid = getuid() == - 1))
- eprint("unknown uid\n");
- if(!(pw = getpwuid(uid)))
- eprint("unknown password for uid '%d'\n", uid);
+void
+rc(char *args[]) {
if((master = getpty()) < 0)
- return -1;
+ eprint("cannot open pseudo-tty\n");
switch((pid = fork())) {
+ case -1:
+ eprint("cannot fork");
+ case 0: /* child */
+ getty();
+ putenv("TERM=xterm");
+ execvp(args[0], args);
+ eprint("cannot execvp");
default:
close(slave);
signal(SIGCHLD, sigchld);
break;
- case -1:
- perror("cannot fork");
- return -1;
- case 0: /* child */
- getty();
- putenv("TERM=xterm");
- chdir(pw->pw_dir);
- args[0] = "ls"; /*pw->pw_shell;*/
- args[1] = "/"; /*"-i";*/
- args[2] = NULL;
- execvp(pw->pw_shell, args);
- fprintf(stderr, "st: execvp '%s -i'", pw->pw_shell);
- perror(" failed");
- exit(EXIT_FAILURE);
- break;
}
- return 0;
}
/* extern */
@@ -135,16 +117,15 @@ Fnt bold;
int
main(int argc, char *argv[]) {
+ char *args[3];
+ fd_set rd;
int xfd;
+ unsigned int len;
+ XEvent ev;
struct timeval tv;
- unsigned int len;
- fd_set rd;
- XEvent ev;
- if(!strncmp("-v", argv[1], 3)) {
- fputs("st-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
- exit(EXIT_SUCCESS);
- }
+ if(argc == 2 && !strncmp("-v", argv[1], 3))
+ eprint("st-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n");
setlocale(LC_CTYPE, "");
dpy = XOpenDisplay(0);
if(!dpy)
@@ -155,8 +136,10 @@ main(int argc, char *argv[]) {
wsz.ws_row = 25;
wsz.ws_col = 80;
wsz.ws_xpixel = wsz.ws_ypixel = 0;
- if(rc(argc, argv) < 0)
- eprint("cannot open pseudo-tty\n");
+ args[0] = "sh";
+ args[1] = "-i";
+ args[2] = NULL;
+ rc(args);
for(;;) {
FD_ZERO(&rd);
FD_SET(master, &rd);
@@ -168,13 +151,6 @@ main(int argc, char *argv[]) {
continue;
eprint("select failed\n");
}
- if(FD_ISSET(xfd, &rd)) {
- while(XPending(dpy)) {
- XNextEvent(dpy, &ev);
- if(handler[ev.type])
- (handler[ev.type])(&ev);
- }
- }
if(FD_ISSET(master, &rd)) {
len = read(master, buffer, sizeof buffer);
if(len < 0)
@@ -184,6 +160,11 @@ main(int argc, char *argv[]) {
buffer[len] = 0;
fprintf(stderr, "got '%s'\n", buffer);
}
+ while(XPending(dpy)) {
+ XNextEvent(dpy, &ev);
+ if(handler[ev.type])
+ (handler[ev.type])(&ev);
+ }
}
XCloseDisplay(dpy);
return 0;
Received on Thu Mar 01 2007 - 16:41:52 UTC
This archive was generated by hypermail 2.2.0 : Sun Jul 13 2008 - 15:56:02 UTC