printf(3) now writes to fd 1.

This commit is contained in:
Jonas 'Sortie' Termansen 2011-11-24 10:26:36 +01:00
parent e72d086a8f
commit 4890c306c4
7 changed files with 42 additions and 51 deletions

View File

@ -43,9 +43,15 @@
#define __POSIX_OBSOLETE 200112L
#endif
#include <sortix/bits.h>
/* Whether sortix-specific extensions to the C library are available. */
#ifndef SORTIX_NO_EXTENSIONS
#define SORTIX_EXTENSIONS
#endif
// Don't provide things from standard headers that is not implemented in libmaxsi/sortix.
/* Don't provide things from standard headers that is not implemented. */
#define SORTIX_UNIMPLEMENTED
#include <sortix/bits.h>
#endif

View File

@ -15,7 +15,7 @@
more details.
You should have received a copy of the GNU Lesser General Public License
along with LibMaxsi. If not, see <http:/*www.gnu.org/licenses/>.
along with LibMaxsi. If not, see <http://www.gnu.org/licenses/>.
unistd.h
The <unistd.h> header defines miscellaneous symbolic constants and types,
@ -170,6 +170,9 @@ int usleep(useconds_t useconds);
#endif
int unlink(const char*);
ssize_t write(int, const void*, size_t);
#ifdef SORTIX_EXTENSIONS
int writeall(int fd, const void* buffer, size_t len);
#endif
__END_DECLS

View File

@ -26,7 +26,9 @@
#include "syscall.h"
#include "io.h"
#include "format.h"
#include "string.h"
#include <sys/readdirents.h>
#include <unistd.h>
namespace Maxsi
{
@ -42,14 +44,17 @@ namespace Maxsi
DEFN_SYSCALL2(char*, SysGetCWD, 26, char*, size_t);
DEFN_SYSCALL1(int, SysUnlink, 27, const char*);
size_t Print(const char* Message)
size_t Print(const char* string)
{
return SysPrint(Message);
size_t stringlen = String::Length(string);
if ( writeall(1, string, stringlen) ) { return 0; }
return stringlen;
}
size_t PrintCallback(void* user, const char* string, size_t stringlen)
{
return SysPrint(string);
if ( writeall(1, string, stringlen) ) { return 0; }
return stringlen;
}
size_t PrintF(const char* format, ...)
@ -81,6 +86,20 @@ namespace Maxsi
return SysWrite(fd, buf, count);
}
extern "C" int writeall(int fd, const void* buffer, size_t len)
{
const char* buf = (const char*) buffer;
while ( len )
{
ssize_t byteswritten = write(fd, buf, len);
if ( byteswritten < 0 ) { return (int) byteswritten; }
buf += byteswritten;
len -= byteswritten;
}
return 0;
}
extern "C" int pipe(int pipefd[2])
{
return SysPipe(pipefd);

View File

@ -5,20 +5,6 @@
#include <errno.h>
#include <libmaxsi/sortix-keyboard.h>
bool writeall(int fd, const void* buffer, size_t len)
{
const char* buf = (const char*) buffer;
while ( len )
{
ssize_t byteswritten = write(fd, buf, len);
if ( byteswritten < 0 ) { return false; }
buf += byteswritten;
len -= byteswritten;
}
return true;
}
int cat(int argc, char* argv[])
{
int result = 0;
@ -48,7 +34,7 @@ int cat(int argc, char* argv[])
result = 1;
break;
}
if ( !writeall(outfd, buffer, bytesread) )
if ( writeall(outfd, buffer, bytesread) )
{
printf("%s: /dev/tty: %s\n", argv[0], strerror(errno));
result = 1;

View File

@ -4,20 +4,6 @@
#include <errno.h>
#include <string.h>
bool writeall(int fd, const void* buffer, size_t len)
{
const char* buf = (const char*) buffer;
while ( len )
{
ssize_t byteswritten = write(fd, buf, len);
if ( byteswritten < 0 ) { return false; }
buf += byteswritten;
len -= byteswritten;
}
return true;
}
const char* basename(const char* path)
{
size_t len = strlen(path);
@ -62,6 +48,6 @@ int main(int argc, char* argv[])
ssize_t bytesread = read(fromfd, buffer, BUFFER_SIZE);
if ( bytesread < 0 ) { printf("%s: %s: %s\n", argv[0], frompath, strerror(errno)); return 1; }
if ( bytesread == 0 ) { return 0; }
if ( !writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; }
if ( writeall(tofd, buffer, bytesread) ) { printf("%s: %s: %s\n", argv[0], topath, strerror(errno)); return 1; }
}
}

View File

@ -192,20 +192,6 @@ unsigned confirmquit()
}
}
bool writeall(int fd, const void* buffer, size_t len)
{
const char* buf = (const char*) buffer;
while ( len )
{
ssize_t byteswritten = write(fd, buf, len);
if ( byteswritten < 0 ) { return false; }
buf += byteswritten;
len -= byteswritten;
}
return true;
}
bool savetofile(const char* path)
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0777);
@ -215,7 +201,7 @@ bool savetofile(const char* path)
{
size_t len = strlen(buffers[y]);
buffers[y][len] = '\n';
bool result = writeall(fd, buffers[y], len+1);
bool result = !writeall(fd, buffers[y], len+1);
buffers[y][len] = 0;
if ( !result ) { printf("%s: %s\n", path, strerror(errno)); close(fd); return false; }
}

View File

@ -2,6 +2,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <libmaxsi/platform.h>
#include <libmaxsi/process.h>
#include <libmaxsi/thread.h>
@ -27,6 +28,10 @@ int child()
int main(int argc, char* argv[])
{
if ( open("/dev/tty", O_RDONLY) != 0 ) { return 2; }
if ( open("/dev/tty", O_WRONLY | O_APPEND) != 1 ) { return 2; }
if ( open("/dev/tty", O_WRONLY | O_APPEND) != 2 ) { return 2; }
// Reset the terminal's color and the rest of it.
printf("\r\e[m\e[J");