VirtualBox

Changeset 1710 in kBuild


Ignore:
Timestamp:
Sep 2, 2008 9:50:04 PM (17 years ago)
Author:
bird
Message:

kmk: Made chmod build on windows. Some cleanup of the bsdisms.

Location:
trunk/src/kmk/kmkbuiltin
Files:
9 edited

Legend:

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

    r1705 r1710  
    5656#else
    5757# include "mscfakes.h"
    58 #endif 
     58#endif
    5959#include "getopt.h"
    6060#include "kmkbuiltin.h"
     61
     62extern void * bsd_setmode(const char *p);
     63extern mode_t bsd_getmode(const void *bbox, mode_t omode);
     64extern void bsd_strmode(mode_t mode, char *p);
    6165
    6266#if defined(__APPLE__) && !defined(_DARWIN_FEATURE_UNIX_CONFORMANCE)
     
    176180
    177181        mode = *argv;
    178         if ((set = setmode(mode)) == NULL)
     182        if ((set = bsd_setmode(mode)) == NULL)
    179183                return errx(1, "invalid file mode: %s", mode);
    180184
     
    210214                        break;
    211215                }
    212                 newmode = getmode(set, p->fts_statp->st_mode);
     216                newmode = bsd_getmode(set, p->fts_statp->st_mode);
    213217                if ((newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
    214218                        continue;
     
    223227                                        char m1[12], m2[12];
    224228
    225                                         strmode(p->fts_statp->st_mode, m1);
    226                                         strmode((p->fts_statp->st_mode &
     229                                        bsd_strmode(p->fts_statp->st_mode, m1);
     230                                        bsd_strmode((p->fts_statp->st_mode &
    227231                                            S_IFMT) | newmode, m2);
    228232
  • trunk/src/kmk/kmkbuiltin/install.c

    r1539 r1710  
    8383
    8484
    85 extern void * setmode(const char *p);
    86 extern mode_t getmode(const void *bbox, mode_t omode);
     85extern void * bsd_setmode(const char *p);
     86extern mode_t bsd_getmode(const void *bbox, mode_t omode);
    8787
    8888#ifndef __unused
     
    209209                        break;
    210210                case 'm':
    211 #ifdef __EMX__
    212211                        if (!(set = bsd_setmode(optarg)))
    213 #else
    214                         if (!(set = setmode(optarg)))
    215 #endif
    216212                                return errx(EX_USAGE, "invalid file mode: %s",
    217213                                            optarg);
    218                         mode = getmode(set, 0);
     214                        mode = bsd_getmode(set, 0);
    219215                        free(set);
    220216                        break;
  • trunk/src/kmk/kmkbuiltin/mkdir.c

    r1595 r1710  
    7272
    7373
    74 extern void * setmode(const char *p);
    75 extern mode_t getmode(const void *bbox, mode_t omode);
     74extern void * bsd_setmode(const char *p);
     75extern mode_t bsd_getmode(const void *bbox, mode_t omode);
    7676
    7777static int      build(char *, mode_t);
     
    127127                omode = S_IRWXU | S_IRWXG | S_IRWXO;
    128128        } else {
    129                 if ((set = setmode(mode)) == NULL)
     129                if ((set = bsd_setmode(mode)) == NULL)
    130130                        return errx(1, "invalid file mode: %s", mode);
    131                 omode = getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
     131                omode = bsd_getmode(set, S_IRWXU | S_IRWXG | S_IRWXO);
    132132                free(set);
    133133        }
  • trunk/src/kmk/kmkbuiltin/mscfakes.c

    r1321 r1710  
    4040
    4141
     42/**
     43 * Makes corrections to a directory path that ends with a trailing slash.
     44 *
     45 * @returns temporary buffer to free.
     46 * @param   ppszPath    The path pointer. This is updated when necessary.
     47 * @param   pfMustBeDir This is set if it must be a directory, otherwise it's cleared.
     48 */
     49static char *
     50msc_fix_path(const char **ppszPath, int *pfMustBeDir)
     51{
     52    const char *pszPath = *ppszPath;
     53    const char *psz;
     54    char *pszNew;
     55    *pfMustBeDir = 0;
     56
     57    /*
     58     * Skip any compusory trailing slashes
     59     */
     60    if (pszPath[0] == '/' || pszPath[0] == '\\')
     61    {
     62        if (   (pszPath[1] == '/' || pszPath[1] == '\\')
     63            &&  pszPath[2] != '/'
     64            &&  pszPath[2] != '\\')
     65            /* unc */
     66            pszPath += 2;
     67        else
     68            /* root slash(es) */
     69            pszPath++;
     70    }
     71    else if (   isalpha(pszPath[0])
     72             && pszPath[1] == ':')
     73    {
     74        if (pszPath[2] == '/' || pszPath[2] == '\\')
     75            /* drive w/ slash */
     76            pszPath += 3;
     77        else
     78            /* drive relative path. */
     79            pszPath += 2;
     80    }
     81    /* else: relative path, no skipping necessary. */
     82
     83    /*
     84     * Any trailing slashes to drop off?
     85     */
     86    psz = strchr(pszPath, '\0');
     87    if (pszPath <= psz)
     88        return NULL;
     89    if (   psz[-1] != '/'
     90        || psz[-1] != '\\')
     91        return NULL;
     92
     93    /* figure how many, make a copy and strip them off. */
     94    while (     psz > pszPath
     95           &&   (   psz[-1] == '/'
     96                 || psz[-1] == '\\'))
     97        psz--;
     98    pszNew = strdup(pszPath);
     99    pszNew[psz - pszPath] = '\0';
     100
     101    *pfMustBeDir = 1;
     102    *ppszPath = pszNew; /* use this one */
     103    return pszNew;
     104}
     105
     106
     107static int
     108msc_set_errno(DWORD dwErr)
     109{
     110    switch (dwErr)
     111    {
     112        default:
     113        case ERROR_INVALID_FUNCTION:        errno = EINVAL; break;
     114        case ERROR_FILE_NOT_FOUND:          errno = ENOENT; break;
     115        case ERROR_PATH_NOT_FOUND:          errno = ENOENT; break;
     116        case ERROR_TOO_MANY_OPEN_FILES:     errno = EMFILE; break;
     117        case ERROR_ACCESS_DENIED:           errno = EACCES; break;
     118        case ERROR_INVALID_HANDLE:          errno = EBADF; break;
     119        case ERROR_ARENA_TRASHED:           errno = ENOMEM; break;
     120        case ERROR_NOT_ENOUGH_MEMORY:       errno = ENOMEM; break;
     121        case ERROR_INVALID_BLOCK:           errno = ENOMEM; break;
     122        case ERROR_BAD_ENVIRONMENT:         errno = E2BIG; break;
     123        case ERROR_BAD_FORMAT:              errno = ENOEXEC; break;
     124        case ERROR_INVALID_ACCESS:          errno = EINVAL; break;
     125        case ERROR_INVALID_DATA:            errno = EINVAL; break;
     126        case ERROR_INVALID_DRIVE:           errno = ENOENT; break;
     127        case ERROR_CURRENT_DIRECTORY:       errno = EACCES; break;
     128        case ERROR_NOT_SAME_DEVICE:         errno = EXDEV; break;
     129        case ERROR_NO_MORE_FILES:           errno = ENOENT; break;
     130        case ERROR_LOCK_VIOLATION:          errno = EACCES; break;
     131        case ERROR_BAD_NETPATH:             errno = ENOENT; break;
     132        case ERROR_NETWORK_ACCESS_DENIED:   errno = EACCES; break;
     133        case ERROR_BAD_NET_NAME:            errno = ENOENT; break;
     134        case ERROR_FILE_EXISTS:             errno = EEXIST; break;
     135        case ERROR_CANNOT_MAKE:             errno = EACCES; break;
     136        case ERROR_FAIL_I24:                errno = EACCES; break;
     137        case ERROR_INVALID_PARAMETER:       errno = EINVAL; break;
     138        case ERROR_NO_PROC_SLOTS:           errno = EAGAIN; break;
     139        case ERROR_DRIVE_LOCKED:            errno = EACCES; break;
     140        case ERROR_BROKEN_PIPE:             errno = EPIPE; break;
     141        case ERROR_DISK_FULL:               errno = ENOSPC; break;
     142        case ERROR_INVALID_TARGET_HANDLE:   errno = EBADF; break;
     143        case ERROR_WAIT_NO_CHILDREN:        errno = ECHILD; break;
     144        case ERROR_CHILD_NOT_COMPLETE:      errno = ECHILD; break;
     145        case ERROR_DIRECT_ACCESS_HANDLE:    errno = EBADF; break;
     146        case ERROR_NEGATIVE_SEEK:           errno = EINVAL; break;
     147        case ERROR_SEEK_ON_DEVICE:          errno = EACCES; break;
     148        case ERROR_DIR_NOT_EMPTY:           errno = ENOTEMPTY; break;
     149        case ERROR_NOT_LOCKED:              errno = EACCES; break;
     150        case ERROR_BAD_PATHNAME:            errno = ENOENT; break;
     151        case ERROR_MAX_THRDS_REACHED:       errno = EAGAIN; break;
     152        case ERROR_LOCK_FAILED:             errno = EACCES; break;
     153        case ERROR_ALREADY_EXISTS:          errno = EEXIST; break;
     154        case ERROR_FILENAME_EXCED_RANGE:    errno = ENOENT; break;
     155        case ERROR_NESTING_NOT_ALLOWED:     errno = EAGAIN; break;
     156    }
     157
     158    return -1;
     159}
     160
    42161char *dirname(char *path)
    43162{
    44163    /** @todo later */
    45164    return path;
     165}
     166
     167
     168int lchmod(const char *pszPath, mode_t mode)
     169{
     170    int rc = 0;
     171    int fMustBeDir;
     172    char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
     173
     174    /*
     175     * Get the current attributes
     176     */
     177    DWORD fAttr = GetFileAttributes(pszPath);
     178    if (fAttr == INVALID_FILE_ATTRIBUTES)
     179        rc = msc_set_errno(GetLastError());
     180    else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
     181    {
     182        errno = ENOTDIR;
     183        rc = -1;
     184    }
     185    else
     186    {
     187        /*
     188         * Modify the attributes and try set them.
     189         */
     190        if (mode & _S_IWRITE)
     191            fAttr &= ~FILE_ATTRIBUTE_READONLY;
     192        else
     193            fAttr |= FILE_ATTRIBUTE_READONLY;
     194        if (!SetFileAttributes(pszPath, fAttr))
     195            rc = msc_set_errno(GetLastError());
     196    }
     197
     198    if (pszPathFree)
     199    {
     200        int saved_errno = errno;
     201        free(pszPathFree);
     202        errno = saved_errno;
     203    }
     204    return rc;
     205}
     206
     207
     208int msc_chmod(const char *pszPath, mode_t mode)
     209{
     210    int rc = 0;
     211    int saved_errno;
     212    int fMustBeDir;
     213    char *pszPathFree = msc_fix_path(&pszPath, &fMustBeDir);
     214
     215    /*
     216     * Get the current attributes.
     217     */
     218    DWORD fAttr = GetFileAttributes(pszPath);
     219    if (fAttr == INVALID_FILE_ATTRIBUTES)
     220        rc = msc_set_errno(GetLastError());
     221    else if (fMustBeDir & !(fAttr & FILE_ATTRIBUTE_DIRECTORY))
     222    {
     223        errno = ENOTDIR;
     224        rc = -1;
     225    }
     226    else if (fAttr & FILE_ATTRIBUTE_REPARSE_POINT)
     227    {
     228        errno = ENOSYS; /** @todo resolve symbolic link / rewrite to NtSetInformationFile. */
     229        rc = -1;
     230    }
     231    else
     232    {
     233        /*
     234         * Modify the attributes and try set them.
     235         */
     236        if (mode & _S_IWRITE)
     237            fAttr &= ~FILE_ATTRIBUTE_READONLY;
     238        else
     239            fAttr |= FILE_ATTRIBUTE_READONLY;
     240        if (!SetFileAttributes(pszPath, fAttr))
     241            rc = msc_set_errno(GetLastError());
     242    }
     243
     244    if (pszPathFree)
     245    {
     246        int saved_errno = errno;
     247        free(pszPathFree);
     248        errno = saved_errno;
     249    }
     250    return rc;
    46251}
    47252
  • trunk/src/kmk/kmkbuiltin/mscfakes.h

    r1321 r1710  
    2727#ifdef _MSC_VER
    2828
    29 #define setmode setmode_msc
    3029#include <io.h>
    3130#include <direct.h>
     
    3332#include <stdarg.h>
    3433#include <malloc.h>
    35 #undef setmode
    3634#include "getopt.h"
    3735
     
    125123#define getegid()  0
    126124#define lstat(path, s) stat(path, s)
    127 #define lchmod(path, mod) chmod(path, mod)
     125int lchmod(const char *path, mode_t mode);
     126int msc_chmod(const char *path, mode_t mode);
     127#define chmod msc_chmod
    128128#define lchown(path, uid, gid) chown(path, uid, gid)
    129129#define lutimes(path, tvs) utimes(path, tvs)
  • trunk/src/kmk/kmkbuiltin/mv.c

    r1604 r1710  
    9797static int      usage(FILE *);
    9898
    99 #if !defined(__FreeBSD__) && !defined(__APPLE__)
    100 extern void strmode(mode_t mode, char *p);
    101 #endif
     99extern void bsd_strmode(mode_t mode, char *p);
    102100
    103101#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__DragonFly__)
     
    258256                        ask = 1;
    259257                } else if (access(to, W_OK) && !stat(to, &sb)) {
    260                         strmode(sb.st_mode, modep);
     258                        bsd_strmode(sb.st_mode, modep);
    261259                        (void)fprintf(stderr, "override %s%s%s/%s for %s? %s",
    262260                            modep + 1, modep[9] == ' ' ? "" : " ",
  • trunk/src/kmk/kmkbuiltin/rm.c

    r1629 r1710  
    9191#endif
    9292
    93 #if !defined(__FreeBSD__) && !defined(__APPLE__)
    94 extern void strmode(mode_t mode, char *p);
    95 #endif
     93extern void bsd_strmode(mode_t mode, char *p);
    9694
    9795static int dflag, eval, fflag, iflag, Pflag, vflag, Wflag, stdin_ok;
     
    610608                    )
    611609                        return (1);
    612                 strmode(sp->st_mode, modep);
     610                bsd_strmode(sp->st_mode, modep);
    613611#ifdef SF_APPEND
    614612                if ((flagsp = fflagstostr(sp->st_flags)) == NULL)
  • trunk/src/kmk/kmkbuiltin/setmode.c

    r616 r1710  
    5555#else
    5656#include "mscfakes.h"
    57 #endif 
     57#endif
    5858
    5959#ifdef SETMODE_DEBUG
     
    8989#ifndef _DIAGASSERT
    9090# define _DIAGASSERT assert
    91 #endif 
     91#endif
    9292
    9393#ifndef S_ISTXT
     
    106106 */
    107107mode_t
    108 getmode(bbox, omode)
     108bsd_getmode(bbox, omode)
    109109        const void *bbox;
    110110        mode_t omode;
     
    196196
    197197void *
    198 setmode(p)
     198bsd_setmode(p)
    199199        const char *p;
    200200{
     
    221221        sigfillset(&signset);
    222222        (void)sigprocmask(SIG_BLOCK, &signset, &sigoset);
    223 #endif 
     223#endif
    224224        (void)umask(mask = umask(0));
    225225        mask = ~mask;
     
    229229
    230230        setlen = SET_LEN + 2;
    231        
     231
    232232        if ((set = malloc((u_int)(sizeof(BITCMD) * setlen))) == NULL)
    233233                return (NULL);
     
    290290                        case 's':
    291291                                /*
    292                                  * If specific bits where requested and 
    293                                  * only "other" bits ignore set-id. 
     292                                 * If specific bits where requested and
     293                                 * only "other" bits ignore set-id.
    294294                                 */
    295295                                if (who == 0 || (who & ~S_IRWXO))
     
    298298                        case 't':
    299299                                /*
    300                                  * If specific bits where requested and 
    301                                  * only "other" bits ignore set-id. 
     300                                 * If specific bits where requested and
     301                                 * only "other" bits ignore set-id.
    302302                                 */
    303303                                if (who == 0 || (who & ~S_IRWXO)) {
     
    412412                        set->bits = mask;
    413413                }
    414        
     414
    415415                if (oparg == '+')
    416416                        set->cmd2 |= CMD2_SET;
     
    446446 * Given an array of bitcmd structures, compress by compacting consecutive
    447447 * '+', '-' and 'X' commands into at most 3 commands, one of each.  The 'u',
    448  * 'g' and 'o' commands continue to be separate.  They could probably be 
     448 * 'g' and 'o' commands continue to be separate.  They could probably be
    449449 * compacted, but it's not worth the effort.
    450450 */
  • trunk/src/kmk/kmkbuiltin/strmode.c

    r618 r1710  
    4848#else
    4949#include "mscfakes.h"
    50 #endif 
     50#endif
    5151
    5252#ifndef _DIAGASSERT
    5353#define _DIAGASSERT assert
    54 #endif 
     54#endif
    5555
    5656void
    57 strmode(mode, p)
     57bsd_strmode(mode, p)
    5858        mode_t mode;
    5959        char *p;
     
    7474                *p++ = 'b';
    7575                break;
    76 #endif 
     76#endif
    7777        case S_IFREG:                   /* regular */
    7878#ifdef S_ARCH2
     
    176176#else
    177177        switch (mode & (S_IXOTH)) {
    178 #endif 
     178#endif
    179179        case 0:
    180180                *p++ = '-';
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette