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/touch.c

    r3140 r3192  
    9191typedef struct KMKTOUCHOPTS
    9292{
     93    /** Command execution context. */
     94    PKMKBUILTINCTX  pCtx;
    9395    /** What timestamps to modify on the files. */
    9496    KMKTOUCHTARGET  enmWhatToTouch;
     
    111113typedef KMKTOUCHOPTS *PKMKTOUCHOPTS;
    112114
    113 
    114 
    115 static int touch_error(const char *pszMsg, ...)
    116 {
    117     va_list va;
    118     fputs(TOUCH_NAME ": error: ", stderr);
    119     va_start(va, pszMsg);
    120     vfprintf(stderr, pszMsg, va);
    121     va_end(va);
    122     fputc('\n', stderr);
    123     return 1;
    124 }
    125 
    126 static int touch_syntax(const char *pszMsg, ...)
    127 {
    128     va_list va;
    129     fputs(TOUCH_NAME ": syntax error: ", stderr);
    130     va_start(va, pszMsg);
    131     vfprintf(stderr, pszMsg, va);
    132     va_end(va);
    133     fputc('\n', stderr);
    134     return 2;
    135 }
    136115
    137116static int touch_usage(void)
     
    189168 * Parses adjustment value: [-][[hh]mm]SS
    190169 */
    191 static int touch_parse_adjust(const char *pszValue, int *piAdjustValue)
     170static int touch_parse_adjust(PKMKBUILTINCTX pCtx, const char *pszValue, int *piAdjustValue)
    192171{
    193172    const char * const  pszInValue = pszValue;
     
    210189            if (   !IS_DIGIT(pszValue[0], 9)
    211190                || !IS_DIGIT(pszValue[0], 9))
    212                 return touch_syntax("Malformed hour part of -A value: %s", pszInValue);
     191                return errx(pCtx, 2, "Malformed hour part of -A value: %s", pszInValue);
    213192            *piAdjustValue = TWO_CHARS_TO_INT(pszValue[0], pszValue[1]) * 60 * 60;
    214193            /* fall thru */
     
    216195            if (   !IS_DIGIT(pszValue[cchValue - 4], 9) /* don't bother limit to 60 minutes */
    217196                || !IS_DIGIT(pszValue[cchValue - 3], 9))
    218                 return touch_syntax("Malformed minute part of -A value: %s", pszInValue);
     197                return errx(pCtx, 2, "Malformed minute part of -A value: %s", pszInValue);
    219198            *piAdjustValue += TWO_CHARS_TO_INT(pszValue[cchValue - 4], pszValue[cchValue - 3]) * 60;
    220199            /* fall thru */
     
    222201            if (   !IS_DIGIT(pszValue[cchValue - 2], 9) /* don't bother limit to 60 seconds */
    223202                || !IS_DIGIT(pszValue[cchValue - 1], 9))
    224                 return touch_syntax("Malformed second part of -A value: %s", pszInValue);
     203                return errx(pCtx, 2, "Malformed second part of -A value: %s", pszInValue);
    225204            *piAdjustValue += TWO_CHARS_TO_INT(pszValue[cchValue - 2], pszValue[cchValue - 1]);
    226205            break;
    227206
    228207        default:
    229             return touch_syntax("Invalid -A value (length): %s", pszInValue);
     208            return errx(pCtx, 2, "Invalid -A value (length): %s", pszInValue);
    230209    }
    231210
     
    241220 * Parse -d timestamp: YYYY-MM-DDThh:mm:SS[.frac][tz]
    242221 */
    243 static int touch_parse_d_ts(const char *pszTs, struct timeval *pDst)
     222static int touch_parse_d_ts(PKMKBUILTINCTX pCtx, const char *pszTs, struct timeval *pDst)
    244223{
    245224    const char * const  pszTsIn = pszTs;
     
    257236        || !IS_DIGIT(pszTs[3], 9)
    258237        || pszTs[4] != '-')
    259         return touch_error("Malformed timestamp '%s' given to -d: expected to start with 4 digit year followed by a dash",
    260                            pszTsIn);
     238        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to start with 4 digit year followed by a dash",
     239                    pszTsIn);
    261240    ExpTime.tm_year = TWO_CHARS_TO_INT(pszTs[0], pszTs[1]) * 100
    262241                    + TWO_CHARS_TO_INT(pszTs[2], pszTs[3])
     
    268247        || !IS_DIGIT(pszTs[1], 9)
    269248        || pszTs[2] != '-')
    270         return touch_error("Malformed timestamp '%s' given to -d: expected to two digit month at position 6 followed by a dash",
    271                            pszTsIn);
     249        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to two digit month at position 6 followed by a dash",
     250                    pszTsIn);
    272251    ExpTime.tm_mon = TWO_CHARS_TO_INT(pszTs[0], pszTs[1]) - 1;
    273252    pszTs += 3;
     
    277256        || !IS_DIGIT(pszTs[1], 9)
    278257        || (pszTs[2] != 'T' && pszTs[2] != 't' && pszTs[2] != ' ') )
    279         return touch_error("Malformed timestamp '%s' given to -d: expected to two digit day of month at position 9 followed by 'T' or space",
    280                            pszTsIn);
     258        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to two digit day of month at position 9 followed by 'T' or space",
     259                    pszTsIn);
    281260    ExpTime.tm_mday = TWO_CHARS_TO_INT(pszTs[0], pszTs[1]);
    282261    pszTs += 3;
     
    286265        || !IS_DIGIT(pszTs[1], 9)
    287266        || pszTs[2] != ':')
    288         return touch_error("Malformed timestamp '%s' given to -d: expected to two digit hour at position 12 followed by colon",
    289                            pszTsIn);
     267        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to two digit hour at position 12 followed by colon",
     268                    pszTsIn);
    290269    ExpTime.tm_hour = TWO_CHARS_TO_INT(pszTs[0], pszTs[1]);
    291270    pszTs += 3;
     
    295274        || !IS_DIGIT(pszTs[1], 9)
    296275        || pszTs[2] != ':')
    297         return touch_error("Malformed timestamp '%s' given to -d: expected to two digit minute at position 15 followed by colon",
    298                            pszTsIn);
     276        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to two digit minute at position 15 followed by colon",
     277                    pszTsIn);
    299278    ExpTime.tm_min = TWO_CHARS_TO_INT(pszTs[0], pszTs[1]);
    300279    pszTs += 3;
     
    303282    if (   !IS_DIGIT(pszTs[0], 5)
    304283        || !IS_DIGIT(pszTs[1], 9))
    305         return touch_error("Malformed timestamp '%s' given to -d: expected to two digit seconds at position 12", pszTsIn);
     284        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to two digit seconds at position 12", pszTsIn);
    306285    ExpTime.tm_sec = TWO_CHARS_TO_INT(pszTs[0], pszTs[1]);
    307286    pszTs += 2;
     
    315294        pszTs++;
    316295        if (!IS_DIGIT(*pszTs, 9))
    317             return touch_error("Malformed timestamp '%s' given to -d: empty fraction", pszTsIn);
     296            return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: empty fraction", pszTsIn);
    318297
    319298        iFactor = 100000;
     
    333312        pszTs++;
    334313        if (*pszTs != '\0')
    335             return touch_error("Malformed timestamp '%s' given to -d: Unexpected character(s) after zulu time indicator at end of timestamp",
    336                                pszTsIn);
     314            return errx(pCtx, 2,
     315                        "Malformed timestamp '%s' given to -d: Unexpected character(s) after zulu time indicator at end of timestamp",
     316                        pszTsIn);
    337317    }
    338318    else if (*pszTs != '\0')
    339         return touch_error("Malformed timestamp '%s' given to -d: expected to 'Z' (zulu) or nothing at end of timestamp", pszTsIn);
     319        return errx(pCtx, 2, "Malformed timestamp '%s' given to -d: expected to 'Z' (zulu) or nothing at end of timestamp", pszTsIn);
    340320
    341321    /*
     
    352332#endif
    353333        if (pDst->tv_sec == -1)
    354             return touch_error("timegm failed on '%s': %s", pszTs, strerror(errno));
     334            return errx(pCtx, 1, "timegm failed on '%s': %s", pszTs, strerror(errno));
    355335    }
    356336    else
     
    358338        pDst->tv_sec = mktime(&ExpTime);
    359339        if (pDst->tv_sec == -1)
    360             return touch_error("mktime failed on '%s': %s", pszTs, strerror(errno));
     340            return errx(pCtx, 1, "mktime failed on '%s': %s", pszTs, strerror(errno));
    361341    }
    362342    return 0;
     
    367347 * Parse -t timestamp: [[CC]YY]MMDDhhmm[.SS]
    368348 */
    369 static int touch_parse_ts(const char *pszTs, struct timeval *pDst)
     349static int touch_parse_ts(PKMKBUILTINCTX pCtx, const char *pszTs, struct timeval *pDst)
    370350{
    371351    size_t const    cchTs = strlen(pszTs);
     
    380360     */
    381361    if ((cchTs & 1) && pszTs[cchTs - 3] != '.')
    382         return touch_error("Invalid timestamp given to -t: %s", pszTs);
     362        return errx(pCtx, 2, "Invalid timestamp given to -t: %s", pszTs);
    383363    switch (cchTs)
    384364    {
     
    392372        case 8 + 3 + 2 + 2: /* CCYYMMDDhhmm.SS */
    393373            if (pszTs[cchTs - 3] != '.')
    394                 return touch_error("Invalid timestamp (-t) '%s': missing dot for seconds part", pszTs);
     374                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': missing dot for seconds part", pszTs);
    395375            if (   !IS_DIGIT(pszTs[cchTs - 2], 5)
    396376                || !IS_DIGIT(pszTs[cchTs - 1], 9))
    397                 return touch_error("Invalid timestamp (-t) '%s': malformed seconds part", pszTs);
     377                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed seconds part", pszTs);
    398378            cchTsNoSec = cchTs - 3;
    399379            break;
    400380        default:
    401             return touch_error("Invalid timestamp (-t) '%s': wrong length (%d)", pszTs, (int)cchTs);
     381            return errx(pCtx, 1, "Invalid timestamp (-t) '%s': wrong length (%d)", pszTs, (int)cchTs);
    402382    }
    403383
     
    407387            if (   !IS_DIGIT(pszTs[cchTsNoSec - 12], 9)
    408388                || !IS_DIGIT(pszTs[cchTsNoSec - 11], 9))
    409                 return touch_error("Invalid timestamp (-t) '%s': malformed CC part", pszTs);
     389                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed CC part", pszTs);
    410390            /* fall thru */
    411391        case 8 + 2:         /*   YYMMDDhhmm */
    412392            if (   !IS_DIGIT(pszTs[cchTsNoSec - 10], 9)
    413393                || !IS_DIGIT(pszTs[cchTsNoSec -  9], 9))
    414                 return touch_error("Invalid timestamp (-t) '%s': malformed YY part", pszTs);
     394                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed YY part", pszTs);
    415395            /* fall thru */
    416396        case 8:             /*     MMDDhhmm */
    417397            if (   !IS_DIGIT(pszTs[cchTsNoSec - 8], 1)
    418398                || !IS_DIGIT(pszTs[cchTsNoSec - 7], 9) )
    419                 return touch_error("Invalid timestamp (-t) '%s': malformed month part", pszTs);
     399                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed month part", pszTs);
    420400            if (   !IS_DIGIT(pszTs[cchTsNoSec - 6], 3)
    421401                || !IS_DIGIT(pszTs[cchTsNoSec - 5], 9) )
    422                 return touch_error("Invalid timestamp (-t) '%s': malformed day part", pszTs);
     402                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed day part", pszTs);
    423403            if (   !IS_DIGIT(pszTs[cchTsNoSec - 4], 2)
    424404                || !IS_DIGIT(pszTs[cchTsNoSec - 3], 9) )
    425                 return touch_error("Invalid timestamp (-t) '%s': malformed hour part", pszTs);
     405                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed hour part", pszTs);
    426406            if (   !IS_DIGIT(pszTs[cchTsNoSec - 2], 5)
    427407                || !IS_DIGIT(pszTs[cchTsNoSec - 1], 9) )
    428                 return touch_error("Invalid timestamp (-t) '%s': malformed minute part", pszTs);
     408                return errx(pCtx, 2, "Invalid timestamp (-t) '%s': malformed minute part", pszTs);
    429409            break;
    430410    }
     
    435415    rc = gettimeofday(&Now, NULL);
    436416    if (rc != 0)
    437         return touch_error("gettimeofday failed: %s", strerror(errno));
     417        return errx(pCtx, 1, "gettimeofday failed: %s", strerror(errno));
    438418
    439419    pExpTime = localtime_r(&Now.tv_sec, &ExpTime);
    440420    if (pExpTime == NULL)
    441         return touch_error("localtime_r failed: %s", strerror(errno));
     421        return errx(pCtx, 1, "localtime_r failed: %s", strerror(errno));
    442422
    443423    /*
     
    475455    if (pDst->tv_sec != -1)
    476456        return 0;
    477     return touch_error("mktime failed on '%s': %s", pszTs, strerror(errno));
     457    return errx(pCtx, 1, "mktime failed on '%s': %s", pszTs, strerror(errno));
    478458}
    479459
     
    482462 * Check for old timestamp: MMDDhhmm[YY]
    483463 */
    484 static int touch_parse_old_ts(const char *pszOldTs, time_t Now, struct timeval *pDst)
     464static int touch_parse_old_ts(PKMKBUILTINCTX pCtx, const char *pszOldTs, time_t Now, struct timeval *pDst)
    485465{
    486466    /*
     
    509489        pExpTime = localtime_r(&Now, &ExpTime);
    510490        if (pExpTime == NULL)
    511             return touch_error("localtime_r failed: %s", strerror(errno));
     491            return errx(pCtx, 1, "localtime_r failed: %s", strerror(errno));
    512492
    513493        /*
     
    535515        if (pDst->tv_sec != -1)
    536516            return 0;
    537         return touch_error("mktime failed on '%s': %s", pszOldTs, strerror(errno));
     517        return errx(pCtx, 1, "mktime failed on '%s': %s", pszOldTs, strerror(errno));
    538518    }
    539519
     
    544524
    545525/**
    546  * Parses the arguments into pOpts.
     526 * Parses the arguments into pThis.
    547527 *
    548528 * @returns exit code.
    549  * @param   pOpts               Options structure to return the parsed info in.
     529 * @param   pThis               Options structure to return the parsed info in.
    550530 *                              Caller initalizes this with defaults.
    551531 * @param   cArgs               The number of arguments.
     
    554534 *                              files.
    555535 */
    556 static int touch_parse_args(PKMKTOUCHOPTS pOpts, int cArgs, char **papszArgs, KBOOL *pfExit)
     536static int touch_parse_args(PKMKTOUCHOPTS pThis, int cArgs, char **papszArgs, KBOOL *pfExit)
    557537{
    558538    int iAdjustValue = 0;
     
    583563                {
    584564                    while (++iArg < cArgs)
    585                         pOpts->papszFiles[pOpts->cFiles++] = papszArgs[iArg];
     565                        pThis->papszFiles[pThis->cFiles++] = papszArgs[iArg];
    586566                    break; /* '--' */
    587567                }
     
    626606                    return kbuild_version(papszArgs[0]);
    627607                else
    628                     return touch_syntax("Unknown option: --%s", pszLong);
     608                    return errx(pThis->pCtx, 2, "Unknown option: --%s", pszLong);
    629609            }
    630610
     
    651631                            pszValue = papszArgs[++iArg];
    652632                        else
    653                             return touch_syntax("Option -%c requires a value", ch);
     633                            return errx(pThis->pCtx, 2, "Option -%c requires a value", ch);
    654634                        break;
    655635
     
    663643                    /* -A [-][[HH]MM]SS */
    664644                    case 'A':
    665                         rc = touch_parse_adjust(pszValue, &iAdjustValue);
     645                        rc = touch_parse_adjust(pThis->pCtx, pszValue, &iAdjustValue);
    666646                        if (rc != 0)
    667647                            return rc;
    668                         if (pOpts->enmAction != kTouchActionSet)
     648                        if (pThis->enmAction != kTouchActionSet)
    669649                        {
    670                             pOpts->enmAction = kTouchActionAdjust;
    671                             pOpts->NewATime.tv_sec  = iAdjustValue;
    672                             pOpts->NewATime.tv_usec = 0;
    673                             pOpts->NewMTime = pOpts->NewATime;
     650                            pThis->enmAction = kTouchActionAdjust;
     651                            pThis->NewATime.tv_sec  = iAdjustValue;
     652                            pThis->NewATime.tv_usec = 0;
     653                            pThis->NewMTime = pThis->NewATime;
    674654                        }
    675655                        /* else: applied after parsing everything. */
     
    677657
    678658                    case 'a':
    679                         pOpts->enmWhatToTouch = kTouchAccessOnly;
     659                        pThis->enmWhatToTouch = kTouchAccessOnly;
    680660                        break;
    681661
    682662                    case 'c':
    683                         pOpts->fCreate = K_FALSE;
     663                        pThis->fCreate = K_FALSE;
    684664                        break;
    685665
    686666                    case 'd':
    687                         rc = touch_parse_d_ts(pszValue, &pOpts->NewATime);
     667                        rc = touch_parse_d_ts(pThis->pCtx, pszValue, &pThis->NewATime);
    688668                        if (rc != 0)
    689669                            return rc;
    690                         pOpts->enmAction = kTouchActionSet;
    691                         pOpts->NewMTime  = pOpts->NewATime;
     670                        pThis->enmAction = kTouchActionSet;
     671                        pThis->NewMTime  = pThis->NewATime;
    692672                        break;
    693673
     
    697677
    698678                    case 'h':
    699                         pOpts->fDereference = K_FALSE;
     679                        pThis->fDereference = K_FALSE;
    700680                        break;
    701681
    702682                    case 'm':
    703                         pOpts->enmWhatToTouch = kTouchModifyOnly;
     683                        pThis->enmWhatToTouch = kTouchModifyOnly;
    704684                        break;
    705685
     
    708688                        struct stat St;
    709689                        if (stat(pszValue, &St) != 0)
    710                             return touch_error("Failed to stat '%s' (-r option): %s", pszValue, strerror(errno));
    711 
    712                         pOpts->enmAction = kTouchActionSet;
    713                         pOpts->NewATime.tv_sec  = St.st_atime;
    714                         pOpts->NewMTime.tv_sec  = St.st_mtime;
     690                            return errx(pThis->pCtx, 1, "Failed to stat '%s' (-r option): %s", pszValue, strerror(errno));
     691
     692                        pThis->enmAction = kTouchActionSet;
     693                        pThis->NewATime.tv_sec  = St.st_atime;
     694                        pThis->NewMTime.tv_sec  = St.st_mtime;
    715695#if FILE_TIMESTAMP_HI_RES
    716                         pOpts->NewATime.tv_usec = St.st_atim.tv_nsec / 1000;
    717                         pOpts->NewMTime.tv_usec = St.st_mtim.tv_nsec / 1000;
     696                        pThis->NewATime.tv_usec = St.st_atim.tv_nsec / 1000;
     697                        pThis->NewMTime.tv_usec = St.st_mtim.tv_nsec / 1000;
    718698#else
    719                         pOpts->NewATime.tv_usec = 0;
    720                         pOpts->NewMTime.tv_usec = 0;
     699                        pThis->NewATime.tv_usec = 0;
     700                        pThis->NewMTime.tv_usec = 0;
    721701#endif
    722702                        break;
     
    724704
    725705                    case 't':
    726                         rc = touch_parse_ts(pszValue, &pOpts->NewATime);
     706                        rc = touch_parse_ts(pThis->pCtx, pszValue, &pThis->NewATime);
    727707                        if (rc != 0)
    728708                            return rc;
    729                         pOpts->enmAction = kTouchActionSet;
    730                         pOpts->NewMTime  = pOpts->NewATime;
     709                        pThis->enmAction = kTouchActionSet;
     710                        pThis->NewMTime  = pThis->NewATime;
    731711                        break;
    732712
     
    734714                        if (   strcmp(pszValue, "atime") == 0
    735715                            || strcmp(pszValue, "access") == 0)
    736                             pOpts->enmWhatToTouch = kTouchAccessOnly;
     716                            pThis->enmWhatToTouch = kTouchAccessOnly;
    737717                        else if (   strcmp(pszValue, "mtime") == 0
    738718                                 || strcmp(pszValue, "modify") == 0)
    739                             pOpts->enmWhatToTouch = kTouchModifyOnly;
     719                            pThis->enmWhatToTouch = kTouchModifyOnly;
    740720                        else
    741                             return touch_syntax("Unknown --time value: %s", pszValue);
     721                            return errx(pThis->pCtx, 2, "Unknown --time value: %s", pszValue);
    742722                        break;
    743723
     
    746726
    747727                    default:
    748                         return touch_syntax("Unknown option: -%c (%c%s)", ch, ch, pszArg);
     728                        return errx(pThis->pCtx, 2, "Unknown option: -%c (%c%s)", ch, ch, pszArg);
    749729                }
    750730
     
    752732        }
    753733        else
    754             pOpts->papszFiles[pOpts->cFiles++] = papszArgs[iArg];
     734            pThis->papszFiles[pThis->cFiles++] = papszArgs[iArg];
    755735    }
    756736
     
    758738     * Allow adjusting specified timestamps too like BSD does.
    759739     */
    760     if (   pOpts->enmAction == kTouchActionSet
     740    if (   pThis->enmAction == kTouchActionSet
    761741        && iAdjustValue != 0)
    762742    {
    763         if (   pOpts->enmWhatToTouch == kTouchAccessAndModify
    764             || pOpts->enmWhatToTouch == kTouchAccessOnly)
    765             pOpts->NewATime.tv_sec += iAdjustValue;
    766         if (   pOpts->enmWhatToTouch == kTouchAccessAndModify
    767             || pOpts->enmWhatToTouch == kTouchModifyOnly)
    768             pOpts->NewMTime.tv_sec += iAdjustValue;
     743        if (   pThis->enmWhatToTouch == kTouchAccessAndModify
     744            || pThis->enmWhatToTouch == kTouchAccessOnly)
     745            pThis->NewATime.tv_sec += iAdjustValue;
     746        if (   pThis->enmWhatToTouch == kTouchAccessAndModify
     747            || pThis->enmWhatToTouch == kTouchModifyOnly)
     748            pThis->NewMTime.tv_sec += iAdjustValue;
    769749    }
    770750
     
    772752     * Check for old timestamp: MMDDhhmm[YY]
    773753     */
    774     if (   pOpts->enmAction == kTouchActionCurrent
    775         && pOpts->cFiles >= 2)
     754    if (   pThis->enmAction == kTouchActionCurrent
     755        && pThis->cFiles >= 2)
    776756    {
    777757        struct timeval OldTs;
    778         rc = touch_parse_old_ts(pOpts->papszFiles[0], pOpts->NewATime.tv_sec, &OldTs);
     758        rc = touch_parse_old_ts(pThis->pCtx, pThis->papszFiles[0], pThis->NewATime.tv_sec, &OldTs);
    779759        if (rc == 0)
    780760        {
    781761            int iFile;
    782762
    783             pOpts->NewATime = OldTs;
    784             pOpts->NewMTime = OldTs;
    785             pOpts->enmAction = kTouchActionSet;
    786 
    787             for (iFile = 1; iFile < pOpts->cFiles; iFile++)
    788                 pOpts->papszFiles[iFile - 1] = pOpts->papszFiles[iFile];
    789             pOpts->cFiles--;
     763            pThis->NewATime = OldTs;
     764            pThis->NewMTime = OldTs;
     765            pThis->enmAction = kTouchActionSet;
     766
     767            for (iFile = 1; iFile < pThis->cFiles; iFile++)
     768                pThis->papszFiles[iFile - 1] = pThis->papszFiles[iFile];
     769            pThis->cFiles--;
    790770        }
    791771        else if (rc > 0)
     
    796776     * Check that we've found at least one file argument.
    797777     */
    798     if (pOpts->cFiles > 0)
     778    if (pThis->cFiles > 0)
    799779    {
    800780        *pfExit = K_FALSE;
    801781        return 0;
    802782    }
    803     return touch_syntax("No file specified");
     783    return errx(pThis->pCtx, 2, "No file specified");
    804784}
    805785
     
    809789 *
    810790 * @returns exit code.
    811  * @param   pOpts               The options.
     791 * @param   pThis               The options.
    812792 * @param   pszFile             The file to touch.
    813793 */
    814 static int touch_process_file(PKMKTOUCHOPTS pOpts, const char *pszFile)
     794static int touch_process_file(PKMKTOUCHOPTS pThis, const char *pszFile)
    815795{
    816796    int             fd;
     
    823803     * in effect, we silently skip the file if it doesn't already exist.
    824804     */
    825     if (pOpts->fDereference)
     805    if (pThis->fDereference)
    826806        rc = stat(pszFile, &St);
    827807    else
     
    830810    {
    831811        if (errno != ENOENT)
    832             return touch_error("Failed to stat '%s': %s", pszFile, strerror(errno));
    833 
    834         if (!pOpts->fCreate)
     812            return errx(pThis->pCtx, 1, "Failed to stat '%s': %s", pszFile, strerror(errno));
     813
     814        if (!pThis->fCreate)
    835815            return 0;
    836         fd = open(pszFile, O_WRONLY | O_CREAT, 0666);
     816        fd = open(pszFile, O_WRONLY | O_CREAT | KMK_OPEN_NO_INHERIT, 0666);
    837817        if (fd == -1)
    838             return touch_error("Failed to create '%s': %s", pszFile, strerror(errno));
     818            return errx(pThis->pCtx, 1, "Failed to create '%s': %s", pszFile, strerror(errno));
    839819
    840820        /* If we're not setting the current time, we may need value stat info
    841821           on the file, so get it thru the file descriptor before closing it. */
    842         if (pOpts->enmAction == kTouchActionCurrent)
     822        if (pThis->enmAction == kTouchActionCurrent)
    843823            rc = 0;
    844824        else
    845825            rc = fstat(fd, &St);
    846826        if (close(fd) != 0)
    847             return touch_error("Failed to close '%s' after creation: %s", pszFile, strerror(errno));
     827            return errx(pThis->pCtx, 1, "Failed to close '%s' after creation: %s", pszFile, strerror(errno));
    848828        if (rc != 0)
    849             return touch_error("Failed to fstat '%s' after creation: %s", pszFile, strerror(errno));
     829            return errx(pThis->pCtx, 1, "Failed to fstat '%s' after creation: %s", pszFile, strerror(errno));
    850830
    851831        /* We're done now if we're setting the current time. */
    852         if (pOpts->enmAction == kTouchActionCurrent)
     832        if (pThis->enmAction == kTouchActionCurrent)
    853833            return 0;
    854834    }
     
    866846    aTimes[1].tv_usec = 0;
    867847#endif
    868     if (   pOpts->enmWhatToTouch == kTouchAccessAndModify
    869         || pOpts->enmWhatToTouch == kTouchAccessOnly)
    870     {
    871         if (   pOpts->enmAction == kTouchActionCurrent
    872             || pOpts->enmAction == kTouchActionSet)
    873             aTimes[0] = pOpts->NewATime;
     848    if (   pThis->enmWhatToTouch == kTouchAccessAndModify
     849        || pThis->enmWhatToTouch == kTouchAccessOnly)
     850    {
     851        if (   pThis->enmAction == kTouchActionCurrent
     852            || pThis->enmAction == kTouchActionSet)
     853            aTimes[0] = pThis->NewATime;
    874854        else
    875             aTimes[0].tv_sec += pOpts->NewATime.tv_sec;
    876     }
    877     if (   pOpts->enmWhatToTouch == kTouchAccessAndModify
    878         || pOpts->enmWhatToTouch == kTouchModifyOnly)
    879     {
    880         if (   pOpts->enmAction == kTouchActionCurrent
    881             || pOpts->enmAction == kTouchActionSet)
    882             aTimes[1] = pOpts->NewMTime;
     855            aTimes[0].tv_sec += pThis->NewATime.tv_sec;
     856    }
     857    if (   pThis->enmWhatToTouch == kTouchAccessAndModify
     858        || pThis->enmWhatToTouch == kTouchModifyOnly)
     859    {
     860        if (   pThis->enmAction == kTouchActionCurrent
     861            || pThis->enmAction == kTouchActionSet)
     862            aTimes[1] = pThis->NewMTime;
    883863        else
    884             aTimes[1].tv_sec += pOpts->NewMTime.tv_sec;
     864            aTimes[1].tv_sec += pThis->NewMTime.tv_sec;
    885865    }
    886866
     
    891871     * may do more than what we want (st_ctime).)
    892872     */
    893     if (pOpts->fDereference)
     873    if (pThis->fDereference)
    894874        rc = utimes(pszFile, aTimes);
    895875    else
     
    897877    if (rc != 0)
    898878    {
    899         if (pOpts->enmAction == kTouchActionCurrent)
     879        if (pThis->enmAction == kTouchActionCurrent)
    900880        {
    901             if (pOpts->fDereference)
     881            if (pThis->fDereference)
    902882                rc = utimes(pszFile, NULL);
    903883            else
     
    905885        }
    906886        if (rc != 0)
    907             rc = touch_error("%stimes failed on '%s': %s", pOpts->fDereference ? "" : "l", pszFile, strerror(errno));
     887            rc = errx(pThis->pCtx, 1, "%stimes failed on '%s': %s", pThis->fDereference ? "" : "l", pszFile, strerror(errno));
    908888    }
    909889
     
    913893
    914894/**
    915  * The function that does almost everything here... ugly.
     895 * Actual main function for the touch command.
    916896 */
    917 #ifdef KMK
    918 int kmk_builtin_touch(int argc, char **argv, char **envp)
    919 #else
    920 int main(int argc, char **argv, char **envp)
    921 #endif
     897int kmk_builtin_touch(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
    922898{
    923899    int             rc;
    924     KMKTOUCHOPTS    Opts;
     900    KMKTOUCHOPTS    This;
    925901    K_NOREF(envp);
    926902
     
    928904     * Initialize options with defaults and parse them.
    929905     */
    930     Opts.enmWhatToTouch = kTouchAccessAndModify;
    931     Opts.enmAction      = kTouchActionCurrent;
    932     Opts.fCreate        = K_TRUE;
    933     Opts.fDereference   = K_TRUE;
    934     Opts.cFiles         = 0;
    935     Opts.papszFiles     = (char **)calloc(argc, sizeof(char *));
    936     if (Opts.papszFiles)
    937     {
    938         rc = gettimeofday(&Opts.NewATime, NULL);
     906    This.pCtx           = pCtx;
     907    This.enmWhatToTouch = kTouchAccessAndModify;
     908    This.enmAction      = kTouchActionCurrent;
     909    This.fCreate        = K_TRUE;
     910    This.fDereference   = K_TRUE;
     911    This.cFiles         = 0;
     912    This.papszFiles     = (char **)calloc(argc, sizeof(char *));
     913    if (This.papszFiles)
     914    {
     915        rc = gettimeofday(&This.NewATime, NULL);
    939916        if (rc == 0)
    940917        {
    941918            KBOOL fExit;
    942             Opts.NewMTime = Opts.NewATime;
    943 
    944             rc = touch_parse_args(&Opts, argc, argv, &fExit);
     919            This.NewMTime = This.NewATime;
     920
     921            rc = touch_parse_args(&This, argc, argv, &fExit);
    945922            if (rc == 0 && !fExit)
    946923            {
     
    949926                 */
    950927                int iFile;
    951                 for (iFile = 0; iFile < Opts.cFiles; iFile++)
     928                for (iFile = 0; iFile < This.cFiles; iFile++)
    952929                {
    953                     int rc2 = touch_process_file(&Opts, Opts.papszFiles[iFile]);
     930                    int rc2 = touch_process_file(&This, This.papszFiles[iFile]);
    954931                    if (rc2 != 0 && rc == 0)
    955932                        rc = rc2;
     
    958935        }
    959936        else
    960             rc = touch_error("gettimeofday failed: %s", strerror(errno));
    961         free(Opts.papszFiles);
     937            rc = errx(pCtx, 2, "gettimeofday failed: %s", strerror(errno));
     938        free(This.papszFiles);
    962939    }
    963940    else
    964         rc = touch_error("calloc failed");
     941        rc = errx(pCtx, 2, "calloc failed");
    965942    return rc;
    966943}
    967944
     945#ifdef KMK_BUILTIN_STANDALONE
     946int main(int argc, char **argv, char **envp)
     947{
     948    KMKBUILTINCTX Ctx = { "kmk_touch", NULL };
     949    return kmk_builtin_touch(argc, argv, envp, &Ctx);
     950}
     951#endif
     952
Note: See TracChangeset for help on using the changeset viewer.