blob: 9a8a51440a1e4ccc63e7312777f7c3f11509d9a1 [file] [log] [blame]
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2017 Denys Vlasenko <vda.linux@googlemail.com>
4 *
5 * Licensed under GPLv2, see file LICENSE in this source tree.
6 */
7//kbuild:lib-y += print_numbered_lines.o
8
9#include "libbb.h"
10
11void FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename)
12{
13 FILE *fp = fopen_or_warn_stdin(filename);
14 unsigned N = ns->start;
15 char *line;
16
17 while ((line = xmalloc_fgetline(fp)) != NULL) {
18 if (ns->all
19 || (ns->nonempty && line[0])
20 ) {
21 printf("%*u%s%s\n", ns->width, N, ns->sep, line);
22 N += ns->inc;
23 } else if (ns->empty_str)
24 fputs(ns->empty_str, stdout);
25 free(line);
26 }
Denys Vlasenko75e90b12017-07-14 10:47:18 +020027 ns->start = N;
Denys Vlasenkod9eb40c2017-04-12 15:48:19 +020028
29 fclose(fp);
30}