xref: /dflybsd-src/contrib/ncurses/progs/transform.c (revision 0cadad7e49c6219b0de0675ef6a6f44683d177d4)
15f4613f2SJohn Marino /****************************************************************************
2*32bb5217SDaniel Fojt  * Copyright 2020 Thomas E. Dickey                                          *
3*32bb5217SDaniel Fojt  * Copyright 2009-2010,2011 Free Software Foundation, Inc.                  *
45f4613f2SJohn Marino  *                                                                          *
55f4613f2SJohn Marino  * Permission is hereby granted, free of charge, to any person obtaining a  *
65f4613f2SJohn Marino  * copy of this software and associated documentation files (the            *
75f4613f2SJohn Marino  * "Software"), to deal in the Software without restriction, including      *
85f4613f2SJohn Marino  * without limitation the rights to use, copy, modify, merge, publish,      *
95f4613f2SJohn Marino  * distribute, distribute with modifications, sublicense, and/or sell       *
105f4613f2SJohn Marino  * copies of the Software, and to permit persons to whom the Software is    *
115f4613f2SJohn Marino  * furnished to do so, subject to the following conditions:                 *
125f4613f2SJohn Marino  *                                                                          *
135f4613f2SJohn Marino  * The above copyright notice and this permission notice shall be included  *
145f4613f2SJohn Marino  * in all copies or substantial portions of the Software.                   *
155f4613f2SJohn Marino  *                                                                          *
165f4613f2SJohn Marino  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
175f4613f2SJohn Marino  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
185f4613f2SJohn Marino  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
195f4613f2SJohn Marino  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
205f4613f2SJohn Marino  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
215f4613f2SJohn Marino  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
225f4613f2SJohn Marino  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
235f4613f2SJohn Marino  *                                                                          *
245f4613f2SJohn Marino  * Except as contained in this notice, the name(s) of the above copyright   *
255f4613f2SJohn Marino  * holders shall not be used in advertising or otherwise to promote the     *
265f4613f2SJohn Marino  * sale, use or other dealings in this Software without prior written       *
275f4613f2SJohn Marino  * authorization.                                                           *
285f4613f2SJohn Marino  ****************************************************************************/
295f4613f2SJohn Marino 
305f4613f2SJohn Marino /****************************************************************************
315f4613f2SJohn Marino  *  Author: Thomas E. Dickey                                                *
325f4613f2SJohn Marino  ****************************************************************************/
335f4613f2SJohn Marino #include <progs.priv.h>
345f4613f2SJohn Marino #include <string.h>
355f4613f2SJohn Marino 
365f4613f2SJohn Marino #include <transform.h>
375f4613f2SJohn Marino 
38*32bb5217SDaniel Fojt MODULE_ID("$Id: transform.c,v 1.4 2020/02/02 23:34:34 tom Exp $")
395f4613f2SJohn Marino 
405f4613f2SJohn Marino #ifdef SUFFIX_IGNORED
415f4613f2SJohn Marino static void
trim_suffix(const char * a,size_t * len)423468e90cSJohn Marino trim_suffix(const char *a, size_t *len)
435f4613f2SJohn Marino {
445f4613f2SJohn Marino     const char ignore[] = SUFFIX_IGNORED;
455f4613f2SJohn Marino 
465f4613f2SJohn Marino     if (sizeof(ignore) != 0) {
475f4613f2SJohn Marino 	bool trim = FALSE;
483468e90cSJohn Marino 	size_t need = (sizeof(ignore) - 1);
495f4613f2SJohn Marino 
505f4613f2SJohn Marino 	if (*len > need) {
513468e90cSJohn Marino 	    size_t first = *len - need;
523468e90cSJohn Marino 	    size_t n;
535f4613f2SJohn Marino 	    trim = TRUE;
545f4613f2SJohn Marino 	    for (n = first; n < *len; ++n) {
555f4613f2SJohn Marino 		if (tolower(UChar(a[n])) != tolower(UChar(ignore[n - first]))) {
565f4613f2SJohn Marino 		    trim = FALSE;
575f4613f2SJohn Marino 		    break;
585f4613f2SJohn Marino 		}
595f4613f2SJohn Marino 	    }
605f4613f2SJohn Marino 	    if (trim) {
615f4613f2SJohn Marino 		*len -= need;
625f4613f2SJohn Marino 	    }
635f4613f2SJohn Marino 	}
645f4613f2SJohn Marino     }
655f4613f2SJohn Marino }
665f4613f2SJohn Marino #else
675f4613f2SJohn Marino #define trim_suffix(a, len)	/* nothing */
685f4613f2SJohn Marino #endif
695f4613f2SJohn Marino 
705f4613f2SJohn Marino bool
same_program(const char * a,const char * b)715f4613f2SJohn Marino same_program(const char *a, const char *b)
725f4613f2SJohn Marino {
733468e90cSJohn Marino     size_t len_a = strlen(a);
743468e90cSJohn Marino     size_t len_b = strlen(b);
755f4613f2SJohn Marino 
765f4613f2SJohn Marino     trim_suffix(a, &len_a);
775f4613f2SJohn Marino     trim_suffix(b, &len_b);
785f4613f2SJohn Marino 
795f4613f2SJohn Marino     return (len_a == len_b) && (strncmp(a, b, len_a) == 0);
805f4613f2SJohn Marino }
81