xref: /freebsd/lib/libc/gen/ftok.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
1d915a14eSPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3d915a14eSPedro F. Giffuni  *
485c30cfaSJoerg Wunsch  * Copyright (c) 1994 SigmaSoft, Th. Lockert <tholo@sigmasoft.com>
585c30cfaSJoerg Wunsch  * All rights reserved.
685c30cfaSJoerg Wunsch  *
785c30cfaSJoerg Wunsch  * Redistribution and use in source and binary forms, with or without
885c30cfaSJoerg Wunsch  * modification, are permitted provided that the following conditions
985c30cfaSJoerg Wunsch  * are met:
1085c30cfaSJoerg Wunsch  * 1. Redistributions of source code must retain the above copyright
1185c30cfaSJoerg Wunsch  *    notice, this list of conditions and the following disclaimer.
1285c30cfaSJoerg Wunsch  * 2. Redistributions in binary form must reproduce the above copyright
1385c30cfaSJoerg Wunsch  *    notice, this list of conditions and the following disclaimer in the
1485c30cfaSJoerg Wunsch  *    documentation and/or other materials provided with the distribution.
1585c30cfaSJoerg Wunsch  * 3. The name of the author may not be used to endorse or promote products
1685c30cfaSJoerg Wunsch  *    derived from this software without specific prior written permission.
1785c30cfaSJoerg Wunsch  *
1885c30cfaSJoerg Wunsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1985c30cfaSJoerg Wunsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2085c30cfaSJoerg Wunsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2185c30cfaSJoerg Wunsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2285c30cfaSJoerg Wunsch  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2385c30cfaSJoerg Wunsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2485c30cfaSJoerg Wunsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2585c30cfaSJoerg Wunsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2685c30cfaSJoerg Wunsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2785c30cfaSJoerg Wunsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2885c30cfaSJoerg Wunsch  */
2985c30cfaSJoerg Wunsch 
3085c30cfaSJoerg Wunsch #include <sys/types.h>
3185c30cfaSJoerg Wunsch #include <sys/stat.h>
3285c30cfaSJoerg Wunsch #include <sys/ipc.h>
3385c30cfaSJoerg Wunsch 
3485c30cfaSJoerg Wunsch key_t
ftok(const char * path,int id)3555b6b759SCraig Rodrigues ftok(const char *path, int id)
3685c30cfaSJoerg Wunsch {
3785c30cfaSJoerg Wunsch 	struct stat st;
3885c30cfaSJoerg Wunsch 
3985c30cfaSJoerg Wunsch 	if (stat(path, &st) < 0)
4085c30cfaSJoerg Wunsch 		return (key_t)-1;
4185c30cfaSJoerg Wunsch 
42fee1489eSPedro F. Giffuni 	return ((key_t)((unsigned int)id << 24 | (st.st_dev & 0xff) << 16 |
43fee1489eSPedro F. Giffuni 	    (st.st_ino & 0xffff)));
4485c30cfaSJoerg Wunsch }
45