mirror of
https://gitlab.com/sortix/sortix.git
synced 2023-02-13 20:55:38 -05:00
Update libc/dirent/fddir-sortix to current coding conventions.
This commit is contained in:
parent
290ee1a6a6
commit
e6a23c5365
1 changed files with 21 additions and 18 deletions
|
@ -17,21 +17,22 @@
|
||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
dirent/fddir-sortix.c
|
dirent/fddir-sortix.cpp
|
||||||
Handles the file descriptor backend for the DIR* API on Sortix.
|
Handles the file descriptor backend for the DIR* API on Sortix.
|
||||||
|
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
|
|
||||||
|
#include <sys/readdirents.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <unistd.h>
|
#include <dirent.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <unistd.h>
|
||||||
#include <sys/readdirents.h>
|
|
||||||
#include <dirent.h>
|
|
||||||
|
|
||||||
typedef struct fddir_sortix_struct
|
typedef struct fddir_sortix_struct
|
||||||
{
|
{
|
||||||
|
@ -41,13 +42,13 @@ typedef struct fddir_sortix_struct
|
||||||
int fd;
|
int fd;
|
||||||
} fddir_sortix_t;
|
} fddir_sortix_t;
|
||||||
|
|
||||||
int fddir_sortix_readents(fddir_sortix_t* info)
|
static int fddir_sortix_readents(fddir_sortix_t* info)
|
||||||
{
|
{
|
||||||
if ( !info->dirent )
|
if ( !info->dirent )
|
||||||
{
|
{
|
||||||
// Allocate a buffer of at least sizeof(kernel_dirent).
|
// Allocate a buffer of at least sizeof(kernel_dirent).
|
||||||
info->direntsize = sizeof(struct kernel_dirent) + 4UL;
|
info->direntsize = sizeof(struct kernel_dirent) + 4UL;
|
||||||
info->dirent = malloc(info->direntsize);
|
info->dirent = (struct kernel_dirent*) malloc(info->direntsize);
|
||||||
if ( !info->dirent )
|
if ( !info->dirent )
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
@ -59,7 +60,7 @@ int fddir_sortix_readents(fddir_sortix_t* info)
|
||||||
size_t newdirentsize = sizeof(struct kernel_dirent) + info->dirent->d_namelen + 1;
|
size_t newdirentsize = sizeof(struct kernel_dirent) + info->dirent->d_namelen + 1;
|
||||||
if ( newdirentsize < info->direntsize )
|
if ( newdirentsize < info->direntsize )
|
||||||
newdirentsize *= 2;
|
newdirentsize *= 2;
|
||||||
struct kernel_dirent* newdirent = malloc(newdirentsize);
|
struct kernel_dirent* newdirent = (struct kernel_dirent*) malloc(newdirentsize);
|
||||||
if ( !newdirent )
|
if ( !newdirent )
|
||||||
return -1;
|
return -1;
|
||||||
free(info->dirent);
|
free(info->dirent);
|
||||||
|
@ -71,7 +72,7 @@ int fddir_sortix_readents(fddir_sortix_t* info)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fddir_sortix_read(void* user, struct dirent* dirent, size_t* size)
|
static int fddir_sortix_read(void* user, struct dirent* dirent, size_t* size)
|
||||||
{
|
{
|
||||||
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
||||||
if ( !info->current )
|
if ( !info->current )
|
||||||
|
@ -84,7 +85,8 @@ int fddir_sortix_read(void* user, struct dirent* dirent, size_t* size)
|
||||||
size_t provided = (user) ? *size : 0;
|
size_t provided = (user) ? *size : 0;
|
||||||
size_t needed = sizeof(struct dirent) + info->current->d_namelen + 1;
|
size_t needed = sizeof(struct dirent) + info->current->d_namelen + 1;
|
||||||
*size = needed;
|
*size = needed;
|
||||||
if ( provided < needed ) { return 1; }
|
if ( provided < needed )
|
||||||
|
return 1;
|
||||||
|
|
||||||
dirent->d_ino = info->current->d_ino;
|
dirent->d_ino = info->current->d_ino;
|
||||||
dirent->d_reclen = needed;
|
dirent->d_reclen = needed;
|
||||||
|
@ -95,19 +97,19 @@ int fddir_sortix_read(void* user, struct dirent* dirent, size_t* size)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fddir_sortix_rewind(void* user)
|
static int fddir_sortix_rewind(void* user)
|
||||||
{
|
{
|
||||||
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
||||||
return lseek(info->fd, 0, SEEK_SET);
|
return lseek(info->fd, 0, SEEK_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
int fddir_sortix_fd(void* user)
|
static int fddir_sortix_fd(void* user)
|
||||||
{
|
{
|
||||||
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
||||||
return info->fd;
|
return info->fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int fddir_sortix_close(void* user)
|
static int fddir_sortix_close(void* user)
|
||||||
{
|
{
|
||||||
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
fddir_sortix_t* info = (fddir_sortix_t*) user;
|
||||||
int result = close(info->fd);
|
int result = close(info->fd);
|
||||||
|
@ -116,10 +118,11 @@ int fddir_sortix_close(void* user)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
DIR* fdopendir(int fd)
|
extern "C" DIR* fdopendir(int fd)
|
||||||
{
|
{
|
||||||
fddir_sortix_t* info = calloc(sizeof(fddir_sortix_t), 1);
|
fddir_sortix_t* info = (fddir_sortix_t*) calloc(sizeof(fddir_sortix_t), 1);
|
||||||
if ( !info ) { return NULL; }
|
if ( !info )
|
||||||
|
return NULL;
|
||||||
|
|
||||||
DIR* dir = dnewdir();
|
DIR* dir = dnewdir();
|
||||||
if ( !dir ) { free(info); return NULL; }
|
if ( !dir ) { free(info); return NULL; }
|
||||||
|
@ -139,7 +142,7 @@ DIR* fdopendir(int fd)
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
DIR* opendir(const char* path)
|
extern "C" DIR* opendir(const char* path)
|
||||||
{
|
{
|
||||||
int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
int fd = open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||||
if ( fd < 0 ) { return NULL; }
|
if ( fd < 0 ) { return NULL; }
|
Loading…
Add table
Reference in a new issue