xref: /netbsd-src/lib/libc/gen/ftok.c (revision 21d1b523b3d9a764f4e9213b72dcb71592f29a16)
1*21d1b523Skamil /*	$NetBSD: ftok.c,v 1.12 2018/07/26 00:05:28 kamil Exp $	*/
272c46b1cScgd 
3288ce51dSjtc /*
4288ce51dSjtc  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
5288ce51dSjtc  * All rights reserved.
6288ce51dSjtc  *
7288ce51dSjtc  * Redistribution and use in source and binary forms, with or without
8288ce51dSjtc  * modification, are permitted provided that the following conditions
9288ce51dSjtc  * are met:
10288ce51dSjtc  * 1. Redistributions of source code must retain the above copyright
11288ce51dSjtc  *    notice, this list of conditions and the following disclaimer.
12288ce51dSjtc  * 2. Redistributions in binary form must reproduce the above copyright
13288ce51dSjtc  *    notice, this list of conditions and the following disclaimer in the
14288ce51dSjtc  *    documentation and/or other materials provided with the distribution.
15288ce51dSjtc  *
16288ce51dSjtc  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17288ce51dSjtc  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18288ce51dSjtc  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19288ce51dSjtc  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20288ce51dSjtc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21288ce51dSjtc  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22288ce51dSjtc  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23288ce51dSjtc  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24288ce51dSjtc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25288ce51dSjtc  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26288ce51dSjtc  */
27288ce51dSjtc 
2826cc2d4fSchristos #include <sys/cdefs.h>
29c9f8f01bSjtc #if defined(LIBC_SCCS) && !defined(lint)
30*21d1b523Skamil __RCSID("$NetBSD: ftok.c,v 1.12 2018/07/26 00:05:28 kamil Exp $");
31c9f8f01bSjtc #endif /* LIBC_SCCS and not lint */
32c9f8f01bSjtc 
3343fa6fe3Sjtc #include "namespace.h"
34288ce51dSjtc #include <sys/types.h>
35288ce51dSjtc #include <sys/stat.h>
36288ce51dSjtc #include <sys/ipc.h>
37288ce51dSjtc 
38b48252f3Slukem #include <assert.h>
39b48252f3Slukem #include <errno.h>
40b48252f3Slukem 
4143fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(ftok,_ftok)4260549036Smycroft __weak_alias(ftok,_ftok)
4343fa6fe3Sjtc #endif
4443fa6fe3Sjtc 
45288ce51dSjtc key_t
46cf884af3Smatt ftok(const char *path, int id)
47288ce51dSjtc {
48288ce51dSjtc 	struct stat st;
49288ce51dSjtc 
50b48252f3Slukem 	_DIAGASSERT(path != NULL);
51b48252f3Slukem 
52288ce51dSjtc 	if (stat(path, &st) < 0)
53288ce51dSjtc 		return (key_t)-1;
54288ce51dSjtc 
5572c46b1cScgd 	return (key_t)
56*21d1b523Skamil 	    ((unsigned int)id << 24 | (st.st_dev & 0xff) << 16 |
57*21d1b523Skamil 	     (st.st_ino & 0xffff));
58288ce51dSjtc }
59