Ignore:
Timestamp:
Mar 26, 2018, 10:25:56 PM (7 years ago)
Author:
bird
Message:

kmkbuiltin: funnel output thru output.c (usually via err.c).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/cmp_util.c

    r3095 r3192  
    6666
    6767static int
    68 errmsg(const char *file, off_t byte, off_t line, int lflag)
     68errmsg(PKMKBUILTINCTX pCtx, const char *file, off_t byte, off_t line, int lflag)
    6969{
    7070    if (lflag)
    7171#ifdef _MSC_VER
    72         return err(ERR_EXIT, "%s: char %I64d, line %lld", file, (__int64)byte, (long long)line);
    73 #else
    74         return err(ERR_EXIT, "%s: char %lld, line %lld", file, (long long)byte, (long long)line);
    75 #endif
    76     return err(ERR_EXIT, "%s", file);
     72        return err(pCtx, ERR_EXIT, "%s: char %I64d, line %lld", file, (__int64)byte, (long long)line);
     73#else
     74        return err(pCtx, ERR_EXIT, "%s: char %lld, line %lld", file, (long long)byte, (long long)line);
     75#endif
     76    return err(pCtx, ERR_EXIT, "%s", file);
    7777}
    7878
    7979
    8080static int
    81 eofmsg(const char *file, off_t byte, off_t line, int sflag, int lflag)
     81eofmsg(PKMKBUILTINCTX pCtx, const char *file, off_t byte, off_t line, int sflag, int lflag)
    8282{
    8383    if (!sflag)
    8484    {
    8585        if (!lflag)
    86             warnx("EOF on %s", file);
     86            warnx(pCtx, "EOF on %s", file);
    8787        else
    8888        {
    8989#ifdef _MSC_VER
    9090            if (line > 0)
    91                 warnx("EOF on %s: char %I64d, line %I64d", file, (__int64)byte, (__int64)line);
     91                warnx(pCtx, "EOF on %s: char %I64d, line %I64d", file, (__int64)byte, (__int64)line);
    9292            else
    93                 warnx("EOF on %s: char %I64d", file, (__int64)byte);
     93                warnx(pCtx, "EOF on %s: char %I64d", file, (__int64)byte);
    9494#else
    9595            if (line > 0)
    96                 warnx("EOF on %s: char %lld, line %lld", file, (long long)byte, (long long)line);
     96                warnx(pCtx, "EOF on %s: char %lld, line %lld", file, (long long)byte, (long long)line);
    9797            else
    98                 warnx("EOF on %s: char %lld", file, (long long)byte);
     98                warnx(pCtx, "EOF on %s: char %lld", file, (long long)byte);
    9999#endif
    100100        }
     
    105105
    106106static int
    107 diffmsg(const char *file1, const char *file2, off_t byte, off_t line, int sflag)
     107diffmsg(PKMKBUILTINCTX pCtx, const char *file1, const char *file2, off_t byte, off_t line, int sflag)
    108108{
    109109    if (!sflag)
    110110#ifdef _MSC_VER
    111         printf("%s %s differ: char %I64d, line %I64d\n",
     111        kmk_builtin_ctx_printf(pCtx, 0, "%s %s differ: char %I64d, line %I64d\n",
    112112               file1, file2, (__int64)byte, (__int64)line);
    113113#else
    114         printf("%s %s differ: char %lld, line %lld\n",
     114        kmk_builtin_ctx_printf(pCtx, 0, "%s %s differ: char %lld, line %lld\n",
    115115               file1, file2, (long long)byte, (long long)line);
    116116#endif
     
    123123 */
    124124static int
    125 c_special(int fd1, const char *file1, off_t skip1,
     125c_special(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1,
    126126          int fd2, const char *file2, off_t skip2,
    127127          int lflag, int sflag)
     
    134134    fd1dup = dup(fd1);
    135135    if (fd1 < 0)
    136         return err(ERR_EXIT, "%s", file1);
     136        return err(pCtx, ERR_EXIT, "%s", file1);
    137137    fp1 = fdopen(fd1dup, "rb");
    138138    if (!fp1)
     
    140140    if (!fp1)
    141141    {
    142         err(ERR_EXIT, "%s", file1);
     142        err(pCtx, ERR_EXIT, "%s", file1);
    143143        close(fd1dup);
    144144        return ERR_EXIT;
     
    189189                        if (!lflag)
    190190                        {
    191                             rc = diffmsg(file1, file2, byte, line, sflag);
     191                            rc = diffmsg(pCtx, file1, file2, byte, line, sflag);
    192192                            break;
    193193                        }
    194194                        rc = DIFF_EXIT;
    195195#ifdef _MSC_VER
    196                         printf("%6i64d %3o %3o\n", (__int64)byte, ch1, ch2);
    197 #else
    198                         printf("%6lld %3o %3o\n", (long long)byte, ch1, ch2);
     196                        kmk_builtin_ctx_printf(pCtx, 0, "%6i64d %3o %3o\n", (__int64)byte, ch1, ch2);
     197#else
     198                        kmk_builtin_ctx_printf(pCtx, 0, "%6lld %3o %3o\n", (long long)byte, ch1, ch2);
    199199#endif
    200200                    }
     
    206206            /* Check for errors and length differences (EOF). */
    207207            if (ferror(fp1) && rc != ERR_EXIT)
    208                 rc = errmsg(file1, byte, line, lflag);
     208                rc = errmsg(pCtx, file1, byte, line, lflag);
    209209            if (ferror(fp2) && rc != ERR_EXIT)
    210                 rc = errmsg(file2, byte, line, lflag);
     210                rc = errmsg(pCtx, file2, byte, line, lflag);
    211211            if (rc == OK_EXIT)
    212212            {
     
    214214                {
    215215                    if (!feof(fp2))
    216                         rc = eofmsg(file1, byte, line, sflag, lflag);
     216                        rc = eofmsg(pCtx, file1, byte, line, sflag, lflag);
    217217                }
    218218                else if (feof(fp2))
    219                     rc = eofmsg(file2, byte, line, sflag, lflag);
     219                    rc = eofmsg(pCtx, file2, byte, line, sflag, lflag);
    220220            }
    221221
     
    224224        else
    225225        {
    226             rc = err(ERR_EXIT, "%s", file2);
     226            rc = err(pCtx, ERR_EXIT, "%s", file2);
    227227            close(fd2dup);
    228228        }
    229229    }
    230230    else
    231         rc = err(ERR_EXIT, "%s", file2);
     231        rc = err(pCtx, ERR_EXIT, "%s", file2);
    232232
    233233    fclose(fp1);
     
    241241 */
    242242static int
    243 c_regular(int fd1, const char *file1, off_t skip1, off_t len1,
     243c_regular(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, off_t len1,
    244244          int fd2, const char *file2, off_t skip2, off_t len2, int sflag, int lflag)
    245245{
     
    253253
    254254    if (skip1 > len1)
    255         return eofmsg(file1, len1 + 1, 0, sflag, lflag);
     255        return eofmsg(pCtx, file1, len1 + 1, 0, sflag, lflag);
    256256    len1 -= skip1;
    257257    if (skip2 > len2)
    258         return eofmsg(file2, len2 + 1, 0, sflag, lflag);
     258        return eofmsg(pCtx, file2, len2 + 1, 0, sflag, lflag);
    259259    len2 -= skip2;
    260260
     
    290290                dfound = 1;
    291291#ifdef _MSC_VER
    292                 printf("%6I64d %3o %3o\n", (__int64)byte, ch, *p2);
    293 #else
    294                 printf("%6lld %3o %3o\n", (long long)byte, ch, *p2);
     292                kmk_builtin_ctx_printf(pCtx, 0, "%6I64d %3o %3o\n", (__int64)byte, ch, *p2);
     293#else
     294                kmk_builtin_ctx_printf(pCtx, 0, "%6lld %3o %3o\n", (long long)byte, ch, *p2);
    295295#endif
    296296            }
     
    305305
    306306    if (len1 != len2)
    307         return eofmsg(len1 > len2 ? file2 : file1, byte, line, sflag, lflag);
     307        return eofmsg(pCtx, len1 > len2 ? file2 : file1, byte, line, sflag, lflag);
    308308    if (dfound)
    309309        return DIFF_EXIT;
     
    311311
    312312l_mmap_failed:
    313     return c_special(fd1, file1, skip1, fd2, file2, skip2, lflag, sflag);
     313    return c_special(pCtx, fd1, file1, skip1, fd2, file2, skip2, lflag, sflag);
    314314}
    315315
     
    320320 */
    321321static int
    322 c_regular(int fd1, const char *file1, off_t skip1, off_t len1,
     322c_regular(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, off_t len1,
    323323          int fd2, const char *file2, off_t skip2, off_t len2, int sflag, int lflag)
    324324{
     
    332332
    333333    if (skip1 > len1)
    334         return eofmsg(file1, len1 + 1, 0, sflag, lflag);
     334        return eofmsg(pCtx, file1, len1 + 1, 0, sflag, lflag);
    335335    len1 -= skip1;
    336336    if (skip2 > len2)
    337         return eofmsg(file2, len2 + 1, 0, sflag, lflag);
     337        return eofmsg(pCtx, file2, len2 + 1, 0, sflag, lflag);
    338338    len2 -= skip2;
    339339
     
    343343    {
    344344        if (skip1 && lseek(fd1, 0, SEEK_SET) < 0)
    345             return err(1, "seek failed");
     345            return err(pCtx, 1, "seek failed");
    346346        goto l_special;
    347347    }
     
    383383                    free(b1);
    384384                    free(b2);
    385                     return diffmsg(file1, file2, byte, line, sflag);
     385                    return diffmsg(pCtx, file1, file2, byte, line, sflag);
    386386                }
    387387                dfound = 1;
    388388#ifdef _MSC_VER
    389                 printf("%6I64d %3o %3o\n", (__int64)byte, ch, *p2);
    390 #else
    391                 printf("%6lld %3o %3o\n", (long long)byte, ch, *p2);
     389                kmk_builtin_ctx_printf(pCtx, 0, "%6I64d %3o %3o\n", (__int64)byte, ch, *p2);
     390#else
     391                kmk_builtin_ctx_printf(pCtx, 0, "%6lld %3o %3o\n", (long long)byte, ch, *p2);
    392392#endif
    393393            }
     
    400400
    401401    if (len1 != len2)
    402         return eofmsg(len1 > len2 ? file2 : file1, byte, line, sflag, lflag);
     402        return eofmsg(pCtx, len1 > len2 ? file2 : file1, byte, line, sflag, lflag);
    403403    if (dfound)
    404404        return DIFF_EXIT;
     
    409409        ||  lseek(fd2, 0, SEEK_SET) < 0)
    410410    {
    411         err(1, "seek failed");
     411        err(pCtx, 1, "seek failed");
    412412        free(b1);
    413413        free(b2);
     
    418418    free(b2);
    419419l_special:
    420     return c_special(fd1, file1, skip1, fd2, file2, skip2, lflag, sflag);
     420    return c_special(pCtx, fd1, file1, skip1, fd2, file2, skip2, lflag, sflag);
    421421}
    422422#endif  /* non-mmap c_regular */
     
    427427 */
    428428int
    429 cmp_fd_and_fd_ex(int fd1, const char *file1, off_t skip1,
     429cmp_fd_and_fd_ex(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1,
    430430                 int fd2, const char *file2, off_t skip2,
    431431                 int sflag, int lflag, int special)
     
    435435
    436436    if (fstat(fd1, &st1))
    437         return err(ERR_EXIT, "%s", file1);
     437        return err(pCtx, ERR_EXIT, "%s", file1);
    438438    if (fstat(fd2, &st2))
    439         return err(ERR_EXIT, "%s", file2);
     439        return err(pCtx, ERR_EXIT, "%s", file2);
    440440
    441441    if (    !S_ISREG(st1.st_mode)
    442442        ||  !S_ISREG(st2.st_mode)
    443443        ||  special)
    444         rc = c_special(fd1, file1, skip1,
     444        rc = c_special(pCtx, fd1, file1, skip1,
    445445                       fd2, file2, skip2, sflag, lflag);
    446446    else
    447         rc = c_regular(fd1, file1, skip1, st1.st_size,
     447        rc = c_regular(pCtx, fd1, file1, skip1, st1.st_size,
    448448                       fd2, file2, skip2, st2.st_size, sflag, lflag);
    449449    return rc;
     
    455455 */
    456456int
    457 cmp_fd_and_fd(int fd1, const char *file1,
     457cmp_fd_and_fd(PKMKBUILTINCTX pCtx, int fd1, const char *file1,
    458458              int fd2, const char *file2,
    459459              int sflag, int lflag, int special)
    460460{
    461     return cmp_fd_and_fd_ex(fd1, file1, 0, fd2, file2, 0, sflag, lflag, special);
     461    return cmp_fd_and_fd_ex(pCtx, fd1, file1, 0, fd2, file2, 0, sflag, lflag, special);
    462462}
    463463
     
    467467 */
    468468int
    469 cmp_fd_and_file_ex(int fd1, const char *file1, off_t skip1,
     469cmp_fd_and_file_ex(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1,
    470470                   const char *file2, off_t skip2,
    471471                   int sflag, int lflag, int special)
     
    484484    if (fd2 >= 0)
    485485    {
    486         rc = cmp_fd_and_fd_ex(fd1, file1, skip1,
     486        rc = cmp_fd_and_fd_ex(pCtx, fd1, file1, skip1,
    487487                              fd2, file2, skip2, sflag, lflag, special);
    488488        close(fd2);
     
    491491    {
    492492        if (!sflag)
    493             warn("%s", file2);
     493            warn(pCtx, "%s", file2);
    494494        rc = ERR_EXIT;
    495495    }
     
    502502 */
    503503int
    504 cmp_fd_and_file(int fd1, const char *file1,
     504cmp_fd_and_file(PKMKBUILTINCTX pCtx, int fd1, const char *file1,
    505505                const char *file2,
    506506                int sflag, int lflag, int special)
    507507{
    508     return cmp_fd_and_file_ex(fd1, file1, 0,
     508    return cmp_fd_and_file_ex(pCtx, fd1, file1, 0,
    509509                                   file2, 0, sflag, lflag, special);
    510510}
     
    515515 */
    516516int
    517 cmp_file_and_file_ex(const char *file1, off_t skip1,
     517cmp_file_and_file_ex(PKMKBUILTINCTX pCtx, const char *file1, off_t skip1,
    518518                     const char *file2, off_t skip2,
    519519                     int sflag, int lflag, int special)
     
    523523
    524524    if (lflag && sflag)
    525         return errx(ERR_EXIT, "only one of -l and -s may be specified");
     525        return errx(pCtx, ERR_EXIT, "only one of -l and -s may be specified");
    526526
    527527    if (!strcmp(file1, "-"))
    528528    {
    529529        if (!strcmp(file2, "-"))
    530             return errx(ERR_EXIT, "standard input may only be specified once");
     530            return errx(pCtx, ERR_EXIT, "standard input may only be specified once");
    531531        file1 = "stdin";
    532532        fd1 = 1;
     
    537537    if (fd1 >= 0)
    538538    {
    539         rc = cmp_fd_and_file_ex(fd1, file1, skip1,
     539        rc = cmp_fd_and_file_ex(pCtx, fd1, file1, skip1,
    540540                                     file2, skip2, sflag, lflag, special);
    541541        close(fd1);
     
    544544    {
    545545        if (!sflag)
    546             warn("%s", file1);
     546            warn(pCtx, "%s", file1);
    547547        rc = ERR_EXIT;
    548548    }
     
    556556 */
    557557int
    558 cmp_file_and_file(const char *file1, const char *file2, int sflag, int lflag, int special)
    559 {
    560     return cmp_file_and_file_ex(file1, 0, file2, 0, sflag, lflag, special);
    561 }
    562 
     558cmp_file_and_file(PKMKBUILTINCTX pCtx, const char *file1, const char *file2, int sflag, int lflag, int special)
     559{
     560    return cmp_file_and_file_ex(pCtx, file1, 0, file2, 0, sflag, lflag, special);
     561}
     562
Note: See TracChangeset for help on using the changeset viewer.