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

« back to all changes in this revision

Viewing changes to text-utils/parse.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:
43
43
#include <string.h>
44
44
#include "hexdump.h"
45
45
#include "nls.h"
 
46
#include "xalloc.h"
46
47
 
47
48
static void escape(char *p1);
48
49
static void badcnt(const char *s);
59
60
        int ch;
60
61
        char buf[2048 + 1];
61
62
 
62
 
        if ((fp = fopen(name, "r")) == NULL) {
63
 
                (void)fprintf(stderr, _("hexdump: can't read %s.\n"), name);
64
 
                exit(1);
65
 
        }
 
63
        if ((fp = fopen(name, "r")) == NULL)
 
64
                err(EXIT_FAILURE, _("can't read %s"), name);
66
65
        while (fgets(buf, sizeof(buf), fp)) {
67
66
                if ((p = strchr(buf, '\n')) == NULL) {
68
 
                        (void)fprintf(stderr, _("hexdump: line too long.\n"));
 
67
                        warnx(_("line too long"));
69
68
                        while ((ch = getchar()) != '\n' && ch != EOF);
70
69
                        continue;
71
70
                }
87
86
        const char *savep;
88
87
 
89
88
        /* Start new linked list of format units. */
90
 
        tfs = emalloc(sizeof(FS));
 
89
        tfs = xcalloc(1, sizeof(FS));
91
90
        if (!fshead)
92
91
                fshead = tfs;
93
92
        else
103
102
                        break;
104
103
 
105
104
                /* Allocate a new format unit and link it in. */
106
 
                tfu = emalloc(sizeof(FU));
 
105
                tfu = xcalloc(1, sizeof(FU));
107
106
                *nextfu = tfu;
108
107
                nextfu = &tfu->nextfu;
109
108
                tfu->reps = 1;
140
139
                for (savep = ++p; *p != '"';)
141
140
                        if (*p++ == 0)
142
141
                                badfmt(fmt);
143
 
                if (!(tfu->fmt = malloc(p - savep + 1)))
144
 
                        nomem();
 
142
                tfu->fmt = xmalloc(p - savep + 1);
145
143
                (void) strncpy(tfu->fmt, savep, p - savep);
146
144
                tfu->fmt[p - savep] = '\0';
147
145
                escape(tfu->fmt);
221
219
                 * conversion character gets its own.
222
220
                 */
223
221
                for (nconv = 0, fmtp = fu->fmt; *fmtp; nextpr = &pr->nextpr) {
224
 
                        pr = emalloc(sizeof(PR));
 
222
                        pr = xcalloc(1, sizeof(PR));
225
223
                        if (!fu->nextpr)
226
224
                                fu->nextpr = pr;
227
225
                        else
388
386
                         */
389
387
                        savech = *p2;
390
388
                        p1[0] = '\0';
391
 
                        pr->fmt = emalloc(strlen(fmtp) + strlen(cs) + 1);
 
389
                        pr->fmt = xmalloc(strlen(fmtp) + strlen(cs) + 1);
392
390
                        (void)strcpy(pr->fmt, fmtp);
393
391
                        (void)strcat(pr->fmt, cs);
394
392
                        *p2 = savech;
396
394
                        fmtp = p2;
397
395
 
398
396
                        /* Only one conversion character if byte count */
399
 
                        if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++) {
400
 
                                (void)fprintf(stderr,
401
 
                                    _("hexdump: byte count with multiple conversion characters.\n"));
402
 
                                exit(1);
403
 
                        }
 
397
                        if (!(pr->flags&F_ADDRESS) && fu->bcnt && nconv++)
 
398
                                errx(EXIT_FAILURE,
 
399
                                    _("byte count with multiple conversion characters"));
404
400
                }
405
401
                /*
406
402
                 * If format unit byte count not specified, figure it out
479
475
 
480
476
static void badcnt(const char *s)
481
477
{
482
 
        (void)fprintf(stderr,
483
 
            _("hexdump: bad byte count for conversion character %s.\n"), s);
484
 
        exit(1);
 
478
        errx(EXIT_FAILURE, _("bad byte count for conversion character %s"), s);
485
479
}
486
480
 
487
481
static void badsfmt(void)
488
482
{
489
 
        (void)fprintf(stderr,
490
 
            _("hexdump: %%s requires a precision or a byte count.\n"));
491
 
        exit(1);
 
483
        errx(EXIT_FAILURE, _("%%s requires a precision or a byte count"));
492
484
}
493
485
 
494
486
static void badfmt(const char *fmt)
495
487
{
496
 
        (void)fprintf(stderr, _("hexdump: bad format {%s}\n"), fmt);
497
 
        exit(1);
 
488
        errx(EXIT_FAILURE, _("bad format {%s}"), fmt);
498
489
}
499
490
 
500
491
static void badconv(const char *ch)
501
492
{
502
 
        (void)fprintf(stderr, _("hexdump: bad conversion character %%%s.\n"), ch);
503
 
        exit(1);
 
493
        errx(EXIT_FAILURE, _("bad conversion character %%%s"), ch);
504
494
}