~ubuntu-branches/ubuntu/saucy/fdclone/saucy-proposed

« back to all changes in this revision

Viewing changes to printf.c

  • Committer: Package Import Robot
  • Author(s): Elías Alejandro Año Mendoza
  • Date: 2013-03-31 20:19:19 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20130331201919-vd2f75ssuh1sdctw
Tags: 3.01-1
* New upstream release
* Updated Standard-Version to 3.9.4
* Bump debhelper to 9
* debian/rules enabled hardening build flags

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#define PRINTBUFUNIT            16
19
19
#define THDIGIT                 3
 
20
#define MINUNITWIDTH            (3 + strsize(" KB"))
20
21
#define NULLSTR                 "(null)"
21
22
 
22
23
#ifdef  FD
29
30
#endif
30
31
static int NEAR checkchar __P_((int, printbuf_t *));
31
32
static int NEAR setint __P_((u_long_t, int, printbuf_t *, int, int));
 
33
#ifndef MINIMUMSHELL
 
34
static int NEAR unitint __P_((u_long_t, int, printbuf_t *, int, int));
 
35
#endif
32
36
static int NEAR setstr __P_((CONST char *, printbuf_t *, int, int));
33
37
static int NEAR commonprintf __P_((printbuf_t *, CONST char *, va_list));
34
38
 
