darren@bacchus.UUCP (Darren Friedlein) (10/11/88)
I recently got the sources to "talk" (the newest version I think) and was having some trouble getting it to work with multiple gettys. The root of the problem was the getlogin() function which returns the username of the user in /dev/w1. cuserid() did the same thing. To remedy the situation, I wrote a function getlognam() which seems to work all the time. It makes use of getuid() and getpwuid(). It's pretty tacky, but it's all I could get to work. I was putting the username in a malloc()ed space and returning a pointer, but for SOME reason, only root could run it at that point without a core dump. Anyway, to get this thing over with quick, I allocated a 32 byte string at the beginning of the program and use that. Oh - one other change I made: it won't die when you switch windows any more. That was REAL aggravating! What follows is a context diff of the changes I made. Comments are encouraged. Enjoy! -darren ____ / \ | Rt 4, Box 416, Durham, NC 27703 _____|_____ Darren G. Friedlein data (bacchus) : 919/596-7746 / | \ voice : 919/596-9492 ( | ) \____/ __/ {mcnc|icus|ditka|ethos|gladys|bakerst}!bacchus!darren *** talk.c.orig Mon Oct 10 19:15:58 1988 --- talk.c Mon Oct 10 21:00:57 1988 *************** *** 21,26 #include <curses.h> #include <signal.h> #include <fcntl.h> #include "talk.h" struct msgbuf sndbuf, rcvbuf; --- 21,27 ----- #include <curses.h> #include <signal.h> #include <fcntl.h> + #include <pwd.h> #include "talk.h" struct msgbuf sndbuf, rcvbuf; *************** *** 28,34 int msqid; int avail; /* The availabity of a perspective partner */ int parpid = 0; /* The pid of the partner */ ! char *ctime(), *getlogin(); time_t tloc, otime, time(); main(argc, argv) --- 29,36 ----- int msqid; int avail; /* The availabity of a perspective partner */ int parpid = 0; /* The pid of the partner */ ! char reserved[32]; /* See explaination in getlognam() */ ! char *ctime(), *getlognam(); time_t tloc, otime, time(); main(argc, argv) *************** *** 72,78 * We need to tell the demon who we are, who we want to talk to, * the tty of who we want (if it was given) and our process id. */ ! strncpy(dmsgbuf.msgval.mtext, getlogin(), NAMESIZ); strncpy(&dmsgbuf.msgval.mtext[NAMESIZ], argv[1], NAMESIZ); if (argv[2]) strncpy(&dmsgbuf.msgval.mtext[TTYLOC], argv[2], LINESIZ); --- 74,80 ----- * We need to tell the demon who we are, who we want to talk to, * the tty of who we want (if it was given) and our process id. */ ! strncpy(dmsgbuf.msgval.mtext, getlognam(), NAMESIZ); strncpy(&dmsgbuf.msgval.mtext[NAMESIZ], argv[1], NAMESIZ); if (argv[2]) strncpy(&dmsgbuf.msgval.mtext[TTYLOC], argv[2], LINESIZ); *************** *** 123,128 for (c = 0; c < NSIG; c++) signal(c, Finish); cbreak(); noecho(); #ifndef SCHIZO --- 125,136 ----- for (c = 0; c < NSIG; c++) signal(c, Finish); + /* + * OK - ONE exception - let's not quit just because the person + * switched windows! -df + */ + signal(SIGWIND, SIG_IGN); + cbreak(); noecho(); #ifndef SCHIZO *************** *** 324,331 tloc = time((time_t *)0); if ((fp = fopen(&dmsgbuf.msgval.mtext[TTYLOC], "w")) != NULL) { fprintf(fp, "\r\n%c%cTalk request from %s on %s at %5.5s\r\n", ! '\007', '\011', getlogin(), ttyname(0)+5, ctime(&tloc)+11); ! fprintf(fp, "%cRespond with 'talk %s'\r\n", '\007', getlogin()); fclose(fp); } /* --- 332,339 ----- tloc = time((time_t *)0); if ((fp = fopen(&dmsgbuf.msgval.mtext[TTYLOC], "w")) != NULL) { fprintf(fp, "\r\n%c%cTalk request from %s on %s at %5.5s\r\n", ! '\007', '\011', getlognam(), ttyname(0)+5, ctime(&tloc)+11); ! fprintf(fp, "%cRespond with 'talk %s'\r\n", '\007', getlognam()); fclose(fp); } /* *************** *** 402,408 kill(parpid, SIGUSR1); else if ((fp = fopen(&dmsgbuf.msgval.mtext[TTYLOC], "w")) != NULL) { tloc = time((long *)0); ! fprintf(fp, "\r\n%cTalk request from %s cancelled at %5.5s\r\n", '\011', getlogin(), ctime(&tloc)+11); fclose(fp); } } --- 410,416 ----- kill(parpid, SIGUSR1); else if ((fp = fopen(&dmsgbuf.msgval.mtext[TTYLOC], "w")) != NULL) { tloc = time((long *)0); ! fprintf(fp, "\r\n%cTalk request from %s cancelled at %5.5s\r\n", '\011', getlognam(), ctime(&tloc)+11); fclose(fp); } } *************** *** 428,430 exit(0); } --- 436,450 ----- exit(0); } + char *getlognam() + { + /* + * I use this funky "reserved" thing because a malloc() kept dumping core. + * I'm not sure why... -df + */ + struct passwd *passwerd; + + passwerd = (struct passwd *) getpwuid(getuid()); + strcpy(reserved, passwerd->pw_name); + return(reserved); + }