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