xref: /netbsd-src/lib/libc/termios/tcgetpgrp.c (revision 9e66e6d75e9910b3de5f4ef031995955f69a7dd1)
1*9e66e6d7Sabs /*	$NetBSD: tcgetpgrp.c,v 1.10 2012/06/25 22:32:46 abs Exp $	*/
25f11a56cSjtc 
35f11a56cSjtc /*-
45f11a56cSjtc  * Copyright (c) 1989, 1993
55f11a56cSjtc  *	The Regents of the University of California.  All rights reserved.
65f11a56cSjtc  *
75f11a56cSjtc  * Redistribution and use in source and binary forms, with or without
85f11a56cSjtc  * modification, are permitted provided that the following conditions
95f11a56cSjtc  * are met:
105f11a56cSjtc  * 1. Redistributions of source code must retain the above copyright
115f11a56cSjtc  *    notice, this list of conditions and the following disclaimer.
125f11a56cSjtc  * 2. Redistributions in binary form must reproduce the above copyright
135f11a56cSjtc  *    notice, this list of conditions and the following disclaimer in the
145f11a56cSjtc  *    documentation and/or other materials provided with the distribution.
15eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
165f11a56cSjtc  *    may be used to endorse or promote products derived from this software
175f11a56cSjtc  *    without specific prior written permission.
185f11a56cSjtc  *
195f11a56cSjtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
205f11a56cSjtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
215f11a56cSjtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
225f11a56cSjtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
235f11a56cSjtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
245f11a56cSjtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
255f11a56cSjtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
265f11a56cSjtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
275f11a56cSjtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
285f11a56cSjtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
295f11a56cSjtc  * SUCH DAMAGE.
305f11a56cSjtc  */
315f11a56cSjtc 
323ad08ca2Schristos #include <sys/cdefs.h>
335f11a56cSjtc #if defined(LIBC_SCCS) && !defined(lint)
345f11a56cSjtc #if 0
355f11a56cSjtc static char sccsid[] = "@(#)termios.c	8.2 (Berkeley) 2/21/94";
365f11a56cSjtc #else
37*9e66e6d7Sabs __RCSID("$NetBSD: tcgetpgrp.c,v 1.10 2012/06/25 22:32:46 abs Exp $");
385f11a56cSjtc #endif
395f11a56cSjtc #endif /* LIBC_SCCS and not lint */
405f11a56cSjtc 
4143fa6fe3Sjtc #include "namespace.h"
425f11a56cSjtc #include <sys/types.h>
435f11a56cSjtc #include <sys/ioctl.h>
44b48252f3Slukem 
45b48252f3Slukem #include <assert.h>
46b48252f3Slukem #include <errno.h>
475f11a56cSjtc #include <termios.h>
483ad08ca2Schristos #include <unistd.h>
495f11a56cSjtc 
5043fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(tcgetpgrp,_tcgetpgrp)5160549036Smycroft __weak_alias(tcgetpgrp,_tcgetpgrp)
5243fa6fe3Sjtc #endif
5343fa6fe3Sjtc 
545f11a56cSjtc pid_t
55*9e66e6d7Sabs tcgetpgrp(int fd)
565f11a56cSjtc {
575f11a56cSjtc 	int s;
585f11a56cSjtc 
59b48252f3Slukem 	_DIAGASSERT(fd != -1);
60b48252f3Slukem 
615f11a56cSjtc 	if (ioctl(fd, TIOCGPGRP, &s) < 0)
625f11a56cSjtc 		return ((pid_t)-1);
635f11a56cSjtc 
644eaada98Sdsl 	if (s == (pid_t)-1)
654eaada98Sdsl 		/* SVID requires a number > 1 that isn't a valid PGID... */
664eaada98Sdsl 		s = (uint)(pid_t)~0u >> 1;
675f11a56cSjtc 	return ((pid_t)s);
685f11a56cSjtc }
69