[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

mintlib getpw* open file, isatty...



1. getpwnam + getpwuid left the passwd file open and then try to reuse
the open FILE * later, no good if a program closes all open fds before
a fork etc.  (rebuilt uucp with new libs and suddenly file requests end
up with getpwnam hanging in a blocking read from the modem line... :)

Index: getpw.c
@@ -299,9 +299,12 @@
 {
   setpwent();
   while (nextent(fp) != 0)
-    if (curentry.pw_uid == uid)
+    if (curentry.pw_uid == uid) {
+      endpwent();
       return(&curentry);
+    }
 
+  endpwent();
   return(NULL);
 } /* End of getpwuid() */
 
@@ -312,9 +315,12 @@
   setpwent();
 
   while (nextent(fp) != 0)
-    if (strcmp(curentry.pw_name, name) == 0)
+    if (strcmp(curentry.pw_name, name) == 0) {
+      endpwent();
       return(&curentry);
+    }
 
+  endpwent();
   return(NULL);
 } /* End of getpwnam() */
 
2. save 1 or 2 system calls in isatty and make it work more often on
reused fds that were not closed thru close.  there still is the case
fd not opened thru the lib, like named pipes and ptys because they need
special Fcreate modes.  (hmm another reason why we should better have a
real mknod or at least some kind of emulation for named pipes/ptys... :)

Index: isatty.c
@@ -21,19 +21,26 @@
   int rc;
   long oldloc;
   int handle = __OPEN_INDEX(fd);
+  extern int __mint;
 
   if (handle < __NHANDLES)
 	if (__open_stat[handle].status != FH_UNKNOWN)
 		return(__open_stat[handle].status == FH_ISATTY);
-  oldloc = Fseek(0L, fd, SEEK_CUR);	/* save current location */
-  if (Fseek(1L, fd, SEEK_CUR) != 0) {	/* try to seek ahead one byte */
-    /* got either a file position or an error (usually EBADARG indicating
-       a range error from trying to seek past EOF), so it is not a tty */
-    rc = 0;
-    (void) Fseek(oldloc, fd, SEEK_SET);	/* seek back to original location */
+  if (__mint) {
+	/* save 1 or 2 system calls (isatty gets called on every open...) */
+	long dummy;
+	rc = (Fcntl(fd, &dummy, TIOCGPGRP) == 0);
+  } else {
+    oldloc = Fseek(0L, fd, SEEK_CUR);	/* save current location */
+    if (Fseek(1L, fd, SEEK_CUR) != 0) {	/* try to seek ahead one byte */
+      /* got either a file position or an error (usually EBADARG indicating
+	 a range error from trying to seek past EOF), so it is not a tty */
+      rc = 0;
+      (void) Fseek(oldloc, fd, SEEK_SET);/* seek back to original location */
+    }
+    else
+      rc = 1;				/* yes, tty */
   }
-  else
-    rc = 1;				/* yes, tty */
   if (handle < __NHANDLES)
 	if (rc) {
 		__open_stat[handle].status = FH_ISATTY;
Index: open.c
@@ -164,6 +164,9 @@
 	if ((iomode & O_APPEND) && !(modemask & _REALO_APPEND))
 		(void)Fseek(0L, rv, SEEK_END);
 
+	/* fix the case `isatty() called before and not closed thru close()' */
+	if (__OPEN_INDEX(rv) < __NHANDLES)
+		__open_stat[__OPEN_INDEX(rv)].status = FH_UNKNOWN;
  	/* Important side effect:  isatty(rv) sets up flags under TOS */
 	if (isatty(rv) && (!(iomode & O_NOCTTY)) && (!(isatty(-1)))) {
           /* If the process is a session leader with no controlling tty,
Index: pipe.c
@@ -7,6 +7,7 @@
 #include <errno.h>
 #include <mintbind.h>
 #include <unistd.h>
+#include "lib.h"
 
 int
 pipe(fd)
@@ -22,5 +23,10 @@
 	}
 	fd[0] = mint_handle[0];
 	fd[1] = mint_handle[1];
+	/* fix the case `isatty() called before and not closed thru close()' */
+	if (__OPEN_INDEX(fd[0]) < __NHANDLES)
+		__open_stat[__OPEN_INDEX(fd[0])].status = FH_UNKNOWN;
+	if (__OPEN_INDEX(fd[1]) < __NHANDLES)
+		__open_stat[__OPEN_INDEX(fd[1])].status = FH_UNKNOWN;
 	return 0;
 }
-- 
J"urgen Lock / nox@jelal.north.de / UUCP: ..!uunet!unido!uniol!jelal!nox
								...ohne Gewehr
PGP public key fingerprint =  8A 18 58 54 03 7B FC 12  1F 8B 63 C7 19 27 CF DA