1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 /* clang-format off */
19 
20 /*	rindex3f.c - Implements LIB3F rindex subprogram.  */
21 
22 #include "ent3f.h"
23 
ENT3F(RINDEX,rindex)24 int ENT3F(RINDEX, rindex)(DCHAR(a1), DCHAR(a2) DCLEN(a1) DCLEN(a2))
25 {
26   char *a1 = CADR(a1); /* pointer to string being searched */
27   char *a2 = CADR(a2); /* pointer to string being searched for */
28   int a1_l = CLEN(a1); /* length of a1 */
29   int a2_l = CLEN(a2); /* length of a2 */
30   int i1, i2, match;
31 
32   for (i1 = a1_l - a2_l; i1 >= 0; i1--) {
33     match = 1;
34     for (i2 = 0; i2 < a2_l; i2++) {
35       if (a1[i1 + i2] != a2[i2]) {
36         match = 0;
37         break;
38       }
39     }
40     if (match)
41       return (i1 + 1);
42   }
43   return (0);
44 }
45