~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to text-utils/colrm.c

  • Committer: Package Import Robot
  • Author(s): LaMont Jones
  • Date: 2011-11-03 15:38:23 UTC
  • mto: (4.5.5 sid) (1.6.4)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: package-import@ubuntu.com-20111103153823-10sx16jprzxlhkqf
ImportĀ upstreamĀ versionĀ 2.20.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
#include <stdio.h>
42
42
#include <stdlib.h>
 
43
#include <getopt.h>
43
44
 
44
45
#include "nls.h"
45
46
#include "widechar.h"
 
47
#include "strutils.h"
 
48
#include "c.h"
46
49
 
47
50
/*
48
51
COLRM removes unwanted columns from a file
49
52
        Jeff Schriebman  UC Berkeley 11-74
50
53
*/
51
54
 
52
 
int
53
 
main(int argc, char **argv)
54
 
{
55
 
        register int ct, first, last;
56
 
        register wint_t c;
57
 
        int i, w;
 
55
static void __attribute__ ((__noreturn__)) usage(FILE * out)
 
56
{
 
57
        fprintf(out, _("\nUsage:\n"
 
58
                       " %s [startcol [endcol]]\n"),
 
59
                       program_invocation_short_name);
 
60
 
 
61
        fprintf(out, _("\nOptions:\n"
 
62
                       " -V, --version   output version information and exit\n"
 
63
                       " -h, --help      display this help and exit\n\n"));
 
64
 
 
65
        fprintf(out, _("%s reads from standard input and writes to standard output\n\n"),
 
66
                       program_invocation_short_name);
 
67
 
 
68
        exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
 
69
}
 
70
 
 
71
int process_input(unsigned long first, unsigned long last)
 
72
{
 
73
        unsigned long ct = 0;
 
74
        wint_t c;
 
75
        unsigned long i;
 
76
        int w;
58
77
        int padding;
59
78
 
60
 
        setlocale(LC_ALL, "");
61
 
 
62
 
        first = 0;
63
 
        last = 0;
64
 
        if (argc > 1)
65
 
                first = atoi(*++argv);
66
 
        if (argc > 2)
67
 
                last = atoi(*++argv);
68
 
 
69
 
start:
70
 
        ct = 0;
71
 
loop1:
72
 
        c = getwc(stdin);
73
 
        if (c == WEOF)
74
 
                goto fin;
75
 
        if (c == '\t')
76
 
                w = ((ct + 8) & ~7) - ct;
77
 
        else if (c == '\b')
78
 
                w = (ct ? ct - 1 : 0) - ct;
79
 
        else {
80
 
                w = wcwidth(c);
81
 
                if (w < 0)
82
 
                        w = 0;
83
 
        }
84
 
        ct += w;
85
 
        if (c == '\n') {
86
 
                putwc(c, stdout);
87
 
                goto start;
88
 
        }
89
 
        if (!first || ct < first) {
90
 
                putwc(c, stdout);
91
 
                goto loop1;
92
 
        }
93
 
        for (i = ct-w+1; i < first; i++)
 
79
        for (;;) {
 
80
                c = getwc(stdin);
 
81
                if (c == WEOF)
 
82
                        return 0;
 
83
                if (c == '\t')
 
84
                        w = ((ct + 8) & ~7) - ct;
 
85
                else if (c == '\b')
 
86
                        w = (ct ? ct - 1 : 0) - ct;
 
87
                else {
 
88
                        w = wcwidth(c);
 
89
                        if (w < 0)
 
90
                                w = 0;
 
91
                }
 
92
                ct += w;
 
93
                if (c == '\n') {
 
94
                        putwc(c, stdout);
 
95
                        ct = 0;
 
96
                        continue;
 
97
 
 
98
                }
 
99
                if (!first || ct < first) {
 
100
                        putwc(c, stdout);
 
101
                        continue;
 
102
                }
 
103
                break;
 
104
        }
 
105
 
 
106
        for (i = ct - w + 1; i < first; i++)
94
107
                putwc(' ', stdout);
95
108
 
96
 
/* Loop getting rid of characters */
 
109
        /* Loop getting rid of characters */
97
110
        while (!last || ct < last) {
98
111
                c = getwc(stdin);
99
112
                if (c == WEOF)
100
 
                        goto fin;
 
113
                        return 0;
101
114
                if (c == '\n') {
102
115
                        putwc(c, stdout);
103
 
                        goto start;
 
116
                        return 1;
104
117
                }
105
118
                if (c == '\t')
106
119
                        ct = (ct + 8) & ~7;
116
129
 
117
130
        padding = 0;
118
131
 
119
 
/* Output last of the line */
 
132
        /* Output last of the line */
120
133
        for (;;) {
121
134
                c = getwc(stdin);
122
135
                if (c == WEOF)
123
136
                        break;
124
137
                if (c == '\n') {
125
138
                        putwc(c, stdout);
126
 
                        goto start;
 
139
                        return 1;
127
140
                }
128
141
                if (padding == 0 && last < ct) {
129
 
                        for (i = last; i <ct; i++)
 
142
                        for (i = last; i < ct; i++)
130
143
                                putwc(' ', stdout);
131
144
                        padding = 1;
132
145
                }
133
146
                putwc(c, stdout);
134
147
        }
135
 
fin:
 
148
        return 0;
 
149
}
 
150
 
 
151
int main(int argc, char **argv)
 
152
{
 
153
        unsigned long first = 0, last = 0;
 
154
        int opt;
 
155
 
 
156
        static const struct option longopts[] = {
 
157
                {"version", no_argument, 0, 'V'},
 
158
                {"help", no_argument, 0, 'h'},
 
159
                {NULL, 0, 0, 0}
 
160
        };
 
161
 
 
162
        setlocale(LC_ALL, "");
 
163
        bindtextdomain(PACKAGE, LOCALEDIR);
 
164
        textdomain(PACKAGE);
 
165
 
 
166
        while ((opt =
 
167
                getopt_long(argc, argv, "bfhl:pxVH", longopts,
 
168
                            NULL)) != -1)
 
169
                switch (opt) {
 
170
                case 'V':
 
171
                        printf(_("%s from %s\n"),
 
172
                               program_invocation_short_name,
 
173
                               PACKAGE_STRING);
 
174
                        return EXIT_SUCCESS;
 
175
                case 'h':
 
176
                        usage(stdout);
 
177
                default:
 
178
                        usage(stderr);
 
179
                }
 
180
 
 
181
        if (argc > 1)
 
182
                first = strtoul_or_err(*++argv, _("first argument"));
 
183
        if (argc > 2)
 
184
                last = strtoul_or_err(*++argv, _("second argument"));
 
185
 
 
186
        while (process_input(first, last))
 
187
                ;
 
188
 
136
189
        fflush(stdout);
137
190
        if (ferror(stdout) || fclose(stdout))
138
 
                return 1;
139
 
        return 0;
 
191
                return EXIT_FAILURE;
 
192
        return EXIT_SUCCESS;
140
193
}