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

« back to all changes in this revision

Viewing changes to text-utils/col.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-06-20 22:31:50 UTC
  • mfrom: (1.6.3 upstream) (4.5.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110620223150-lz8wrv0946ihcz3z
Tags: 2.19.1-2ubuntu1
* Merge from Debian unstable, remaining changes:
  - Build for multiarch.
  - Add pre-depends on multiarch-support.
  - configure.ac: don't try to be clever about extracting a path name from
    $libdir to append to /usr in a way that's not overridable; instead,
    reuse the built-in configurable libexecdir.
  - Fix up the .pc.in files to know about libexecdir, so our substitutions
    don't leave us with unusable pkg-config files.
  - Install custom blkid.conf to use /dev/.blkid.tab since we don't
    expect device names to survive a reboot
  - Mention mountall(8) in fstab(5) manpages, along with its special
    options.
  - Since upstart is required in Ubuntu, the hwclock.sh init script is not
    called on startup and the hwclockfirst.sh init script is removed.
  - Drop depends on initscripts for the above.
  - Replace hwclock udev rule with an Upstart job.
  - For the case where mount is called with a directory to mount, look
    that directory up in mountall's /lib/init/fstab if we couldn't find
    it mentioned anywhere else.  This means "mount /proc", "mount /sys",
    etc. work.
  - mount.8 points to the cifs-utils package, not the obsolete smbfs one. 
* Dropped changes:
  - mount.preinst: lsb_release has been fixed in lucid and above to be
    usable without configuration, so we don't have to diverge from Debian
    here anymore.
* Changes merged upstream:
  - sfdisk support for '+' with '-N'
  - mount/umount.c: fix a segfault on umount with empty mtab entry
  - Fix arbitrary unmount with fuse security issue

Show diffs side-by-side

added added

removed removed

Lines of Context:
50
50
#include <string.h>
51
51
#include <stdio.h>
52
52
#include <unistd.h>
 
53
 
53
54
#include "nls.h"
54
 
 
 
55
#include "xalloc.h"
55
56
#include "widechar.h"
56
57
 
57
58
#define BS      '\b'            /* backspace */
92
93
        int     l_max_col;              /* max column in the line */
93
94
};
94
95
 
95
 
void usage(void);
96
 
void wrerr(void);
97
 
void warn(int);
98
96
void free_line(LINE *l);
99
97
void flush_line(LINE *l);
100
98
void flush_lines(int);
101
99
void flush_blanks(void);
102
 
void *xmalloc(void *p, size_t size);
103
100
LINE *alloc_line(void);
104
101
 
105
102
CSET last_set;                  /* char_set of last char printed */
115
112
        if (putwchar(ch) == WEOF) \
116
113
                wrerr();
117
114
 
 
115
static void __attribute__((__noreturn__)) usage(void)
 
116
{
 
117
        errx(EXIT_FAILURE, _("usage: %s [-bfpx] [-l nline]"),
 
118
                        program_invocation_short_name);
 
119
}
 
120
 
 
121
static void __attribute__((__noreturn__)) wrerr()
 
122
{
 
123
        errx(EXIT_FAILURE, _("write error."));
 
124
}
 
125
 
