xref: /dflybsd-src/contrib/ncurses/progs/transform.c (revision 5f4613f264219ca3409bd9bba6ca51d4c493c16a)
1*5f4613f2SJohn Marino /****************************************************************************
2*5f4613f2SJohn Marino  * Copyright (c) 2009,2010 Free Software Foundation, Inc.                   *
3*5f4613f2SJohn Marino  *                                                                          *
4*5f4613f2SJohn Marino  * Permission is hereby granted, free of charge, to any person obtaining a  *
5*5f4613f2SJohn Marino  * copy of this software and associated documentation files (the            *
6*5f4613f2SJohn Marino  * "Software"), to deal in the Software without restriction, including      *
7*5f4613f2SJohn Marino  * without limitation the rights to use, copy, modify, merge, publish,      *
8*5f4613f2SJohn Marino  * distribute, distribute with modifications, sublicense, and/or sell       *
9*5f4613f2SJohn Marino  * copies of the Software, and to permit persons to whom the Software is    *
10*5f4613f2SJohn Marino  * furnished to do so, subject to the following conditions:                 *
11*5f4613f2SJohn Marino  *                                                                          *
12*5f4613f2SJohn Marino  * The above copyright notice and this permission notice shall be included  *
13*5f4613f2SJohn Marino  * in all copies or substantial portions of the Software.                   *
14*5f4613f2SJohn Marino  *                                                                          *
15*5f4613f2SJohn Marino  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16*5f4613f2SJohn Marino  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17*5f4613f2SJohn Marino  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18*5f4613f2SJohn Marino  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19*5f4613f2SJohn Marino  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20*5f4613f2SJohn Marino  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21*5f4613f2SJohn Marino  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22*5f4613f2SJohn Marino  *                                                                          *
23*5f4613f2SJohn Marino  * Except as contained in this notice, the name(s) of the above copyright   *
24*5f4613f2SJohn Marino  * holders shall not be used in advertising or otherwise to promote the     *
25*5f4613f2SJohn Marino  * sale, use or other dealings in this Software without prior written       *
26*5f4613f2SJohn Marino  * authorization.                                                           *
27*5f4613f2SJohn Marino  ****************************************************************************/
28*5f4613f2SJohn Marino 
29*5f4613f2SJohn Marino /****************************************************************************
30*5f4613f2SJohn Marino  *  Author: Thomas E. Dickey                                                *
31*5f4613f2SJohn Marino  ****************************************************************************/
32*5f4613f2SJohn Marino #include <progs.priv.h>
33*5f4613f2SJohn Marino #include <string.h>
34*5f4613f2SJohn Marino 
35*5f4613f2SJohn Marino #include <transform.h>
36*5f4613f2SJohn Marino 
37*5f4613f2SJohn Marino MODULE_ID("$Id: transform.c,v 1.2 2010/09/04 21:16:17 tom Exp $")
38*5f4613f2SJohn Marino 
39*5f4613f2SJohn Marino #ifdef SUFFIX_IGNORED
40*5f4613f2SJohn Marino static void
41*5f4613f2SJohn Marino trim_suffix(const char *a, unsigned *len)
42*5f4613f2SJohn Marino {
43*5f4613f2SJohn Marino     const char ignore[] = SUFFIX_IGNORED;
44*5f4613f2SJohn Marino 
45*5f4613f2SJohn Marino     if (sizeof(ignore) != 0) {
46*5f4613f2SJohn Marino 	bool trim = FALSE;
47*5f4613f2SJohn Marino 	unsigned need = (sizeof(ignore) - 1);
48*5f4613f2SJohn Marino 
49*5f4613f2SJohn Marino 	if (*len > need) {
50*5f4613f2SJohn Marino 	    unsigned first = *len - need;
51*5f4613f2SJohn Marino 	    unsigned n;
52*5f4613f2SJohn Marino 	    trim = TRUE;
53*5f4613f2SJohn Marino 	    for (n = first; n < *len; ++n) {
54*5f4613f2SJohn Marino 		if (tolower(UChar(a[n])) != tolower(UChar(ignore[n - first]))) {
55*5f4613f2SJohn Marino 		    trim = FALSE;
56*5f4613f2SJohn Marino 		    break;
57*5f4613f2SJohn Marino 		}
58*5f4613f2SJohn Marino 	    }
59*5f4613f2SJohn Marino 	    if (trim) {
60*5f4613f2SJohn Marino 		*len -= need;
61*5f4613f2SJohn Marino 	    }
62*5f4613f2SJohn Marino 	}
63*5f4613f2SJohn Marino     }
64*5f4613f2SJohn Marino }
65*5f4613f2SJohn Marino #else
66*5f4613f2SJohn Marino #define trim_suffix(a, len)	/* nothing */
67*5f4613f2SJohn Marino #endif
68*5f4613f2SJohn Marino 
69*5f4613f2SJohn Marino bool
70*5f4613f2SJohn Marino same_program(const char *a, const char *b)
71*5f4613f2SJohn Marino {
72*5f4613f2SJohn Marino     unsigned len_a = strlen(a);
73*5f4613f2SJohn Marino     unsigned len_b = strlen(b);
74*5f4613f2SJohn Marino 
75*5f4613f2SJohn Marino     trim_suffix(a, &len_a);
76*5f4613f2SJohn Marino     trim_suffix(b, &len_b);
77*5f4613f2SJohn Marino 
78*5f4613f2SJohn Marino     return (len_a == len_b) && (strncmp(a, b, len_a) == 0);
79*5f4613f2SJohn Marino }
80