1 /* $NetBSD: balpar.c,v 1.2 2017/02/14 01:16:49 christos Exp $ */ 2 3 /*++ 4 /* NAME 5 /* balpar 3 6 /* SUMMARY 7 /* determine length of string in parentheses 8 /* SYNOPSIS 9 /* #include <stringops.h> 10 /* 11 /* size_t balpar(string, parens) 12 /* const char *string; 13 /* const char *parens; 14 /* DESCRIPTION 15 /* balpar() determines the length of a string enclosed in 16 /* the specified parentheses, zero in case of error. 17 /* SEE ALSO 18 /* A balpar() routine appears in Brian W. Kernighan, P.J. Plauger: 19 /* "Software Tools", Addison-Wesley 1976. This function is different. 20 /* LICENSE 21 /* .ad 22 /* .fi 23 /* The Secure Mailer license must be distributed with this software. 24 /* AUTHOR(S) 25 /* Wietse Venema 26 /* IBM T.J. Watson Research 27 /* P.O. Box 704 28 /* Yorktown Heights, NY 10598, USA 29 /*--*/ 30 31 /* System library. */ 32 33 #include <sys_defs.h> 34 35 /* Utility library. */ 36 37 #include <stringops.h> 38 39 /* balpar - return length of {text} */ 40 41 size_t balpar(const char *string, const char *parens) 42 { 43 const char *cp; 44 int level; 45 int ch; 46 47 if (*string != parens[0]) 48 return (0); 49 for (level = 1, cp = string + 1; (ch = *cp) != 0; cp++) { 50 if (ch == parens[1]) { 51 if (--level == 0) 52 return (cp - string + 1); 53 } else if (ch == parens[0]) { 54 level++; 55 } 56 } 57 return (0); 58 } 59