Release 4.1
[unix-history] / usr / src / usr.bin / tn3270 / general / genbsubs.c
CommitLineData
535751f3 1/*
584449f4
KB
2 * Copyright (c) 1988 Regents of the University of California.
3 * All rights reserved.
535751f3 4 *
584449f4 5 * Redistribution and use in source and binary forms are permitted
b36fc510
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
535751f3
GM
16 */
17
584449f4 18#ifndef lint
d43d1075 19static char sccsid[] = "@(#)genbsubs.c 4.1 (Berkeley) %G%";
584449f4 20#endif /* not lint */
535751f3
GM
21
22/* The output of bunequal is the offset of the byte which didn't match;
23 * if all the bytes match, then we return n.
24 * bunequal(s1, s2, n) */
25
26int
27bunequal(s1, s2, n)
28register char *s1, *s2;
29register n;
30{
31 register int i = 0;
32
33 while (i++ < n) {
34 if (*s1++ != *s2++) {
35 break;
36 }
37 }
38 return(i-1);
39}
40
41/* bskip(s1, n, b) : finds the first occurrence of any byte != 'b' in the 'n'
42 * bytes beginning at 's1'.
43 */
44
45int
46bskip(s1, n, b)
47register char *s1;
48register int n;
49register int b;
50{
51 register int i = 0;
52
53 while (i++ < n) {
54 if (*s1++ != b) {
55 break;
56 }
57 }
58 return(i-1);
59}
eb8f58d6
GM
60
61/*
a3e38099 62 * memNSchr(const void *s, int c, size_t n, int and)
eb8f58d6
GM
63 *
64 * Like memchr, but the comparison is '((*s)&and) == c',
65 * and we increment our way through s by "stride" ('s += stride').
66 *
67 * We optimize for the most used strides of +1 and -1.
68 */
69
70unsigned char *
71memNSchr(s, c, n, and, stride)
72char *s;
73int c;
74unsigned int n;
75int and;
76int stride;
77{
78 register unsigned char _c, *_s, _and;
79
80 _and = and;
81 _c = (c&_and);
82 _s = (unsigned char *)s;
83 switch (stride) {
84 case 1:
85 while (n--) {
86 if (((*_s)&_and) == _c) {
87 return _s;
88 }
89 _s++;
90 }
91 break;
92 case -1:
93 while (n--) {
94 if (((*_s)&_and) == _c) {
95 return _s;
96 }
97 _s--;
98 }
99 break;
100 default:
101 while (n--) {
102 if (((*_s)&_and) == _c) {
103 return _s;
104 }
105 _s += stride;
106 }
107 }
108 return 0;
109}