+ /* print the matched line */
+ if (print_match_counts == 0) {
+#ifdef BB_FEATURE_GREP_CONTEXT
+ int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
+
+ /* if we were told to print 'before' lines and there is at least
+ * one line in the circular buffer, print them */
+ if (lines_before && before_buf[prevpos] != NULL) {
+ int first_buf_entry_line_num = linenum - lines_before;
+
+ /* advance to the first entry in the circular buffer, and
+ * figure out the line number is of the first line in the
+ * buffer */
+ idx = curpos;
+ while (before_buf[idx] == NULL) {
+ idx = (idx + 1) % lines_before;
+ first_buf_entry_line_num++;
+ }
+
+ /* now print each line in the buffer, clearing them as we go */
+ while (before_buf[idx] != NULL) {
+ print_line(before_buf[idx], first_buf_entry_line_num, '-');
+ free(before_buf[idx]);
+ before_buf[idx] = NULL;
+ idx = (idx + 1) % lines_before;
+ first_buf_entry_line_num++;
+ }
+ }
+
+ /* make a note that we need to print 'after' lines */
+ print_n_lines_after = lines_after;
+#endif /* BB_FEATURE_GREP_CONTEXT */
+ print_line(line, linenum, ':');
+ }
+ }
+#ifdef BB_FEATURE_GREP_CONTEXT
+ else { /* no match */
+ /* Add the line to the circular 'before' buffer */
+ if(lines_before) {
+ if(before_buf[curpos])
+ free(before_buf[curpos]);
+ before_buf[curpos] = strdup(line);
+ curpos = (curpos + 1) % lines_before;
+ }
+ }
+
+ /* if we need to print some context lines after the last match, do so */
+ if (print_n_lines_after && (last_line_printed != linenum)) {
+ print_line(line, linenum, '-');
+ print_n_lines_after--;
+ }
+#endif /* BB_FEATURE_GREP_CONTEXT */
+ } /* for */
+ free(line);
+ }
+
+
+ /* special-case file post-processing for options where we don't print line
+ * matches, just filenames and possibly match counts */
+
+ /* grep -c: print [filename:]count, even if count is zero */
+ if (print_match_counts) {
+ if (print_filename)
+ printf("%s:", cur_file);
+ printf("%d\n", nmatches);