1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** POSIX wrapper for THREADX                                             */
16 /**                                                                       */
17 /**                                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /* Include necessary system files.  */
23 
24 #include "tx_api.h"     /* Threadx API */
25 #include "pthread.h"    /* Posix API */
26 #include "px_int.h"     /* Posix helper functions */
27 
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    posix_find_sem                                      PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    This routine returns sem descriptor indicating that name of         */
42 /*    in the semaphore exists.                                            */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    const char * name        Name of the semaphore.                     */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    sem                      If successful                              */
51 /*    ERROR                    If fails                                   */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*     None                                                               */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    POSIX internal Code                                                 */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  06-02-2021      William E. Lamie        Initial Version 6.1.7         */
66 /*  08-02-2021      Scott Larson            Removed unneeded semicolon,   */
67 /*                                            resulting in version 6.1.8  */
68 /*                                                                        */
69 /**************************************************************************/
posix_find_sem(const CHAR * name)70 sem_t* posix_find_sem(const CHAR * name)
71 {
72 
73 sem_t                  *sem;
74 ULONG                   index;
75 ULONG                   match;
76 CHAR                   *dummy_name;
77 CHAR                   *dummy_sem_name;
78 ULONG                   namelength;
79 
80     /* For checking the name.  */
81     for(index = 0,sem = posix_sem_pool;index < SEM_NSEMS_MAX;index ++,sem ++)
82     {
83         /* Assume the worst case.  */
84         match = TX_FALSE;
85 
86         dummy_sem_name = sem->sem_name;
87 
88         for(namelength = 0 ,dummy_name = (CHAR *)name ; namelength < 10 ;
89                   namelength ++, dummy_name++,dummy_sem_name ++)
90         {
91             /* Copy name.  */
92             if(* dummy_name == * dummy_sem_name)
93             {
94                 /* End of the string.  */
95                 if(* dummy_name == '\0')
96                 {
97                     match = TX_TRUE;
98                     break;
99                 }
100             }/*  Copy name.  */
101             else
102                 break;
103         }
104 
105         /* Stop searching.  */
106         if ( match == TX_TRUE)
107         {
108             break;
109         }
110     }/* For each semaphore.  */
111     if(match == TX_TRUE)
112     {
113         return(sem);
114     }
115 
116     /* return NULL.  */
117     sem = NULL;
118     return(sem);
119 }
120