Ignore:
Timestamp:
Mar 31, 2018, 1:27:52 AM (7 years ago)
Author:
bird
Message:

kmk_cp: use getopt_r and stop using global and static variables.

File:
1 edited

Legend:

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

    r3219 r3221  
    3636#endif /* not lint */
    3737
     38/*********************************************************************************************************************************
     39*   Header Files                                                                                                                 *
     40*********************************************************************************************************************************/
    3841#define MSC_DO_64_BIT_IO
    3942#include "config.h"
     
    7275#include "cmp_extern.h"
    7376
     77
     78/*********************************************************************************************************************************
     79*   Defined Constants And Macros                                                                                                 *
     80*********************************************************************************************************************************/
    7481#define cp_pct(x,y)     (int)(100.0 * (double)(x) / (double)(y))
    7582
    7683#ifndef MAXBSIZE
    77 # define MAXBSIZE 0x20000
     84# define MAXBSIZE 0x10000
    7885#endif
    7986#ifndef O_BINARY
     
    8794
    8895int
    89 copy_file(PKMKBUILTINCTX pCtx, const FTSENT *entp, int dne, int changed_only, int *pcopied)
    90 {
    91         static char buf[MAXBSIZE];
     96copy_file(CPUTILSINSTANCE *pThis, const FTSENT *entp, int dne, int changed_only, int *pcopied)
     97{
     98        /*static*/ char buf[MAXBSIZE];
    9299        struct stat *fs;
    93100        int ch, checkch, from_fd, rcount, rval, to_fd;
     
    103110
    104111        if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) == -1) {
    105                 warn(pCtx, "open: %s", entp->fts_path);
     112                warn(pThis->pCtx, "open: %s", entp->fts_path);
    106113                return (1);
    107114        }
     
    120127                /* compare the files first if requested */
    121128                if (changed_only) {
    122                         if (cmp_fd_and_file(pCtx, from_fd, entp->fts_path, to.p_path,
     129                        if (cmp_fd_and_file(pThis->pCtx, from_fd, entp->fts_path, pThis->to.p_path,
    123130                                            1 /* silent */, 0 /* lflag */,
    124131                                            0 /* special */) == OK_EXIT) {
     
    129136                                close(from_fd);
    130137                                if ((from_fd = open(entp->fts_path, O_RDONLY | O_BINARY | KMK_OPEN_NO_INHERIT, 0)) == -1) {
    131                                         warn(pCtx, "open: %s", entp->fts_path);
     138                                        warn(pThis->pCtx, "open: %s", entp->fts_path);
    132139                                        return (1);
    133140                                }
     
    136143
    137144#define YESNO "(y/n [n]) "
    138                 if (nflag) {
    139                         if (vflag)
    140                                 kmk_builtin_ctx_printf(pCtx, 0, "%s not overwritten\n", to.p_path);
     145                if (pThis->nflag) {
     146                        if (pThis->vflag)
     147                                kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s not overwritten\n", pThis->to.p_path);
    141148                        return (0);
    142                 } else if (iflag) {
     149                } else if (pThis->iflag) {
    143150                        (void)fprintf(stderr, "overwrite %s? %s",
    144                                         to.p_path, YESNO);
     151                                        pThis->to.p_path, YESNO);
    145152                        checkch = ch = getchar();
    146153                        while (ch != '\n' && ch != EOF)
     
    148155                        if (checkch != 'y' && checkch != 'Y') {
    149156                                (void)close(from_fd);
    150                                 kmk_builtin_ctx_printf(pCtx, 1, "not overwritten\n");
     157                                kmk_builtin_ctx_printf(pThis->pCtx, 1, "not overwritten\n");
    151158                                return (1);
    152159                        }
    153160                }
    154161
    155                 if (fflag) {
     162                if (pThis->fflag) {
    156163                    /* remove existing destination file name,
    157164                     * create a new file  */
    158                     (void)unlink(to.p_path);
    159                     to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
     165                    (void)unlink(pThis->to.p_path);
     166                    to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
    160167                                 fs->st_mode & ~(S_ISUID | S_ISGID));
    161168                } else
    162169                    /* overwrite existing destination file name */
    163                     to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, 0);
     170                    to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_BINARY | KMK_OPEN_NO_INHERIT, 0);
    164171        } else
    165                 to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
     172                to_fd = open(pThis->to.p_path, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY | KMK_OPEN_NO_INHERIT,
    166173                    fs->st_mode & ~(S_ISUID | S_ISGID));
    167174
    168175        if (to_fd == -1) {
    169                 warn(pCtx, "open: %s", to.p_path);
     176                warn(pThis->pCtx, "open: %s", pThis->to.p_path);
    170177                (void)close(from_fd);
    171178                return (1);
     
    185192                if ((p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
    186193                    MAP_SHARED, from_fd, (off_t)0)) == MAP_FAILED) {
    187                         warn(pCtx, "mmap: %s", entp->fts_path);
     194                        warn(pThis->pCtx, "mmap: %s", entp->fts_path);
    188195                        rval = 1;
    189196                } else {
     
    193200                                wcount = write(to_fd, bufp, wresid);
    194201                                wtotal += wcount;
    195                                 if (info) {
    196                                         info = 0;
    197                                         kmk_builtin_ctx_printf(pCtx, 1,
     202# if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
     203                                if (g_cp_info) {
     204                                        g_cp_info = 0;
     205                                        kmk_builtin_ctx_printf(pThis->pCtx, 1,
    198206                                                "%s -> %s %3d%%\n",
    199                                                 entp->fts_path, to.p_path,
     207                                                entp->fts_path, pThis->to.p_path,
    200208                                                cp_pct(wtotal, fs->st_size));
    201209
    202210                                }
     211#endif
    203212                                if (wcount >= (ssize_t)wresid || wcount <= 0)
    204213                                        break;
    205214                        }
    206215                        if (wcount != (ssize_t)wresid) {
    207                                 warn(pCtx, "write[%zd != %zu]: %s", wcount, wresid, to.p_path);
     216                                warn(pThis->pCtx, "write[%zd != %zu]: %s", wcount, wresid, pThis->to.p_path);
    208217                                rval = 1;
    209218                        }
    210219                        /* Some systems don't unmap on close(2). */
    211220                        if (munmap(p, fs->st_size) < 0) {
    212                                 warn(pCtx, "munmap: %s", entp->fts_path);
     221                                warn(pThis->pCtx, "munmap: %s", entp->fts_path);
    213222                                rval = 1;
    214223                        }
     
    223232                                wcount = write(to_fd, bufp, wresid);
    224233                                wtotal += wcount;
    225                                 if (info) {
    226                                         info = 0;
    227                                         kmk_builtin_ctx_printf(pCtx, 1,
     234#if defined(SIGINFO) && defined(KMK_BUILTIN_STANDALONE)
     235                                if (g_cp_info) {
     236                                        g_cp_info = 0;
     237                                        kmk_builtin_ctx_printf(pThis->pCtx, 1,
    228238                                                "%s -> %s %3d%%\n",
    229                                                 entp->fts_path, to.p_path,
     239                                                entp->fts_path, pThis->to.p_path,
    230240                                                cp_pct(wtotal, fs->st_size));
    231241
    232242                                }
     243#endif
    233244                                if (wcount >= (ssize_t)wresid || wcount <= 0)
    234245                                        break;
    235246                        }
    236247                        if (wcount != (ssize_t)wresid) {
    237                                 warn(pCtx, "write[%zd != %zu]: %s", wcount, wresid, to.p_path);
     248                                warn(pThis->pCtx, "write[%zd != %zu]: %s", wcount, wresid, pThis->to.p_path);
    238249                                rval = 1;
    239250                                break;
     
    241252                }
    242253                if (rcount < 0) {
    243                         warn(pCtx, "read: %s", entp->fts_path);
     254                        warn(pThis->pCtx, "read: %s", entp->fts_path);
    244255                        rval = 1;
    245256                }
     
    253264         */
    254265
    255         if (pflag && setfile(pCtx, fs, to_fd))
     266        if (pThis->pflag && copy_file_attribs(pThis, fs, to_fd))
    256267                rval = 1;
    257268        (void)close(from_fd);
    258269        if (close(to_fd)) {
    259                 warn(pCtx, "close: %s", to.p_path);
     270                warn(pThis->pCtx, "close: %s", pThis->to.p_path);
    260271                rval = 1;
    261272        }
     
    264275
    265276int
    266 copy_link(PKMKBUILTINCTX pCtx, const FTSENT *p, int exists)
     277copy_link(CPUTILSINSTANCE *pThis, const FTSENT *p, int exists)
    267278{
    268279        int len;
     
    270281
    271282        if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
    272                 warn(pCtx, "readlink: %s", p->fts_path);
     283                warn(pThis->pCtx, "readlink: %s", p->fts_path);
    273284                return (1);
    274285        }
    275286        llink[len] = '\0';
    276         if (exists && unlink(to.p_path)) {
    277                 warn(pCtx, "unlink: %s", to.p_path);
    278                 return (1);
    279         }
    280         if (symlink(llink, to.p_path)) {
    281                 warn(pCtx, "symlink: %s", llink);
    282                 return (1);
    283         }
    284         return (pflag ? setfile(pCtx, p->fts_statp, -1) : 0);
    285 }
    286 
    287 int
    288 copy_fifo(PKMKBUILTINCTX pCtx, struct stat *from_stat, int exists)
    289 {
    290         if (exists && unlink(to.p_path)) {
    291                 warn(pCtx, "unlink: %s", to.p_path);
    292                 return (1);
    293         }
    294         if (mkfifo(to.p_path, from_stat->st_mode)) {
    295                 warn(pCtx, "mkfifo: %s", to.p_path);
    296                 return (1);
    297         }
    298         return (pflag ? setfile(pCtx, from_stat, -1) : 0);
    299 }
    300 
    301 int
    302 copy_special(PKMKBUILTINCTX pCtx, struct stat *from_stat, int exists)
    303 {
    304         if (exists && unlink(to.p_path)) {
    305                 warn(pCtx, "unlink: %s", to.p_path);
    306                 return (1);
    307         }
    308         if (mknod(to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
    309                 warn(pCtx, "mknod: %s", to.p_path);
    310                 return (1);
    311         }
    312         return (pflag ? setfile(pCtx, from_stat, -1) : 0);
    313 }
    314 
    315 int
    316 setfile(PKMKBUILTINCTX pCtx, struct stat *fs, int fd)
    317 {
    318         static struct timeval tv[2];
     287        if (exists && unlink(pThis->to.p_path)) {
     288                warn(pThis->pCtx, "unlink: %s", pThis->to.p_path);
     289                return (1);
     290        }
     291        if (symlink(llink, pThis->to.p_path)) {
     292                warn(pThis->pCtx, "symlink: %s", llink);
     293                return (1);
     294        }
     295        return (pThis->pflag ? copy_file_attribs(pThis, p->fts_statp, -1) : 0);
     296}
     297
     298int
     299copy_fifo(CPUTILSINSTANCE *pThis, struct stat *from_stat, int exists)
     300{
     301        if (exists && unlink(pThis->to.p_path)) {
     302                warn(pThis->pCtx, "unlink: %s", pThis->to.p_path);
     303                return (1);
     304        }
     305        if (mkfifo(pThis->to.p_path, from_stat->st_mode)) {
     306                warn(pThis->pCtx, "mkfifo: %s", pThis->to.p_path);
     307                return (1);
     308        }
     309        return (pThis->pflag ? copy_file_attribs(pThis, from_stat, -1) : 0);
     310}
     311
     312int
     313copy_special(CPUTILSINSTANCE *pThis, struct stat *from_stat, int exists)
     314{
     315        if (exists && unlink(pThis->to.p_path)) {
     316                warn(pThis->pCtx, "unlink: %s", pThis->to.p_path);
     317                return (1);
     318        }
     319        if (mknod(pThis->to.p_path, from_stat->st_mode, from_stat->st_rdev)) {
     320                warn(pThis->pCtx, "mknod: %s", pThis->to.p_path);
     321                return (1);
     322        }
     323        return (pThis->pflag ? copy_file_attribs(pThis, from_stat, -1) : 0);
     324}
     325
     326int
     327copy_file_attribs(CPUTILSINSTANCE *pThis, struct stat *fs, int fd)
     328{
     329        /*static*/ struct timeval tv[2];
    319330        struct stat ts;
    320331        int rval, gotstat, islink, fdval;
     
    334345        tv[0].tv_usec = tv[1].tv_usec = 0;
    335346#endif
    336         if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
    337                 warn(pCtx, "%sutimes: %s", islink ? "l" : "", to.p_path);
     347        if (islink ? lutimes(pThis->to.p_path, tv) : utimes(pThis->to.p_path, tv)) {
     348                warn(pThis->pCtx, "%sutimes: %s", islink ? "l" : "", pThis->to.p_path);
    338349                rval = 1;
    339350        }
    340351        if (fdval ? fstat(fd, &ts) :
    341             (islink ? lstat(to.p_path, &ts) : stat(to.p_path, &ts)))
     352            (islink ? lstat(pThis->to.p_path, &ts) : stat(pThis->to.p_path, &ts)))
    342353                gotstat = 0;
    343354        else {
     
    354365        if (!gotstat || fs->st_uid != ts.st_uid || fs->st_gid != ts.st_gid)
    355366                if (fdval ? fchown(fd, fs->st_uid, fs->st_gid) :
    356                     (islink ? lchown(to.p_path, fs->st_uid, fs->st_gid) :
    357                     chown(to.p_path, fs->st_uid, fs->st_gid))) {
     367                    (islink ? lchown(pThis->to.p_path, fs->st_uid, fs->st_gid) :
     368                    chown(pThis->to.p_path, fs->st_uid, fs->st_gid))) {
    358369                        if (errno != EPERM) {
    359                                 warn(pCtx, "chown: %s", to.p_path);
     370                                warn(pThis->pCtx, "chown: %s", pThis->to.p_path);
    360371                                rval = 1;
    361372                        }
     
    365376        if (!gotstat || fs->st_mode != ts.st_mode)
    366377                if (fdval ? fchmod(fd, fs->st_mode) :
    367                     (islink ? lchmod(to.p_path, fs->st_mode) :
    368                     chmod(to.p_path, fs->st_mode))) {
    369                         warn(pCtx, "chmod: %s", to.p_path);
     378                    (islink ? lchmod(pThis->to.p_path, fs->st_mode) :
     379                    chmod(pThis->to.p_path, fs->st_mode))) {
     380                        warn(pThis->pCtx, "chmod: %s", pThis->to.p_path);
    370381                        rval = 1;
    371382                }
     
    376387                    fchflags(fd, fs->st_flags) :
    377388                    (islink ? (errno = ENOSYS) :
    378                     chflags(to.p_path, fs->st_flags))) {
    379                         warn(pCtx, "chflags: %s", to.p_path);
     389                    chflags(pThis->to.p_path, fs->st_flags))) {
     390                        warn(pThis->pCtx, "chflags: %s", pThis->to.p_path);
    380391                        rval = 1;
    381392                }
Note: See TracChangeset for help on using the changeset viewer.