118
126
int main(int argc, char **argv)
119
127
{
120
128
        register wint_t ch;
128
136
        int this_line;                  /* line l points to */
129
137
        int nflushd_lines;              /* number of lines that were flushed */
130
138
        int adjust, opt, warned;
131
 
        int ret = 0;
 
139
        int ret = EXIT_SUCCESS;
132
140
 
133
141
        setlocale(LC_ALL, "");
134
142
        bindtextdomain(PACKAGE, LOCALEDIR);
135
143
        textdomain(PACKAGE);
136
 
        
 
144
 
137
145
        max_bufd_lines = 128;
138
146
        compress_spaces = 1;            /* compress spaces into tabs */
139
147
        pass_unknown_seqs = 0;          /* remove unknown escape sequences */
149
157
                        compress_spaces = 1;
150
158
                        break;
151
159
                case 'l':               /* buffered line count */
152
 
                        if ((max_bufd_lines = atoi(optarg)) <= 0) {
153
 
                                (void)fprintf(stderr,
154
 
                                    _("col: bad -l argument %s.\n"), optarg);
155
 
                                exit(1);
156
 
                        }
 
160
                        if ((max_bufd_lines = atoi(optarg)) <= 0)
 
161
                                errx(EXIT_FAILURE, _("bad -l argument %s."), optarg);
157
162
                        break;
158
163
                case 'p':
159
164
                        pass_unknown_seqs = 1;
176
181
        cur_line = max_line = nflushd_lines = this_line = 0;
177
182
        cur_set = last_set = CS_NORMAL;
178
183
        lines = l = alloc_line();
179
 
        
 
184
 
180
185
        while (feof(stdin)==0) {
181
186
                errno = 0;
182
187
                if ((ch = getwchar()) == WEOF) {
183
188
                        if (errno==EILSEQ) {
184
 
                                perror("col");
185
 
                                ret = 1;
 
189
                                warn(NULL);
 
190
                                ret = EXIT_FAILURE;
186
191
                        }
187
192
                        break;
188
 
                }       
 
193
                }
189
194
                if (!iswgraph(ch)) {
190
195
                        switch (ch) {
191
196
                        case BS:                /* can't go back further */
275
280
                                                }
276
281
                                        } else {
277
282
                                                if (!warned++)
278
 
                                                        warn(cur_line);
 
283
                                                        warnx(
 
284
                        _("warning: can't back up %s."), cur_line < 0 ?
 
285
                        _("past first line") : _("-- line already flushed"));
279
286
                                                cur_line -= nmove;
280
287
                                        }
281
288
                                }
302
309
                        int need;
303
310
 
304
311
                        need = l->l_lsize ? l->l_lsize * 2 : 90;
305
 
                        l->l_line = (CHAR *)xmalloc((void *) l->l_line,
306
 
                            (unsigned) need * sizeof(CHAR));
 
312
                        l->l_line = (CHAR *)xrealloc((void *) l->l_line,
 
313
                                                    (unsigned) need * sizeof(CHAR));
307
314
                        l->l_lsize = need;
308
315
                }
309
316
                c = &l->l_line[l->l_line_len++];
340
347
                nblank_lines = 2;
341
348
        flush_blanks();
342
349
        if (ferror(stdout) || fclose(stdout))
343
 
                return 1;
 
350
                return EXIT_FAILURE;
344
351
        return ret;
345
352
}
346
353
 
415
422
                 */
416
423
                if (l->l_lsize > sorted_size) {
417
424
                        sorted_size = l->l_lsize;
418
 
                        sorted = (CHAR *)xmalloc((void *)sorted,
419
 
                            (unsigned)sizeof(CHAR) * sorted_size);
 
425
                        sorted = (CHAR *)xrealloc((void *)sorted,
 
426
                                                  (unsigned)sizeof(CHAR) * sorted_size);
420
427
                }
421
428
                if (l->l_max_col >= count_size) {
422
429
                        count_size = l->l_max_col + 1;
423
 
                        count = (int *)xmalloc((void *)count,
 
430
                        count = (int *)xrealloc((void *)count,
424
431
                            (unsigned)sizeof(int) * count_size);
425
432
                }
426
433
                memset(count, 0, sizeof(int) * l->l_max_col + 1);
510
517
        int i;
511
518
 
512
519
        if (!line_freelist) {
513
 
                l = (LINE *)xmalloc((void *)NULL, sizeof(LINE) * NALLOC);
 
520
                l = xmalloc(sizeof(LINE) * NALLOC);
514
521
                line_freelist = l;
515
522
                for (i = 1; i < NALLOC; i++, l++)
516
523
                        l->l_next = l + 1;
529
536
        line_freelist = l;
530
537
}
531
538
 
532
 
void *
533
 
xmalloc(void *p, size_t size)
534
 
{
535
 
        if (!(p = (void *)realloc(p, size))) {
536
 
                (void)fprintf(stderr, "col: %s.\n", strerror(ENOMEM));
537
 
                exit(1);
538
 
        }
539
 
        return(p);
540
 
}
541
 
 
542
 
void usage()
543
 
{
544
 
        (void)fprintf(stderr, _("usage: col [-bfpx] [-l nline]\n"));
545
 
        exit(1);
546
 
}
547
 
 
548
 
void wrerr()
549
 
{
550
 
        (void)fprintf(stderr, _("col: write error.\n"));
551
 
        exit(1);
552
 
}
553
 
 
554
 
void warn(int line)
555
 
{
556
 
        (void)fprintf(stderr,
557
 
            _("col: warning: can't back up %s.\n"), line < 0 ?
558
 
            _("past first line") : _("-- line already flushed"));
559
 
}
 
539