35
39
CONST char printfflagchar[] = {
36
40
        '-', '0', '\'',
37
41
#ifndef MINIMUMSHELL
38
 
        '+', ' ', '<', '^',
 
42
        '+', ' ', '<', '^', '>',
39
43
#endif
40
44
        '\0',
41
45
};
42
46
CONST int printfflag[] = {
43
47
        VF_MINUS, VF_ZERO, VF_THOUSAND,
44
48
#ifndef MINIMUMSHELL
45
 
        VF_PLUS, VF_SPACE, VF_STRICTWIDTH, VF_PRINTABLE,
 
49
        VF_PLUS, VF_SPACE, VF_STRICTWIDTH, VF_PRINTABLE, VF_SIZEUNIT,
46
50
#endif
47
51
};
48
52
CONST char printfsizechar[] = {
63
67
int printf_defkanji = 0;
64
68
#endif
65
69
 
 
70
#ifndef MINIMUMSHELL
 
71
static CONST char printfsizeunit[] = "KMGTPEZY";
 
72
#endif
 
73
 
66
74
 
67
75
#ifndef MINIMUMSHELL
68
76
VOID getcharwidth(s, ptr, rwp, vwp)
323
331
#endif
324
332
        if (c && setchar(c, pbufp) < 0) return(-1);
325
333
 
326
 
        if (prec >= 0 && len < prec) {
327
 
                for (i = 0; i < prec - len; i++) {
 
334
        if (prec < 0 || len >= prec) /*EMPTY*/;
 
335
        else for (i = 0; i < prec - len; i++) {
328
336
#ifndef MINIMUMSHELL
329
 
                        if ((pbufp -> flags & VF_STRICTWIDTH) && width == 1)
330
 
                                break;
 
337
                if ((pbufp -> flags & VF_STRICTWIDTH) && width == 1) break;
331
338
#endif
332
 
                        width--;
333
 
                        c = '0';
334
 
                        if ((pbufp -> flags & VF_THOUSAND)
335
 
                        && !((prec - i) % (THDIGIT + 1)))
336
 
                                c = (i) ? ',' : ' ';
337
 
                        if (setchar(c, pbufp) < 0) return(-1);
338
 
                }
339
 
        }
340
 
#ifndef MINIMUMSHELL
341
 
        if ((pbufp -> flags & VF_STRICTWIDTH) && width >= 0 && width < len)
342
 
        for (i = 0; i < width; i++) {
343
 
                c = '9';
 
339
                width--;
 
340
                c = '0';
344
341
                if ((pbufp -> flags & VF_THOUSAND)
345
 
                && !((width - i) % (THDIGIT + 1)))
 
342
                && !((prec - i) % (THDIGIT + 1)))
346
343
                        c = (i) ? ',' : ' ';
347
344
                if (setchar(c, pbufp) < 0) return(-1);
348
345
        }
349
 
        else
350
 
#endif  /* !MINIMUMSHELL */
351
 
        for (i = 0; i < len; i++) {
352
 
                if (setchar(num[len - i - 1], pbufp) < 0) return(-1);
353
 
        }
 
346
 
 
347
        i = 0;
 
348
#ifndef MINIMUMSHELL
 
349
        if (width < 0 || width >= len) /*EMPTY*/;
 
350
        else if (pbufp -> flags & VF_SIZEUNIT) return(len);
 
351
        else if (pbufp -> flags & VF_ASPOSSIBLE) i = len - width;
 
352
        else if (pbufp -> flags & VF_STRICTWIDTH)
 
353
                for (len = 0; len < width; len++)
 
354
                        if (Xisdigit(num[len])) num[len] = '9';
 
355
#endif
 
356
 
 
357
        while (i++ < len) if (setchar(num[len - i], pbufp) < 0) return(-1);
354
358
 
355
359
        if (width >= 0) for (; i < width; i++) {
356
360
                if (setchar(' ', pbufp) < 0) return(-1);
359
363
        return(pbufp -> ptr - ptr);
360
364
}
361
365
 
 
366
#ifndef MINIMUMSHELL
 
367
static int NEAR unitint(u, base, pbufp, width, prec)
 
368
u_long_t u;
 
369
int base;
 
370
printbuf_t *pbufp;
 
371
int width, prec;
 
372
{
 
373
        printbuf_t pbuf;
 
374
        int len, unit, uwidth, frac;
 
375
 
 
376
        memcpy((char *)&pbuf, (char *)pbufp, sizeof(pbuf));
 
377
        pbufp -> buf = NULL;
 
378
        pbufp -> flags |= VF_FILE;
 
379
        len = unit = uwidth = 0;
 
380
        frac = -1;
 
381
        if (pbufp -> flags & VF_STRICTWIDTH) unit++;
 
382
        if (width >= MINUNITWIDTH) for (;;) {
 
383
                if (unit) uwidth = 3;
 
384
                pbufp -> ptr = pbuf.ptr;
 
385
                len = setint(u, base, pbufp, -1, prec);
 
386
                if (len < 0) {
 
387
                        len = width;
 
388
                        break;
 
389
                }
 
390
                if (len + uwidth <= width) break;
 
391
                if (!(printfsizeunit[unit])) break;
 
392
                unit++;
 
393
                frac = (int)(u & (u_long_t)0x3ff);
 
394
                u >>= 10;
 
395
        }
 
396
 
 
397
        memcpy((char *)pbufp, (char *)&pbuf, sizeof(pbuf));
 
398
        if ((pbufp -> flags & VF_STRICTWIDTH) || len + 2 + uwidth > width)
 
399
                frac = -1;
 
400
        if (frac < 0) len = width - uwidth;
 
401
        pbufp -> flags &= ~VF_SIZEUNIT;
 
402
        pbufp -> flags |= VF_STRICTWIDTH;
 
403
        len = setint(u, base, pbufp, len, prec);
 
404
        if (len < 0) return(-1);
 
405
        if (frac >= 0) {
 
406
                width -= len + 1 + uwidth;
 
407
                u = (u_long_t)frac;
 
408
                u *= (u_long_t)1000;
 
409
                u >>= 10;
 
410
                if (setchar('.', pbufp) < 0) return(-1);
 
411
                pbufp -> flags &= (VF_NEW | VF_FILE);
 
412
                pbufp -> flags |= (VF_ZERO | VF_ASPOSSIBLE);
 
413
                if (setint(u, base, pbufp, width, prec) < 0) return(-1);
 
414
        }
 
415
        if (unit > 0) {
 
416
                if (setchar(' ', pbufp) < 0) return(-1);
 
417
                if (setchar(printfsizeunit[unit - 1], pbufp) < 0) return(-1);
 
418
                if (setchar('B', pbufp) < 0) return(-1);
 
419
        }
 
420
 
 
421
        return(pbufp -> ptr - pbuf.ptr);
 
422
}
 
423
#endif  /* !MINIMUMSHELL */
 
424
 
362
425
static int NEAR setstr(s, pbufp, width, prec)
363
426
CONST char *s;
364
427
printbuf_t *pbufp;
690
753
                                mask >>= 1;
691
754
                                if (u & ~mask) u |= ~mask;
692
755
                        }
 
756
 
 
757
#ifndef MINIMUMSHELL
 
758
                        if (pbufp -> flags & VF_SIZEUNIT)
 
759
                                len = unitint(u, base, pbufp, width, prec);
 
760
                        else
 
761
#endif
693
762
                        len = setint(u, base, pbufp, width, prec);
694
763
                }
695
764
 
814
883
}
815
884
 
816
885
#ifdef  USESTDARGH
817
 
/*VARARGS2*/
 
886
/*VARARGS1*/
818
887
int Xprintf(CONST char *fmt, ...)
819
888
#else
820
 
/*VARARGS2*/
 
889
/*VARARGS1*/
821
890
int Xprintf(fmt, va_alist)
822
891
CONST char *fmt;
823
892
va_dcl