xref: /onnv-gate/usr/src/lib/libc/port/gen/ttyslot.c (revision 1914:8a8c5f225b1b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1914Scasper  * Common Development and Distribution License (the "License").
6*1914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*1914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Return the number of the slot in the utmp file
340Sstevel@tonic-gate  * corresponding to the current user: try for file 0, 1, 2.
350Sstevel@tonic-gate  * Returns -1 if slot not found.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #pragma weak ttyslot = _ttyslot
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "synonyms.h"
410Sstevel@tonic-gate #include <string.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/stat.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <utmpx.h>
470Sstevel@tonic-gate #include <stdlib.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #ifndef TRUE
500Sstevel@tonic-gate #define	TRUE 1
510Sstevel@tonic-gate #define	FALSE 0
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate 
540Sstevel@tonic-gate int
550Sstevel@tonic-gate ttyslot(void)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	struct futmpx ubuf;
580Sstevel@tonic-gate 	char *tp, *p;
590Sstevel@tonic-gate 	int s;
600Sstevel@tonic-gate 	int ret = -1, console = FALSE;
610Sstevel@tonic-gate 	char ttynm[128];
620Sstevel@tonic-gate 	FILE	*fp;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if ((tp = ttyname_r(0, ttynm, 128)) == NULL &&
650Sstevel@tonic-gate 	    (tp = ttyname_r(1, ttynm, 128)) == NULL &&
660Sstevel@tonic-gate 	    (tp = ttyname_r(2, ttynm, 128)) == NULL)
670Sstevel@tonic-gate 		return (-1);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	p = tp;
700Sstevel@tonic-gate 	if (strncmp(tp, "/dev/", 5) == 0)
710Sstevel@tonic-gate 		p += 5;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if (strcmp(p, "console") == 0)
740Sstevel@tonic-gate 		console = TRUE;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	s = 0;
77*1914Scasper 	if ((fp = fopen(UTMPX_FILE, "rF")) == NULL)
780Sstevel@tonic-gate 		return (-1);
790Sstevel@tonic-gate 	while ((fread(&ubuf, sizeof (ubuf), 1, fp)) == 1) {
800Sstevel@tonic-gate 		if ((ubuf.ut_type == INIT_PROCESS ||
810Sstevel@tonic-gate 		    ubuf.ut_type == LOGIN_PROCESS ||
820Sstevel@tonic-gate 		    ubuf.ut_type == USER_PROCESS) &&
830Sstevel@tonic-gate 		    strncmp(p, ubuf.ut_line, sizeof (ubuf.ut_line)) == 0) {
840Sstevel@tonic-gate 			ret = s;
850Sstevel@tonic-gate 			if (!console || strncmp(ubuf.ut_host, ":0", 2) == 0)
860Sstevel@tonic-gate 				break;
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 		s++;
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 	(void) fclose(fp);
910Sstevel@tonic-gate 	return (ret);
920Sstevel@tonic-gate }
93