1 /* Part of publib.
2 
3    Copyright (c) 1994-2006 Lars Wirzenius.  All rights reserved.
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    1. Redistributions of source code must retain the above copyright
10       notice, this list of conditions and the following disclaimer.
11 
12    2. Redistributions in binary form must reproduce the above
13       copyright notice, this list of conditions and the following
14       disclaimer in the documentation and/or other materials provided
15       with the distribution.
16 
17    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18    OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20    ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29 /*
30  * strendzap.c -- remove pat from end of str, if it is there
31  *
32  * Part of publib.  See man page for more information
33  * "@(#)publib-strutil:$Id: strendzap.c,v 1.2 1994/07/22 12:22:51 liw Exp $"
34  */
35 
36 #include <assert.h>
37 #include <string.h>
38 #include "publib/strutil.h"
39 
strendzap(char * str,const char * pat)40 int strendzap(char *str, const char *pat) {
41 	size_t len, patlen;
42 
43 	assert(str != NULL);
44 	assert(pat != NULL);
45 
46 	len = strlen(str);
47 	patlen = strlen(pat);
48 
49 	if (patlen <= len) {
50 		str += len - patlen;
51 		if (strcmp(str, pat) == 0) {
52 			*str = '\0';
53 			return 1;
54 		}
55 	}
56 	return 0;
57 }
58