2 * regcomp and regexec -- regsub and regerror are elsewhere
4 * Copyright (c) 1986 by University of Toronto.
5 * Written by Henry Spencer. Not derived from licensed software.
7 * Permission is granted to anyone to use this software for any
8 * purpose on any computer system, and to redistribute it freely,
9 * subject to the following restrictions:
11 * 1. The author is not responsible for the consequences of use of
12 * this software, no matter how awful, even if they arise
15 * 2. The origin of this software must not be misrepresented, either
16 * by explicit claim or by omission.
18 * 3. Altered versions must be plainly marked as such, and must not
19 * be misrepresented as being the original software.
21 * Beware that some of this code is subtly aware of the way operator
22 * precedence is structured in regular expressions. Serious changes in
23 * regular-expression syntax might require a total rethink.
40 * The "internal use only" fields in regexp.h are present to pass info from
41 * compile to execute that permits the execute phase to run lots faster on
42 * simple cases. They are:
44 * regstart char that must begin a match; '\0' if none obvious
45 * reganch is the match anchored (at beginning-of-line only)?
46 * regmust string (pointer into program) that match must include, or NULL
47 * regmlen length of regmust string
49 * Regstart and reganch permit very fast decisions on suitable starting points
50 * for a match, cutting down the work a lot. Regmust permits fast rejection
51 * of lines that cannot possibly match. The regmust tests are costly enough
52 * that regcomp() supplies a regmust only if the r.e. contains something
53 * potentially expensive (at present, the only such thing detected is * or +
54 * at the start of the r.e., which can involve a lot of backup). Regmlen is
55 * supplied because the test in regexec() needs it and regcomp() is computing
60 * Structure for regexp "program". This is essentially a linear encoding
61 * of a nondeterministic finite-state machine (aka syntax charts or
62 * "railroad normal form" in parsing technology). Each node is an opcode
63 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
64 * all nodes except BRANCH implement concatenation; a "next" pointer with
65 * a BRANCH on both ends of it is connecting two alternatives. (Here we
66 * have one of the subtle syntax dependencies: an individual BRANCH (as
67 * opposed to a collection of them) is never concatenated with anything
68 * because of operator precedence.) The operand of some types of node is
69 * a literal string; for others, it is a node leading into a sub-FSM. In
70 * particular, the operand of a BRANCH node is the first node of the branch.
71 * (NB this is *not* a tree structure: the tail of the branch connects
72 * to the thing following the set of BRANCHes.) The opcodes are:
75 /* definition number opnd? meaning */
76 #define END 0 /* no End of program. */
77 #define BOL 1 /* no Match "" at beginning of line. */
78 #define EOL 2 /* no Match "" at end of line. */
79 #define ANY 3 /* no Match any one character. */
80 #define ANYOF 4 /* str Match any character in this string. */
81 #define ANYBUT 5 /* str Match any character not in this string. */
82 #define BRANCH 6 /* node Match this alternative, or the next... */
83 #define BACK 7 /* no Match "", "next" ptr points backward. */
84 #define EXACTLY 8 /* str Match this string. */
85 #define NOTHING 9 /* no Match empty string. */
86 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
87 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
88 #define OPEN 20 /* no Mark this point in input as start of #n. */
89 /* OPEN+1 is number 1, etc. */
90 #define CLOSE 30 /* no Analogous to OPEN. */
95 * BRANCH The set of branches constituting a single choice are hooked
96 * together with their "next" pointers, since precedence prevents
97 * anything being concatenated to any individual branch. The
98 * "next" pointer of the last BRANCH in a choice points to the
99 * thing following the whole choice. This is also where the
100 * final "next" pointer of each individual branch points; each
101 * branch starts with the operand node of a BRANCH node.
103 * BACK Normal "next" pointers all implicitly point forward; BACK
104 * exists to make loop structures possible.
106 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
107 * BRANCH structures using BACK. Simple cases (one character
108 * per match) are implemented with STAR and PLUS for speed
109 * and to minimize recursive plunges.
111 * OPEN,CLOSE ...are numbered at compile time.
115 * A node is one char of opcode followed by two chars of "next" pointer.
116 * "Next" pointers are stored as two 8-bit pieces, high order first. The
117 * value is a positive offset from the opcode of the node containing it.
118 * An operand, if any, simply follows the node. (Note that much of the
119 * code generation knows about this implicit relationship.)
121 * Using two bytes for the "next" pointer is vast overkill for most things,
122 * but allows patterns to get big without disasters.
125 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
126 #define OPERAND(p) ((p) + 3)
129 * See regmagic.h for one further detail of program structure.
134 * Utility definitions.
137 #define UCHARAT(p) ((int)*(unsigned char *)(p))
139 #define UCHARAT(p) ((int)*(p)&CHARBITS)
142 #define FAIL(m) { regerror(m); return(NULL); }
143 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
144 #define META "^$.[()|?+*\\"
147 * Flags to be passed up and down.
149 #define HASWIDTH 01 /* Known never to match null string. */
150 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
151 #define SPSTART 04 /* Starts with * or +. */
152 #define WORST 0 /* Worst case. */
155 * Global work variables for regcomp().
158 STATIC
char *regparse
; /* Input-scan pointer. */
159 STATIC
int regnpar
; /* () count. */
160 STATIC
unsigned char regdummy
;
161 STATIC
unsigned char *regcode
; /* Code-emit pointer; ®dummy = don't. */
162 STATIC
long regsize
; /* Code size. */
165 * Forward declarations for regcomp()'s friends.
167 STATIC
unsigned char *reg(int paren
, int *flagp
);
168 STATIC
unsigned char *regbranch(int *flagp
);
169 STATIC
unsigned char *regpiece(int *flagp
);
170 STATIC
unsigned char *regatom(int *flagp
);
171 STATIC
unsigned char *regnode(int op
);
172 STATIC
void regc(int b
);
173 STATIC
void reginsert(int op
, unsigned char *opnd
);
174 STATIC
void regtail(unsigned char *p
, unsigned char *val
);
175 STATIC
void regoptail(unsigned char *p
, unsigned char *val
);
176 STATIC
int regtry(regexp
*prog
, unsigned char *string
);
177 STATIC
int regmatch(unsigned char *prog
);
178 STATIC
int regrepeat(unsigned char *p
);
179 STATIC
unsigned char *regnext(unsigned char *p
);
180 STATIC
unsigned char *regprop(unsigned char *op
);
183 STATIC
int strcspn(unsigned char *s1
, unsigned char *s2
);
187 - regcomp - compile a regular expression into internal code
189 * We can't allocate space until we know how big the compiled form will be,
190 * but we can't compile it (and thus know how big it is) until we've got a
191 * place to put the code. So we cheat: we compile it twice, once with code
192 * generation turned off and size counting turned on, and once "for real".
193 * This also means that we don't allocate space until we are sure that the
194 * thing really will compile successfully, and we never have to move the
195 * code and thus invalidate pointers into it. (Note that it has to be in
196 * one piece because free() must be able to free it all.)
198 * Beware that the optimization-preparation code in here knows about some
199 * of the structure of the compiled regexp.
206 register unsigned char *scan
;
207 register unsigned char *longest
;
212 FAIL("NULL argument");
214 /* First pass: determine size, legality. */
220 if (reg(0, &flags
) == NULL
)
223 /* Small enough for pointer-storage convention? */
224 if (regsize
>= 32767L) /* Probably could be 65535L. */
225 FAIL("regexp too big");
227 /* Allocate space. */
228 r
= (regexp
*)malloc(sizeof(regexp
) + (unsigned)regsize
);
230 FAIL("out of space");
232 /* Second pass: emit code. */
235 regcode
= r
->program
;
237 if (reg(0, &flags
) == NULL
)
240 /* Dig out information for optimizations. */
241 r
->regstart
= '\0'; /* Worst-case defaults. */
245 scan
= r
->program
+1; /* First BRANCH. */
246 if (OP(regnext(scan
)) == END
) { /* Only one top-level choice. */
247 scan
= OPERAND(scan
);
249 /* Starting-point info. */
250 if (OP(scan
) == EXACTLY
)
251 r
->regstart
= *OPERAND(scan
);
252 else if (OP(scan
) == BOL
)
256 * If there's something expensive in the r.e., find the
257 * longest literal string that must appear and make it the
258 * regmust. Resolve ties in favor of later strings, since
259 * the regstart check works with the beginning of the r.e.
260 * and avoiding duplication strengthens checking. Not a
261 * strong reason, but sufficient in the absence of others.
266 for (; scan
!= NULL
; scan
= regnext(scan
))
267 if (OP(scan
) == EXACTLY
268 && strlen((char *)OPERAND(scan
)) >= len
) {
269 longest
= OPERAND(scan
);
270 len
= strlen((char *)OPERAND(scan
));
272 r
->regmust
= longest
;
281 - reg - regular expression, i.e. main body or parenthesized thing
283 * Caller must absorb opening parenthesis.
285 * Combining parenthesis handling with the base level of regular expression
286 * is a trifle forced, but the need to tie the tails of the branches to what
287 * follows makes it hard to avoid.
289 STATIC
unsigned char *
291 int paren
; /* Parenthesized? */
294 register unsigned char *ret
;
295 register unsigned char *br
;
296 register unsigned char *ender
;
300 *flagp
= HASWIDTH
; /* Tentatively. */
302 /* Make an OPEN node, if parenthesized. */
304 if (regnpar
>= NSUBEXP
)
308 ret
= regnode(OPEN
+parno
);
312 /* Pick up the branches, linking them together. */
313 br
= regbranch(&flags
);
317 regtail(ret
, br
); /* OPEN -> first. */
320 if (!(flags
&HASWIDTH
))
322 *flagp
|= flags
&SPSTART
;
323 while (*regparse
== '|') {
325 br
= regbranch(&flags
);
328 regtail(ret
, br
); /* BRANCH -> BRANCH. */
329 if (!(flags
&HASWIDTH
))
331 *flagp
|= flags
&SPSTART
;
334 /* Make a closing node, and hook it on the end. */
335 ender
= regnode((paren
) ? CLOSE
+parno
: END
);
338 /* Hook the tails of the branches to the closing node. */
339 for (br
= ret
; br
!= NULL
; br
= regnext(br
))
340 regoptail(br
, ender
);
342 /* Check for proper termination. */
343 if (paren
&& *regparse
++ != ')') {
344 FAIL("unmatched ()");
345 } else if (!paren
&& *regparse
!= '\0') {
346 if (*regparse
== ')') {
347 FAIL("unmatched ()");
349 FAIL("junk on end"); /* "Can't happen". */
357 - regbranch - one alternative of an | operator
359 * Implements the concatenation operator.
361 STATIC
unsigned char *
365 register unsigned char *ret
;
366 register unsigned char *chain
;
367 register unsigned char *latest
;
370 *flagp
= WORST
; /* Tentatively. */
372 ret
= regnode(BRANCH
);
374 while (*regparse
!= '\0' && *regparse
!= '|' && *regparse
!= ')') {
375 latest
= regpiece(&flags
);
378 *flagp
|= flags
&HASWIDTH
;
379 if (chain
== NULL
) /* First piece. */
380 *flagp
|= flags
&SPSTART
;
382 regtail(chain
, latest
);
385 if (chain
== NULL
) /* Loop ran zero times. */
386 (void) regnode(NOTHING
);
392 - regpiece - something followed by possible [*+?]
394 * Note that the branching code sequences used for ? and the general cases
395 * of * and + are somewhat optimized: they use the same NOTHING node as
396 * both the endmarker for their branch list and the body of the last branch.
397 * It might seem that this node could be dispensed with entirely, but the
398 * endmarker role is not redundant.
400 STATIC
unsigned char *
404 register unsigned char *ret
;
405 register unsigned char op
;
406 register unsigned char *next
;
409 ret
= regatom(&flags
);
419 if (!(flags
&HASWIDTH
) && op
!= '?')
420 FAIL("*+ operand could be empty");
421 *flagp
= (op
!= '+') ? (WORST
|SPSTART
) : (WORST
|HASWIDTH
);
423 if (op
== '*' && (flags
&SIMPLE
))
424 reginsert(STAR
, ret
);
425 else if (op
== '*') {
426 /* Emit x* as (x&|), where & means "self". */
427 reginsert(BRANCH
, ret
); /* Either x */
428 regoptail(ret
, regnode(BACK
)); /* and loop */
429 regoptail(ret
, ret
); /* back */
430 regtail(ret
, regnode(BRANCH
)); /* or */
431 regtail(ret
, regnode(NOTHING
)); /* null. */
432 } else if (op
== '+' && (flags
&SIMPLE
))
433 reginsert(PLUS
, ret
);
434 else if (op
== '+') {
435 /* Emit x+ as x(&|), where & means "self". */
436 next
= regnode(BRANCH
); /* Either */
438 regtail(regnode(BACK
), ret
); /* loop back */
439 regtail(next
, regnode(BRANCH
)); /* or */
440 regtail(ret
, regnode(NOTHING
)); /* null. */
441 } else if (op
== '?') {
442 /* Emit x? as (x|) */
443 reginsert(BRANCH
, ret
); /* Either x */
444 regtail(ret
, regnode(BRANCH
)); /* or */
445 next
= regnode(NOTHING
); /* null. */
447 regoptail(ret
, next
);
450 if (ISMULT(*regparse
))
457 - regatom - the lowest level
459 * Optimization: gobbles an entire sequence of ordinary characters so that
460 * it can turn them into a single node, which is smaller to store and
461 * faster to run. Backslashed characters are exceptions, each becoming a
462 * separate node; the code is simpler that way and it's not worth fixing.
464 STATIC
unsigned char *
468 register unsigned char *ret
;
471 *flagp
= WORST
; /* Tentatively. */
473 switch (*regparse
++) {
482 *flagp
|= HASWIDTH
|SIMPLE
;
486 register int classend
;
488 if (*regparse
== '^') { /* Complement of range. */
489 ret
= regnode(ANYBUT
);
492 ret
= regnode(ANYOF
);
493 if (*regparse
== ']' || *regparse
== '-')
495 while (*regparse
!= '\0' && *regparse
!= ']') {
496 if (*regparse
== '-') {
498 if (*regparse
== ']' || *regparse
== '\0')
501 class = UCHARAT(regparse
-2)+1;
502 classend
= UCHARAT(regparse
);
503 if (class > classend
+1)
504 FAIL("invalid [] range");
505 for (; class <= classend
; class++)
513 if (*regparse
!= ']')
514 FAIL("unmatched []");
516 *flagp
|= HASWIDTH
|SIMPLE
;
520 ret
= reg(1, &flags
);
523 *flagp
|= flags
&(HASWIDTH
|SPSTART
);
528 FAIL("internal urp"); /* Supposed to be caught earlier. */
533 FAIL("?+* follows nothing");
536 if (*regparse
== '\0')
538 ret
= regnode(EXACTLY
);
541 *flagp
|= HASWIDTH
|SIMPLE
;
545 register unsigned char ender
;
549 len
= strcspn(regparse
, (unsigned char *)META
);
551 len
= strcspn((char *)regparse
, META
);
554 FAIL("internal disaster");
555 ender
= *(regparse
+len
);
556 if (len
> 1 && ISMULT(ender
))
557 len
--; /* Back off clear of ?+* operand. */
561 ret
= regnode(EXACTLY
);
575 - regnode - emit a node
577 STATIC
unsigned char * /* Location. */
581 register unsigned char *ret
;
582 register unsigned char *ptr
;
585 op
= (unsigned char) iop
;
587 if (ret
== ®dummy
) {
594 *ptr
++ = '\0'; /* Null "next" pointer. */
602 - regc - emit (if appropriate) a byte of code
610 b
= (unsigned char) ib
;
611 if (regcode
!= ®dummy
)
618 - reginsert - insert an operator in front of already-emitted operand
620 * Means relocating the operand.
627 register unsigned char *src
;
628 register unsigned char *dst
;
629 register unsigned char *place
;
632 op
= (unsigned char) iop
;
633 if (regcode
== ®dummy
) {
644 place
= opnd
; /* Op node, where operand used to be. */
651 - regtail - set the next-pointer at the end of a node chain
658 register unsigned char *scan
;
659 register unsigned char *temp
;
665 /* Find last node. */
668 temp
= regnext(scan
);
674 if (OP(scan
) == BACK
)
678 *(scan
+1) = (offset
>>8)&0377;
679 *(scan
+2) = offset
&0377;
683 - regoptail - regtail on operand of first argument; nop if operandless
690 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
691 if (p
== NULL
|| p
== ®dummy
|| OP(p
) != BRANCH
)
693 regtail(OPERAND(p
), val
);
697 * regexec and friends
701 * Global work variables for regexec().
703 STATIC
unsigned char *reginput
; /* String-input pointer. */
704 STATIC
unsigned char *regbol
; /* Beginning of input, for ^ check. */
705 STATIC
unsigned char **regstartp
; /* Pointer to startp array. */
706 STATIC
unsigned char **regendp
; /* Ditto for endp. */
711 STATIC
unsigned char *regprop();
715 - regexec - match a regexp against a string
718 regexec(prog
, string
)
719 register regexp
*prog
;
720 register unsigned char *string
;
722 register unsigned char *s
;
724 extern char *strchr();
728 if (prog
== NULL
|| string
== NULL
) {
729 regerror("NULL parameter");
733 /* Check validity of program. */
734 if (UCHARAT(prog
->program
) != MAGIC
) {
735 regerror("corrupted program");
739 /* If there is a "must appear" string, look for it. */
740 if (prog
->regmust
!= NULL
) {
742 while ((s
= (unsigned char *)strchr((char *)s
,prog
->regmust
[0]))
744 if (strncmp((char *)s
, (char *)prog
->regmust
,
747 break; /* Found it. */
750 if (s
== NULL
) /* Not present. */
754 /* Mark beginning of line for ^ . */
757 /* Simplest case: anchored match need be tried only once. */
759 return(regtry(prog
, string
));
761 /* Messy cases: unanchored match. */
763 if (prog
->regstart
!= '\0')
764 /* We know what char it must start with. */
765 while ((s
= (unsigned char *)strchr((char *)s
, prog
->regstart
))
772 /* We don't -- general case. */
776 } while (*s
++ != '\0');
783 - regtry - try match at specific point
785 STATIC
int /* 0 failure, 1 success */
788 unsigned char *string
;
791 register unsigned char **sp
;
792 register unsigned char **ep
;
795 regstartp
= prog
->startp
;
796 regendp
= prog
->endp
;
800 for (i
= NSUBEXP
; i
> 0; i
--) {
804 if (regmatch(prog
->program
+ 1)) {
805 prog
->startp
[0] = string
;
806 prog
->endp
[0] = reginput
;
813 - regmatch - main matching routine
815 * Conceptually the strategy is simple: check to see whether the current
816 * node matches, call self recursively to see whether the rest matches,
817 * and then act accordingly. In practice we make some effort to avoid
818 * recursion, in particular by going through "ordinary" nodes (that don't
819 * need to know whether the rest of the match failed) by a loop instead of
822 STATIC
int /* 0 failure, 1 success */
826 register unsigned char *scan
; /* Current node. */
827 unsigned char *next
; /* Next node. */
829 extern char *strchr();
834 if (scan
!= NULL
&& regnarrate
)
835 fprintf(stderr
, "%s(\n", regprop(scan
));
837 while (scan
!= NULL
) {
840 fprintf(stderr
, "%s...\n", regprop(scan
));
842 next
= regnext(scan
);
846 if (reginput
!= regbol
)
850 if (*reginput
!= '\0')
854 if (*reginput
== '\0')
860 register unsigned char *opnd
;
862 opnd
= OPERAND(scan
);
863 /* Inline the first character, for speed. */
864 if (*opnd
!= *reginput
)
866 len
= strlen((char *)opnd
);
867 if (len
> 1 && strncmp((char *)opnd
,
868 (char *)reginput
, len
)
875 if (*reginput
== '\0'
876 || strchr((char *)OPERAND(scan
), *reginput
) == NULL
)
881 if (*reginput
== '\0'
882 || strchr((char *)OPERAND(scan
), *reginput
) != NULL
)
900 register unsigned char *save
;
902 no
= OP(scan
) - OPEN
;
905 if (regmatch(next
)) {
907 * Don't set startp if some later
908 * invocation of the same parentheses
911 if (regstartp
[no
] == NULL
)
912 regstartp
[no
] = save
;
928 register unsigned char *save
;
930 no
= OP(scan
) - CLOSE
;
933 if (regmatch(next
)) {
935 * Don't set endp if some later
936 * invocation of the same parentheses
939 if (regendp
[no
] == NULL
)
947 register unsigned char *save
;
949 if (OP(next
) != BRANCH
) /* No choice. */
950 next
= OPERAND(scan
); /* Avoid recursion. */
954 if (regmatch(OPERAND(scan
)))
957 scan
= regnext(scan
);
958 } while (scan
!= NULL
&& OP(scan
) == BRANCH
);
966 register unsigned char nextch
;
968 register unsigned char *save
;
972 * Lookahead to avoid useless match attempts
973 * when we know what character comes next.
976 if (OP(next
) == EXACTLY
)
977 nextch
= *OPERAND(next
);
978 min
= (OP(scan
) == STAR
) ? 0 : 1;
980 no
= regrepeat(OPERAND(scan
));
982 /* If it could work, try it. */
983 if (nextch
== '\0' || *reginput
== nextch
)
986 /* Couldn't or didn't -- back up. */
988 reginput
= save
+ no
;
994 return(1); /* Success! */
997 regerror("memory corruption");
1006 * We get here only if there's trouble -- normally "case END" is
1007 * the terminating point.
1009 regerror("corrupted pointers");
1014 - regrepeat - repeatedly match something simple, report how many
1020 register int count
= 0;
1021 register unsigned char *scan
;
1022 register unsigned char *opnd
;
1028 count
= strlen((char *)scan
);
1032 while (*opnd
== *scan
) {
1038 while (*scan
!= '\0' && strchr((char *)opnd
, *scan
) != NULL
) {
1044 while (*scan
!= '\0' && strchr((char *)opnd
, *scan
) == NULL
) {
1049 default: /* Oh dear. Called inappropriately. */
1050 regerror("internal foulup");
1051 count
= 0; /* Best compromise. */
1060 - regnext - dig the "next" pointer out of a node
1062 STATIC
unsigned char *
1064 register unsigned char *p
;
1066 register int offset
;
1083 STATIC
unsigned char *regprop();
1086 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1092 register unsigned char *s
;
1093 register unsigned char op
= EXACTLY
; /* Arbitrary non-END op. */
1094 register unsigned char *next
;
1095 extern char *strchr();
1099 while (op
!= END
) { /* While that wasn't END last time... */
1101 printf("%2d%s", s
-r
->program
, regprop(s
)); /* Where, what. */
1103 if (next
== NULL
) /* Next ptr. */
1106 printf("(%d)", (s
-r
->program
)+(next
-s
));
1108 if (op
== ANYOF
|| op
== ANYBUT
|| op
== EXACTLY
) {
1109 /* Literal string, where present. */
1110 while (*s
!= '\0') {
1119 /* Header fields of interest. */
1120 if (r
->regstart
!= '\0')
1121 printf("start `%c' ", r
->regstart
);
1123 printf("anchored ");
1124 if (r
->regmust
!= NULL
)
1125 printf("must have \"%s\"", r
->regmust
);
1130 - regprop - printable representation of opcode
1132 STATIC
unsigned char *
1136 register unsigned char *p
;
1137 STATIC
unsigned char buf
[50];
1139 (void) strcpy(buf
, ":");
1181 sprintf(buf
+strlen(buf
), "OPEN%d", OP(op
)-OPEN
);
1193 sprintf(buf
+strlen(buf
), "CLOSE%d", OP(op
)-CLOSE
);
1203 regerror("corrupted opcode");
1207 (void) strcat(buf
, p
);
1213 * The following is provided for those people who do not have strcspn() in
1214 * their C libraries. They should get off their butts and do something
1215 * about it; at least one public-domain implementation of those (highly
1216 * useful) string routines has been published on Usenet.
1220 * strcspn - find length of initial segment of s1 consisting entirely
1221 * of characters not from s2
1229 register unsigned char *scan1
;
1230 register unsigned char *scan2
;
1234 for (scan1
= s1
; *scan1
!= '\0'; scan1
++) {
1235 for (scan2
= s2
; *scan2
!= '\0';) /* ++ moved down. */
1236 if (*scan1
== *scan2
++)