Split libc/unistd/ioleast.cpp into multiple files.

This commit is contained in:
Jonas 'Sortie' Termansen 2013-09-25 23:40:27 +02:00
parent defa74d7ec
commit a212ec0ee3
15 changed files with 441 additions and 80 deletions

View File

@ -237,6 +237,14 @@ getopt/getopt_long.o \
getopt/getopt.o \
grp/grent.o \
init/init.o \
ioleast/preadall.o \
ioleast/preadleast.o \
ioleast/pwriteall.o \
ioleast/pwriteleast.o \
ioleast/readall.o \
ioleast/readleast.o \
ioleast/writeall.o \
ioleast/writeleast.o \
libgen/basename.o \
libgen/dirname.o \
locale/localeconv.o \
@ -430,7 +438,6 @@ unistd/getpgid.o \
unistd/getpid.o \
unistd/getppid.o \
unistd/getuid.o \
unistd/ioleast.o \
unistd/isatty.o \
unistd/lchown.o \
unistd/linkat.o \

51
libc/include/ioleast.h Normal file
View File

@ -0,0 +1,51 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast.h
Versions of {,p}{read,write} that don't return until it has returned as much
data as requested, end of file, or an error occurs. This is sometimes needed
as read(2) and write(2) is not always guaranteed to fill up the entire
buffer or write it all.
*******************************************************************************/
#ifndef INCLUDE_IOLEAST_H
#define INCLUDE_IOLEAST_H
#include <features.h>
#include <sys/__/types.h>
__BEGIN_DECLS
@include(off_t.h)
@include(size_t.h)
size_t preadall(int fd, void* buf, size_t count, off_t off);
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off);
size_t pwriteall(int fd, const void* buf, size_t count, off_t off);
size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off);
size_t readall(int fd, void* buf, size_t count);
size_t readleast(int fd, void* buf, size_t least, size_t max);
size_t writeall(int fd, const void* buf, size_t count);
size_t writeleast(int fd, const void* buf, size_t least, size_t max);
__END_DECLS
#endif

View File

@ -41,6 +41,7 @@ __BEGIN_DECLS
@include(time_t.h)
__END_DECLS
#include <sortix/timespec.h>
#include <ioleast.h>
#endif
#include <sortix/seek.h>
#include <sortix/unistd.h>
@ -366,12 +367,6 @@ int getdtablesize(void);
size_t getpagesize(void);
int memstat(size_t* memused, size_t* memtotal);
int mkpartition(int fd, off_t start, off_t length);
size_t preadall(int fd, void* buf, size_t count, off_t off);
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off);
size_t pwriteall(int fd, const void* buf, size_t count, off_t off);
size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off);
size_t readall(int fd, void* buf, size_t count);
size_t readleast(int fd, void* buf, size_t least, size_t max);
pid_t sfork(int flags);
pid_t tfork(int flags, tforkregs_t* regs);
size_t writeall(int fd, const void* buf, size_t count);

30
libc/ioleast/preadall.cpp Normal file
View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/preadall.cpp
Reads as much data as requested from the given offset.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t preadall(int fd, void* buf, size_t count, off_t off)
{
return preadleast(fd, buf, count, count, off);
}

View File

@ -0,0 +1,47 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/preadleast.cpp
Reads at least as much data as requested or more from the given offset.
*******************************************************************************/
#include <errno.h>
#include <ioleast.h>
#include <stdint.h>
#include <unistd.h>
extern "C"
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off)
{
ssize_t amount = pread(fd, buf, max, off);
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
void* nextbuf = (uint8_t*) buf + amount;
size_t nextleast = least - amount;
size_t nextmax = max - amount;
off_t nextoff = off + amount;
amount += preadleast(fd, nextbuf, nextleast, nextmax, nextoff);
}
return amount;
}

View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/pwriteall.cpp
Writes as much data as requested at the given offset.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t pwriteall(int fd, const void* buf, size_t count, off_t off)
{
return pwriteleast(fd, buf, count, count, off);
}

View File

@ -0,0 +1,47 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/pwriteleast.cpp
Writes at least as much data as requested or more at the given offset.
*******************************************************************************/
#include <errno.h>
#include <ioleast.h>
#include <stdint.h>
#include <unistd.h>
extern "C"
size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off)
{
ssize_t amount = pwrite(fd, buf, max, off);
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
const void* nextbuf = (const uint8_t*) buf + amount;
size_t nextleast = least - amount;
size_t nextmax = max - amount;
off_t nextoff = off + amount;
amount += pwriteleast(fd, nextbuf, nextleast, nextmax, nextoff);
}
return amount;
}

