Ignore:
Timestamp:
Mar 14, 2018, 10:28:10 PM (7 years ago)
Author:
bird
Message:

kmk: Merged in changes from GNU make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6 / https://git.savannah.gnu.org/git/make.git).

Location:
trunk/src/kmk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk

  • trunk/src/kmk/w32/compat/dirent.c

    r2591 r3140  
    11/* Directory entry code for Window platforms.
    2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
     2Copyright (C) 1996-2016 Free Software Foundation, Inc.
    43This file is part of GNU Make.
    54
     
    3231opendir(const char* pDirName)
    3332{
    34         struct stat sb;
    35         DIR*    pDir;
    36         char*   pEndDirName;
    37         int     nBufferLen;
    38 
    39         /* sanity checks */
    40         if (!pDirName) {
    41                 errno = EINVAL;
    42                 return NULL;
    43         }
    44         if (stat(pDirName, &sb) != 0) {
    45                 errno = ENOENT;
    46                 return NULL;
    47         }
    48         if ((sb.st_mode & S_IFMT) != S_IFDIR) {
    49                 errno = ENOTDIR;
    50                 return NULL;
    51         }
    52 
    53         /* allocate a DIR structure to return */
    54         pDir = (DIR *) malloc(sizeof (DIR));
    55 
    56         if (!pDir)
    57                 return NULL;
    58 
    59         /* input directory name length */
    60         nBufferLen = strlen(pDirName);
    61 
    62         /* copy input directory name to DIR buffer */
    63         strcpy(pDir->dir_pDirectoryName, pDirName);
    64 
    65         /* point to end of the copied directory name */
    66         pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1];
    67 
    68         /* if directory name did not end in '/' or '\', add '/' */
    69         if ((*pEndDirName != '/') && (*pEndDirName != '\\')) {
    70                 pEndDirName++;
    71                 *pEndDirName = '/';
    72         }
    73 
    74         /* now append the wildcard character to the buffer */
    75         pEndDirName++;
    76         *pEndDirName = '*';
    77         pEndDirName++;
    78         *pEndDirName = '\0';
    79 
    80         /* other values defaulted */
    81         pDir->dir_nNumFiles = 0;
    82         pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
    83         pDir->dir_ulCookie = __DIRENT_COOKIE;
     33        struct stat sb;
     34        DIR*    pDir;
     35        char*   pEndDirName;
     36        int     nBufferLen;
     37
     38        /* sanity checks */
     39        if (!pDirName) {
     40                errno = EINVAL;
     41                return NULL;
     42        }
     43        if (stat(pDirName, &sb) != 0) {
     44                errno = ENOENT;
     45                return NULL;
     46        }
     47        if ((sb.st_mode & S_IFMT) != S_IFDIR) {
     48                errno = ENOTDIR;
     49                return NULL;
     50        }
     51
     52        /* allocate a DIR structure to return */
     53        pDir = (DIR *) malloc(sizeof (DIR));
     54
     55        if (!pDir)
     56                return NULL;
     57
     58        /* input directory name length */
     59        nBufferLen = strlen(pDirName);
     60
     61        /* copy input directory name to DIR buffer */
     62        strcpy(pDir->dir_pDirectoryName, pDirName);
     63
     64        /* point to end of the copied directory name */
     65        pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1];
     66
     67        /* if directory name did not end in '/' or '\', add '/' */
     68        if ((*pEndDirName != '/') && (*pEndDirName != '\\')) {
     69                pEndDirName++;
     70                *pEndDirName = '/';
     71        }
     72
     73        /* now append the wildcard character to the buffer */
     74        pEndDirName++;
     75        *pEndDirName = '*';
     76        pEndDirName++;
     77        *pEndDirName = '\0';
     78
     79        /* other values defaulted */
     80        pDir->dir_nNumFiles = 0;
     81        pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
     82        pDir->dir_ulCookie = __DIRENT_COOKIE;
    8483
    8584#ifdef KMK_PRF
    86         fprintf(stderr, "opendir(%s) -> %p\n", pDirName, pDir);
     85        fprintf(stderr, "opendir(%s) -> %p\n", pDirName, pDir);
    8786#endif
    88         return pDir;
     87        return pDir;
    8988}
    9089
     
    9291closedir(DIR *pDir)
    9392{
    94         /* got a valid pointer? */
    95         if (!pDir) {
    96                 errno = EINVAL;
    97                 return;
    98         }
    99 
    100         /* sanity check that this is a DIR pointer */
    101         if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
    102                 errno = EINVAL;
    103                 return;
    104         }
    105 
    106         /* close the WINDOWS32 directory handle */
    107         if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
    108                 FindClose(pDir->dir_hDirHandle);
    109 
    110         free(pDir);
    111 
    112         return;
     93        /* got a valid pointer? */
     94        if (!pDir) {
     95                errno = EINVAL;
     96                return;
     97        }
     98
     99        /* sanity check that this is a DIR pointer */
     100        if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
     101                errno = EINVAL;
     102                return;
     103        }
     104
     105        /* close the WINDOWS32 directory handle */
     106        if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
     107                FindClose(pDir->dir_hDirHandle);
     108
     109        free(pDir);
     110
     111        return;
    113112}
    114113
     
    116115readdir(DIR* pDir)
    117116{
    118         WIN32_FIND_DATA wfdFindData;
    119 
    120         if (!pDir) {
    121                 errno = EINVAL;
    122                 return NULL;
    123         }
    124 
    125         /* sanity check that this is a DIR pointer */
    126         if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
    127                 errno = EINVAL;
    128                 return NULL;
    129         }
    130 
    131         if (pDir->dir_nNumFiles == 0) {
    132                 pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData);
    133                 if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE)
    134                         return NULL;
    135         } else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData))
    136                         return NULL;
    137 
    138         /* bump count for next call to readdir() or telldir() */
    139         pDir->dir_nNumFiles++;
    140 
    141         /* fill in struct dirent values */
    142         pDir->dir_sdReturn.d_ino = (ino_t)-1;
    143         strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName);
    144 
    145         return &pDir->dir_sdReturn;
     117        WIN32_FIND_DATA wfdFindData;
     118
     119        if (!pDir) {
     120                errno = EINVAL;
     121                return NULL;
     122        }
     123
     124        /* sanity check that this is a DIR pointer */
     125        if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
     126                errno = EINVAL;
     127                return NULL;
     128        }
     129
     130        if (pDir->dir_nNumFiles == 0) {
     131                pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData);
     132                if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE)
     133                        return NULL;
     134        } else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData))
     135                        return NULL;
     136
     137        /* bump count for next call to readdir() or telldir() */
     138        pDir->dir_nNumFiles++;
     139
     140        /* fill in struct dirent values */
     141        pDir->dir_sdReturn.d_ino = (ino_t)-1;
     142        strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName);
     143
     144        return &pDir->dir_sdReturn;
    146145}
    147146
     
    149148rewinddir(DIR* pDir)
    150149{
    151         if (!pDir) {
    152                 errno = EINVAL;
    153                 return;
    154         }
    155 
    156         /* sanity check that this is a DIR pointer */
    157         if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
    158                 errno = EINVAL;
    159                 return;
    160         }
    161 
    162         /* close the WINDOWS32 directory handle */
    163         if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
    164                 if (!FindClose(pDir->dir_hDirHandle))
    165                         errno = EBADF;
    166 
    167         /* reset members which control readdir() */
    168         pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
    169         pDir->dir_nNumFiles = 0;
    170 
    171         return;
     150        if (!pDir) {
     151                errno = EINVAL;
     152                return;
     153        }
     154
     155        /* sanity check that this is a DIR pointer */
     156        if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
     157                errno = EINVAL;
     158                return;
     159        }
     160
     161        /* close the WINDOWS32 directory handle */
     162        if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
     163                if (!FindClose(pDir->dir_hDirHandle))
     164                        errno = EBADF;
     165
     166        /* reset members which control readdir() */
     167        pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
     168        pDir->dir_nNumFiles = 0;
     169
     170        return;
    172171}
    173172
     
    175174telldir(DIR* pDir)
    176175{
    177         if (!pDir) {
    178                 errno = EINVAL;
    179                 return -1;
    180         }
    181 
    182         /* sanity check that this is a DIR pointer */
    183         if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
    184                 errno = EINVAL;
    185                 return -1;
    186         }
    187 
    188         /* return number of times readdir() called */
    189         return pDir->dir_nNumFiles;
     176        if (!pDir) {
     177                errno = EINVAL;
     178                return -1;
     179        }
     180
     181        /* sanity check that this is a DIR pointer */
     182        if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
     183                errno = EINVAL;
     184                return -1;
     185        }
     186
     187        /* return number of times readdir() called */
     188        return pDir->dir_nNumFiles;
    190189}
    191190
     
    193192seekdir(DIR* pDir, long nPosition)
    194193{
    195         if (!pDir)
    196                 return;
    197 
    198         /* sanity check that this is a DIR pointer */
    199         if (pDir->dir_ulCookie != __DIRENT_COOKIE)
    200                 return;
    201 
    202         /* go back to beginning of directory */
    203         rewinddir(pDir);
    204 
    205         /* loop until we have found position we care about */
    206         for (--nPosition; nPosition && readdir(pDir); nPosition--);
    207 
    208         /* flag invalid nPosition value */
    209         if (nPosition)
    210                 errno = EINVAL;
    211 
    212         return;
    213 }
     194        if (!pDir)
     195                return;
     196
     197        /* sanity check that this is a DIR pointer */
     198        if (pDir->dir_ulCookie != __DIRENT_COOKIE)
     199                return;
     200
     201        /* go back to beginning of directory */
     202        rewinddir(pDir);
     203
     204        /* loop until we have found position we care about */
     205        for (--nPosition; nPosition && readdir(pDir); nPosition--);
     206
     207        /* flag invalid nPosition value */
     208        if (nPosition)
     209                errno = EINVAL;
     210
     211        return;
     212}
Note: See TracChangeset for help on using the changeset viewer.