This is an automated email from the ASF dual-hosted git repository.

pkarashchenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 644283c8fff7c1c6262d2ec41dc7cfb3fed9e839
Author: chao.an <anc...@xiaomi.com>
AuthorDate: Mon May 16 11:25:37 2022 +0800

    libc/ipc: add ftok(3) support
    
    https://man.openbsd.org/ftok.3
    
    Signed-off-by: chao.an <anc...@xiaomi.com>
---
 include/sys/ipc.h         | 24 ++++++++++++++++-
 include/sys/types.h       |  2 +-
 libs/libc/misc/Make.defs  |  2 +-
 libs/libc/misc/lib_ftok.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/include/sys/ipc.h b/include/sys/ipc.h
index 6c6d3f953a..919b767645 100644
--- a/include/sys/ipc.h
+++ b/include/sys/ipc.h
@@ -88,7 +88,29 @@ struct ipc_perm
  * Public Function Prototypes
  ****************************************************************************/
 
-key_t ftok(FAR const char *path, int id);
+/****************************************************************************
+ * 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);
 
 #undef EXTERN
 #if defined(__cplusplus)
diff --git a/include/sys/types.h b/include/sys/types.h
index 9d5952e5e9..475e59321b 100644
--- a/include/sys/types.h
+++ b/include/sys/types.h
@@ -158,7 +158,7 @@ typedef int          id_t;
  * semaphores. A key is simply an integer of type key_t
  */
 
-typedef int16_t      key_t;
+typedef int32_t      key_t;
 
 /* Signed integral type of the result of subtracting two pointers */
 
diff --git a/libs/libc/misc/Make.defs b/libs/libc/misc/Make.defs
index 0a73af711d..405b9b40fe 100644
--- a/libs/libc/misc/Make.defs
+++ b/libs/libc/misc/Make.defs
@@ -37,7 +37,7 @@ endif
 
 CSRCS += lib_dumpbuffer.c lib_dumpvbuffer.c lib_fnmatch.c lib_debug.c
 CSRCS += lib_crc64.c lib_crc32.c lib_crc16.c lib_crc8.c lib_crc8ccitt.c
-CSRCS += lib_crc8table.c lib_glob.c lib_execinfo.c
+CSRCS += lib_crc8table.c lib_glob.c lib_execinfo.c lib_ftok.c
 
 # Keyboard driver encoder/decoder
 
diff --git a/libs/libc/misc/lib_ftok.c b/libs/libc/misc/lib_ftok.c
new file mode 100644
index 0000000000..6d38ed62b1
--- /dev/null
+++ b/libs/libc/misc/lib_ftok.c
@@ -0,0 +1,69 @@
+/****************************************************************************
+ * 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));
+}

Reply via email to