30
libc/ioleast/readall.cpp Normal file
View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/readall.cpp
Reads as much data as requested.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t readall(int fd, void* buf, size_t count)
{
return readleast(fd, buf, count, count);
}

View File

@ -0,0 +1,45 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/readleast.cpp
Reads at least as much data as requested or more.
*******************************************************************************/
#include <errno.h>
#include <ioleast.h>
#include <stdint.h>
#include <unistd.h>
extern "C" size_t readleast(int fd, void* buf, size_t least, size_t max)
{
ssize_t amount = read(fd, buf, max);
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
void* nextbuf = (uint8_t*) buf + amount;
size_t nextleast = least - amount;
size_t nextmax = max - amount;
amount += readleast(fd, nextbuf, nextleast, nextmax);
}
return amount;
}

30
libc/ioleast/writeall.cpp Normal file
View File

@ -0,0 +1,30 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/writeall.cpp
Writes as much data as requested.
*******************************************************************************/
#include <ioleast.h>
extern "C" size_t writeall(int fd, const void* buf, size_t count)
{
return writeleast(fd, buf, count, count);
}

View File

@ -0,0 +1,45 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
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/>.
ioleast/writeleast.cpp
Writes at least as much data as requested or more.
*******************************************************************************/
#include <errno.h>
#include <ioleast.h>
#include <stdint.h>
#include <unistd.h>
extern "C" size_t writeleast(int fd, const void* buf, size_t least, size_t max)
{
ssize_t amount = write(fd, buf, max);
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
const void* nextbuf = (const uint8_t*) buf + amount;
size_t nextleast = least - amount;
size_t nextmax = max - amount;
amount += writeleast(fd, nextbuf, nextleast, nextmax);
}
return amount;
}

View File

