xref: /minix3/lib/libc/gen/ftok.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: ftok.c,v 1.11 2012/03/20 16:36:05 matt Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  *
162fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172fe8fb19SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192fe8fb19SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
202fe8fb19SBen Gras  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
212fe8fb19SBen Gras  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
222fe8fb19SBen Gras  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
232fe8fb19SBen Gras  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
242fe8fb19SBen Gras  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
252fe8fb19SBen Gras  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262fe8fb19SBen Gras  */
272fe8fb19SBen Gras 
282fe8fb19SBen Gras #include <sys/cdefs.h>
292fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
30*f14fb602SLionel Sambuc __RCSID("$NetBSD: ftok.c,v 1.11 2012/03/20 16:36:05 matt Exp $");
312fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
322fe8fb19SBen Gras 
332fe8fb19SBen Gras #include "namespace.h"
342fe8fb19SBen Gras #include <sys/types.h>
352fe8fb19SBen Gras #include <sys/stat.h>
362fe8fb19SBen Gras #include <sys/ipc.h>
372fe8fb19SBen Gras 
382fe8fb19SBen Gras #include <assert.h>
392fe8fb19SBen Gras #include <errno.h>
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(ftok,_ftok)422fe8fb19SBen Gras __weak_alias(ftok,_ftok)
432fe8fb19SBen Gras #endif
442fe8fb19SBen Gras 
452fe8fb19SBen Gras key_t
46*f14fb602SLionel Sambuc ftok(const char *path, int id)
472fe8fb19SBen Gras {
482fe8fb19SBen Gras 	struct stat st;
492fe8fb19SBen Gras 
502fe8fb19SBen Gras 	_DIAGASSERT(path != NULL);
512fe8fb19SBen Gras 
522fe8fb19SBen Gras 	if (stat(path, &st) < 0)
532fe8fb19SBen Gras 		return (key_t)-1;
542fe8fb19SBen Gras 
552fe8fb19SBen Gras 	return (key_t)
562fe8fb19SBen Gras 	    (id << 24 | (st.st_dev & 0xff) << 16 | (st.st_ino & 0xffff));
572fe8fb19SBen Gras }
58