nuttx/libs/libc/misc/lib_ftok.c
chao.an 644283c8ff libc/ipc: add ftok(3) support
https://man.openbsd.org/ftok.3

Signed-off-by: chao.an <anchao@xiaomi.com>
2022-06-09 18:19:48 +03:00

70 lines
2.5 KiB
C

/****************************************************************************
* libs/libc/misc/lib_ftok.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ftok
*
* Description:
* Convert a pathname and a project identifier to a System V IPC key.
* The ftok() function uses the identity of the file named by the given
* pathname (which must refer to an existing, accessible file) and the
* least significant 8 bits of proj_id (which must be nonzero) to
* generate a key_t type System V IPC key, suitable for use with
* msgget(2), semget(2), or shmget(2).
*
* Input Parameters:
* pathname - identity of the file name
* proj_id - The value that uniquely project identifies.
*
* Returned Value:
* On success, the generated key_t value is returned.
* On failure -1 is returned, with errno indicating the error as for the
* stat(2) system call.
*
****************************************************************************/
key_t ftok(FAR const char *pathname, int proj_id)
{
struct stat st;
if (stat(pathname, &st) < 0)
{
return (key_t)-1;
}
return ((key_t)proj_id << 24 |
(key_t)(st.st_dev & 0xff) << 16 |
(key_t)(st.st_ino & 0xffff));
}