@ -11,9 +11,8 @@ ifndef CXXFLAGS
endif
SORTIXKERNEL=../sortix
LIBC=../libc
CPPFLAGS:=$(CPPFLAGS) -I$(SORTIXKERNEL)/include
CPPFLAGS:=$(CPPFLAGS) -I$(SORTIXKERNEL)/include -I.
CXXFLAGS:=$(CXXFLAGS) -Wall -Wextra -fno-exceptions -fno-rtti
BINARIES=mkinitrd initrdfs
@ -24,8 +23,8 @@ all: $(BINARIES)
.PHONY: all install uninstall clean
%: %.cpp crc32.cpp rules.cpp $(LIBC)/unistd/ioleast.cpp
$(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $< crc32.cpp rules.cpp $(LIBC)/unistd/ioleast.cpp -o $@
%: %.cpp crc32.cpp rules.cpp
$(CXX) -std=gnu++11 $(CPPFLAGS) $(CXXFLAGS) $< crc32.cpp rules.cpp -o $@
clean:
rm -f $(BINARIES)

View File

@ -26,6 +26,7 @@
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <ioleast.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@ -35,19 +36,6 @@
#include "crc32.h"
#if !defined(sortix)
__BEGIN_DECLS
size_t preadall(int fd, void* buf, size_t count, off_t off);
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off);
size_t pwriteall(int fd, const void* buf, size_t count, off_t off);
size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off);
size_t readall(int fd, void* buf, size_t count);
size_t readleast(int fd, void* buf, size_t least, size_t max);
size_t writeall(int fd, const void* buf, size_t count);
size_t writeleast(int fd, const void* buf, size_t least, size_t max);
__END_DECLS
#endif
char* Substring(const char* str, size_t start, size_t length)
{
char* result = (char*) malloc(length+1);

View File

@ -1,6 +1,6 @@
/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2012.
Copyright(C) Jonas 'Sortie' Termansen 2012, 2013.
This file is part of the Sortix C Library.
@ -17,7 +17,7 @@
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/>.
unistd/ioleast.cpp
ioleast.h
Versions of {,p}{read,write} that don't return until it has returned as much
data as requested, end of file, or an error occurs. This is sometimes needed
as read(2) and write(2) is not always guaranteed to fill up the entire
@ -25,20 +25,34 @@
*******************************************************************************/
#ifndef SORTIX_COMPATIBILITY_INCLUDE_IOLEAST_H
#define SORTIX_COMPATIBILITY_INCLUDE_IOLEAST_H
#if defined(__sortix__) || defined(__sortix_libc__)
#include_next <ioleast.h>
#else
#include <sys/types.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#ifndef EEOF
#if !defined(EEOF) && defined(EIO)
#define EEOF EIO
#endif
extern "C" size_t readleast(int fd, void* buf, size_t least, size_t max)
__attribute__((unused)) static inline
size_t readleast(int fd, void* buf, size_t least, size_t max)
{
ssize_t amount = read(fd, buf, max);
if ( amount < 0 ) { return 0; }
if ( least && !amount ) { errno = EEOF; return 0; }
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
void* nextbuf = (uint8_t*) buf + amount;
@ -49,17 +63,32 @@ extern "C" size_t readleast(int fd, void* buf, size_t least, size_t max)
return amount;
}
extern "C" size_t readall(int fd, void* buf, size_t count)
__attribute__((unused)) static inline
size_t writeleast(int fd, const void* buf, size_t least, size_t max)
{
return readleast(fd, buf, count, count);
ssize_t amount = write(fd, buf, max);
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
const void* nextbuf = (const uint8_t*) buf + amount;
size_t nextleast = least - amount;
size_t nextmax = max - amount;
amount += writeleast(fd, nextbuf, nextleast, nextmax);
}
return amount;
}
extern "C" size_t preadleast(int fd, void* buf, size_t least, size_t max,
off_t off)
__attribute__((unused)) static inline
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off)
{
ssize_t amount = pread(fd, buf, max, off);
if ( amount < 0 ) { return 0; }
if ( least && !amount ) { errno = EEOF; return 0; }
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
void* nextbuf = (uint8_t*) buf + amount;
@ -71,37 +100,14 @@ extern "C" size_t preadleast(int fd, void* buf, size_t least, size_t max,
return amount;
}
extern "C" size_t preadall(int fd, void* buf, size_t count, off_t off)
{
return preadleast(fd, buf, count, count, off);
}
extern "C" size_t writeleast(int fd, const void* buf, size_t least, size_t max)
{
ssize_t amount = write(fd, buf, max);
if ( amount < 0 ) { return 0; }
if ( least && !amount ) { errno = EEOF; return 0; }
if ( (size_t) amount < least )
{
const void* nextbuf = (const uint8_t*) buf + amount;
size_t nextleast = least - amount;
size_t nextmax = max - amount;
amount += writeleast(fd, nextbuf, nextleast, nextmax);
}
return amount;
}
extern "C" size_t writeall(int fd, const void* buf, size_t count)
{
return writeleast(fd, buf, count, count);
}
extern "C" size_t pwriteleast(int fd, const void* buf, size_t least, size_t max,
off_t off)
__attribute__((unused)) static inline
size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off)
{
ssize_t amount = pwrite(fd, buf, max, off);
if ( amount < 0 ) { return 0; }
if ( least && !amount ) { errno = EEOF; return 0; }
if ( amount < 0 )
return 0;
if ( least && !amount )
return errno = EEOF, 0;
if ( (size_t) amount < least )
{
const void* nextbuf = (const uint8_t*) buf + amount;
@ -113,7 +119,30 @@ extern "C" size_t pwriteleast(int fd, const void* buf, size_t least, size_t max,
return amount;
}
extern "C" size_t pwriteall(int fd, const void* buf, size_t count, off_t off)
__attribute__((unused)) static inline
size_t readall(int fd, void* buf, size_t count)
{
return readleast(fd, buf, count, count);
}
__attribute__((unused)) static inline
size_t writeall(int fd, const void* buf, size_t count)
{
return writeleast(fd, buf, count, count);
}
__attribute__((unused)) static inline
size_t preadall(int fd, void* buf, size_t count, off_t off)
{
return preadleast(fd, buf, count, count, off);
}
__attribute__((unused)) static inline
size_t pwriteall(int fd, const void* buf, size_t count, off_t off)
{
return pwriteleast(fd, buf, count, count, off);
}
#endif
#endif

View File

@ -27,6 +27,7 @@
#include <errno.h>
#include <error.h>
#include <fcntl.h>
#include <ioleast.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
@ -38,19 +39,6 @@
#include "crc32.h"
#include "rules.h"
#if !defined(sortix)
__BEGIN_DECLS
size_t preadall(int fd, void* buf, size_t count, off_t off);
size_t preadleast(int fd, void* buf, size_t least, size_t max, off_t off);
size_t pwriteall(int fd, const void* buf, size_t count, off_t off);
size_t pwriteleast(int fd, const void* buf, size_t least, size_t max, off_t off);
size_t readall(int fd, void* buf, size_t count);
size_t readleast(int fd, void* buf, size_t least, size_t max);
size_t writeall(int fd, const void* buf, size_t count);
size_t writeleast(int fd, const void* buf, size_t least, size_t max);
__END_DECLS
#endif
uint32_t HostModeToInitRD(mode_t mode)
{
uint32_t result = mode & 0777; // Lower 9 bits per POSIX and tradition.