From: John Hodge Date: Mon, 8 Aug 2011 08:52:15 +0000 (+0800) Subject: Usermode - Switched to the POSIX read()/write() argument order X-Git-Tag: rel0.11~158 X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=6c7e82169e66197939b83945812b02787ed0f52e;p=tpg%2Facess2.git Usermode - Switched to the POSIX read()/write() argument order --- diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index af817825..807527b1 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -77,8 +77,7 @@ int main(int argc, char *argv[], char *envp[]) } } - write(_stdout, 22, "Acess Shell Version 3\n"); - write(_stdout, 2, "\n"); + printf("Acess Shell Version 3\n\n"); for(;;) { // Free last command & arguments @@ -88,8 +87,7 @@ int main(int argc, char *argv[], char *envp[]) bCached = 0; #endif - write(_stdout, strlen(gsCurrentDirectory), gsCurrentDirectory); - write(_stdout, 2, "$ "); + printf("%s$ ", gsCurrentDirectory); // Read Command line #if USE_READLINE @@ -100,7 +98,7 @@ int main(int argc, char *argv[], char *envp[]) sCommandStr = ReadCommandLine( &length ); if(!sCommandStr) { - write(_stdout, 25, "PANIC: Out of heap space\n"); + printf("PANIC: Out of heap space\n"); return -1; } @@ -147,6 +145,7 @@ int main(int argc, char *argv[], char *envp[]) } } +#if USE_READLINE /** * \fn char *ReadCommandLine(int *Length) * \brief Read from the command line @@ -167,21 +166,20 @@ char *ReadCommandLine(int *Length) // Read In Command Line do { - read(_stdin, 1, &ch); // Read Character from stdin (read is a blocking call) - + ch = getchar(); // Read Character from stdin (read is a blocking call) if(ch == '\n') break; switch(ch) { // Control characters case '\x1B': - read(_stdin, 1, &ch); // Read control character + ch = getchar(); // Read control character switch(ch) { //case 'D': if(pos) pos--; break; //case 'C': if(posFD, "PONG %s\n", gsHostname); + } else if( strcmp(cmd, "NOTICE") == 0 ) { char *class, *message; @@ -643,7 +647,7 @@ int ProcessIncoming(tServer *Server) { #endif // Read data - len = read(Server->FD, BUFSIZ - Server->ReadPos, &Server->InBuf[Server->ReadPos]); + len = read(Server->FD, &Server->InBuf[Server->ReadPos], BUFSIZ - Server->ReadPos); if( len == -1 ) { return -1; } @@ -697,7 +701,7 @@ int writef(int FD, const char *Format, ...) vsnprintf(buf, len+1, Format, args); va_end(args); - return write(FD, len, buf); + return write(FD, buf, len); } } diff --git a/Usermode/Applications/pcidump_src/main.c b/Usermode/Applications/pcidump_src/main.c index 919d5a69..2bae2c9a 100644 --- a/Usermode/Applications/pcidump_src/main.c +++ b/Usermode/Applications/pcidump_src/main.c @@ -63,25 +63,25 @@ int main(int argc, char *argv[], char *envp[]) fp = open(tmpPath, OPENFLAG_READ); if(fp == -1) continue; - read(fp, 2, &vendor); read(fp, 2, &device); + read(fp, &vendor, 2); read(fp, &device, 2); printf(" Vendor 0x%04x, Device 0x%04x\n", vendor, device); printf(" %s - %s\n", GetVendorName(vendor), GetDeviceName(vendor, device)); // Reuse vendor and device seek(fp, 0x8, SEEK_SET); - read(fp, 2, &vendor); read(fp, 2, &device); + read(fp, &vendor, 2); read(fp, &device, 2); printf(" Revision 0x%04x, Class 0x%04x ()\n", vendor, device); // Read File #if DUMP_BARS seek(fp, 0x10, SEEK_SET); printf("Base Address Registers (BARs):\n"); - read(fp, 4, &tmp32); printf(" 0x%08x", tmp32); - read(fp, 4, &tmp32); printf(" 0x%08x", tmp32); - read(fp, 4, &tmp32); printf(" 0x%08x", tmp32); - read(fp, 4, &tmp32); printf(" 0x%08x", tmp32); - read(fp, 4, &tmp32); printf(" 0x%08x", tmp32); - read(fp, 4, &tmp32); printf(" 0x%08x", tmp32); + read(fp, &tmp32, 4); printf(" 0x%08x", tmp32); + read(fp, &tmp32, 4); printf(" 0x%08x", tmp32); + read(fp, &tmp32, 4); printf(" 0x%08x", tmp32); + read(fp, &tmp32, 4); printf(" 0x%08x", tmp32); + read(fp, &tmp32, 4); printf(" 0x%08x", tmp32); + read(fp, &tmp32, 4); printf(" 0x%08x", tmp32); printf("\n"); #endif printf("\n"); diff --git a/Usermode/Applications/telnet_src/main.c b/Usermode/Applications/telnet_src/main.c index 0d12c1cb..7733805f 100644 --- a/Usermode/Applications/telnet_src/main.c +++ b/Usermode/Applications/telnet_src/main.c @@ -64,8 +64,8 @@ int main(int argc, char *argv[], char *envp[]) // Read from server, and write to stdout do { - len = read(server_fd, BUFSIZ, buffer); - write(1, len, buffer); + len = read(server_fd, buffer, BUFSIZ); + write(1, buffer, len); } while( len == BUFSIZ ); } @@ -78,17 +78,17 @@ int main(int argc, char *argv[], char *envp[]) char *line = Readline_NonBlock(readline_info); if( line ) { - write(server_fd, strlen(line), line); - write(server_fd, 1, "\n"); + write(server_fd, line, strlen(line)); + write(server_fd, "\n", 1); } } else { do { - len = read(0, BUFSIZ, buffer); - write(server_fd, len, buffer); - write(1, len, buffer); + len = read(0, buffer, BUFSIZ); + write(server_fd, buffer, len); + write(1, buffer, len); } while( len == BUFSIZ ); } } diff --git a/Usermode/Libraries/ld-acess.so_src/common.h b/Usermode/Libraries/ld-acess.so_src/common.h index 3eef946c..ec254bf2 100644 --- a/Usermode/Libraries/ld-acess.so_src/common.h +++ b/Usermode/Libraries/ld-acess.so_src/common.h @@ -60,7 +60,7 @@ extern void *SysLoadBin(const char *path, void **entry); extern int SysUnloadBin(void *Base); extern void SysSetFaultHandler(int (*Hanlder)(int)); extern int open(const char *filename, int flags); -extern void close(int fd); +extern int close(int fd); // === ELF Loader === extern int ElfGetSymbol(void *Base, char *name, void **ret); diff --git a/Usermode/Libraries/libc.so_src/fileIO.c b/Usermode/Libraries/libc.so_src/fileIO.c index b588f246..60def892 100644 --- a/Usermode/Libraries/libc.so_src/fileIO.c +++ b/Usermode/Libraries/libc.so_src/fileIO.c @@ -10,6 +10,8 @@ #include "lib.h" #include "stdio_int.h" +#define WRITE_STR(_fd, _str) write(_fd, _str, sizeof(_str)) + #define DEBUG_BUILD 0 // === CONSTANTS === @@ -116,10 +118,12 @@ EXPORT FILE *fopen(const char *file, const char *mode) return freopen(file, mode, retFile); } -EXPORT void fclose(FILE *fp) +EXPORT int fclose(FILE *fp) { close(fp->FD); - free(fp); + fp->Flags = 0; + fp->FD = -1; + return 0; } EXPORT void fflush(FILE *fp) @@ -152,20 +156,18 @@ EXPORT int vfprintf(FILE *fp, const char *format, va_list args) int size; char sbuf[1024]; char *buf = sbuf; - - if(!fp || !format) return -1; va_copy(tmpList, args); - size = vsnprintf(sbuf, 1024, (char*)format, tmpList); + size = vsnprintf(sbuf, sizeof(sbuf), (char*)format, tmpList); - if( size >= 1024 ) + if( size >= sizeof(sbuf) ) { buf = (char*)malloc(size+1); if(!buf) { - write(_stdout, 31, "vfprintf ERROR: malloc() failed"); + WRITE_STR(_stdout, "vfprintf ERROR: malloc() failed"); return 0; } buf[size] = '\0'; @@ -175,7 +177,7 @@ EXPORT int vfprintf(FILE *fp, const char *format, va_list args) } // Write to stream - write(fp->FD, size, buf); + write(fp->FD, buf, size); // Free buffer free(buf); @@ -210,7 +212,7 @@ EXPORT size_t fwrite(void *ptr, size_t size, size_t num, FILE *fp) int ret; if(!fp || !fp->FD) return -1; - ret = write(fp->FD, size*num, ptr); + ret = write(fp->FD, ptr, size*num); return ret; } @@ -224,7 +226,7 @@ EXPORT size_t fread(void *ptr, size_t size, size_t num, FILE *fp) int ret; if(!fp || !fp->FD) return -1; - ret = read(fp->FD, size*num, ptr); + ret = read(fp->FD, ptr, size*num); return ret; } @@ -236,7 +238,13 @@ EXPORT size_t fread(void *ptr, size_t size, size_t num, FILE *fp) EXPORT int fputc(int c, FILE *fp) { if(!fp || !fp->FD) return -1; - return write(fp->FD, 1, &c); + return write(fp->FD, &c, 1); +} + +EXPORT int putchar(int c) +{ + c &= 0xFF; + return write(_stdout, &c, 1); } /** @@ -247,7 +255,14 @@ EXPORT int fgetc(FILE *fp) { char ret = 0; if(!fp) return -1; - if(read(fp->FD, 1, &ret) == -1) return -1; + if(read(fp->FD, &ret, 1) == -1) return -1; + return ret; +} + +EXPORT int getchar(void) +{ + char ret = 0; + if(read(_stdin, &ret, 1) != 1) return -1; return ret; } @@ -266,20 +281,15 @@ FILE *get_file_struct() return NULL; } -EXPORT int putchar(int ch) -{ - return write(_stdout, 1, (char*)&ch); -} - -EXPORT int puts(const char *str) +EXPORT int puts(const char *str) { int len; if(!str) return 0; len = strlen(str); - len = write(_stdout, len, (char*)str); - write(_stdout, 1, "\n"); + len = write(_stdout, str, len); + write(_stdout, "\n", 1); return len; } @@ -546,7 +556,7 @@ EXPORT int printf(const char *format, ...) // Allocate buffer buf = (char*)malloc(size+1); if(buf) { - write(_stdout, 29, "PRINTF ERROR: malloc() failed"); + WRITE_STR(_stdout, "PRINTF ERROR: malloc() failed\n"); return 0; } buf[size] = '\0'; @@ -558,7 +568,7 @@ EXPORT int printf(const char *format, ...) } // Send to stdout - write(_stdout, size+1, buf); + write(_stdout, buf, size+1); // Free buffer free(buf); diff --git a/Usermode/Libraries/libgcc.so_src/libgcc.c b/Usermode/Libraries/libgcc.so_src/libgcc.c index 8f6021f2..8c5c13a0 100644 --- a/Usermode/Libraries/libgcc.so_src/libgcc.c +++ b/Usermode/Libraries/libgcc.so_src/libgcc.c @@ -13,7 +13,7 @@ int SoMain() // --- Errors --- void __stack_chk_fail() { - write(2, 32, "FATAL ERROR: Stack Check Failed\n"); + write(2, "FATAL ERROR: Stack Check Failed\n", 32); _exit(-1); for(;;); } diff --git a/Usermode/Libraries/libreadline.so_src/main.c b/Usermode/Libraries/libreadline.so_src/main.c index 58bcb43d..1408eef7 100644 --- a/Usermode/Libraries/libreadline.so_src/main.c +++ b/Usermode/Libraries/libreadline.so_src/main.c @@ -80,7 +80,7 @@ char *Readline_NonBlock(tReadline *Info) int len, i; // Read as much as possible (appending to remaining data) - len = read(STDIN_FD, READ_BUFFER_SIZE - 1 - Info->ReadBufferLen, Info->ReadBuffer); + len = read(STDIN_FD, Info->ReadBuffer, READ_BUFFER_SIZE - 1 - Info->ReadBufferLen); if( len <= 0 ) return NULL; Info->ReadBuffer[Info->ReadBufferLen + len] = '\0'; @@ -204,21 +204,21 @@ int Readline_int_ParseCharacter(tReadline *Info, char *Input) // Move to the beginning of the line pos = oldLen; - while(pos--) write(STDOUT_FD, 3, "\x1B[D"); + while(pos--) write(STDOUT_FD, "\x1B[D", 3); // Update state Info->CurBuffer = Info->History[--Info->HistoryPos]; Info->BufferSize = Info->BufferUsed = strlen(Info->CurBuffer); - write(STDOUT_FD, Info->BufferUsed, Info->CurBuffer); + write(STDOUT_FD, Info->CurBuffer, Info->BufferUsed); Info->BufferWritePos = Info->BufferUsed; // Clear old characters (if needed) if( oldLen > Info->BufferWritePos ) { pos = oldLen - Info->BufferWritePos; - while(pos--) write(STDOUT_FD, 1, " "); + while(pos--) write(STDOUT_FD, " ", 1); pos = oldLen - Info->BufferWritePos; - while(pos--) write(STDOUT_FD, 3, "\x1B[D"); + while(pos--) write(STDOUT_FD, "\x1B[D", 3); } } break; @@ -230,34 +230,34 @@ int Readline_int_ParseCharacter(tReadline *Info, char *Input) // Move to the beginning of the line pos = oldLen; - while(pos--) write(STDOUT_FD, 3, "\x1B[D"); + while(pos--) write(STDOUT_FD, "\x1B[D", 3); // Update state Info->CurBuffer = Info->History[Info->HistoryPos++]; Info->BufferSize = Info->BufferUsed = strlen(Info->CurBuffer); // Write new line - write(STDOUT_FD, Info->BufferUsed, Info->CurBuffer); + write(STDOUT_FD, Info->CurBuffer, Info->BufferUsed); Info->BufferWritePos = Info->BufferUsed; // Clear old characters (if needed) if( oldLen > Info->BufferWritePos ) { pos = oldLen - Info->BufferWritePos; - while(pos--) write(STDOUT_FD, 1, " "); + while(pos--) write(STDOUT_FD, " ", 1); pos = oldLen - Info->BufferWritePos; - while(pos--) write(STDOUT_FD, 3, "\x1B[D"); + while(pos--) write(STDOUT_FD, "\x1B[D", 3); } } break; case 'D': // Left if(Info->BufferWritePos == 0) break; Info->BufferWritePos --; - write(STDOUT_FD, 3, "\x1B[D"); + write(STDOUT_FD, "\x1B[D", 3); break; case 'C': // Right if(Info->BufferWritePos == Info->BufferUsed) break; Info->BufferWritePos ++; - write(STDOUT_FD, 3, "\x1B[C"); + write(STDOUT_FD, "\x1B[C", 3); break; } } @@ -267,7 +267,7 @@ int Readline_int_ParseCharacter(tReadline *Info, char *Input) case '\b': if(Info->BufferWritePos <= 0) break; // Protect against underflows // Write the backsapce - write(STDOUT_FD, 1, &ch); + write(STDOUT_FD, &ch, 1); if(Info->BufferWritePos == Info->BufferUsed) // Simple case: End of string { Info->BufferUsed --; @@ -283,11 +283,11 @@ int Readline_int_ParseCharacter(tReadline *Info, char *Input) buf[4] += (delta) % 10; // Write everything save for the deleted character write(STDOUT_FD, - Info->BufferUsed - Info->BufferWritePos, - &Info->CurBuffer[Info->BufferWritePos] + &Info->CurBuffer[Info->BufferWritePos], + Info->BufferUsed - Info->BufferWritePos ); - ch = ' '; write(STDOUT_FD, 1, &ch); ch = '\b'; // Clear old last character - write(STDOUT_FD, 7, buf); // Update Cursor + ch = ' '; write(STDOUT_FD, &ch, 1); ch = '\b'; // Clear old last character + write(STDOUT_FD, buf, 7); // Update Cursor // Alter Buffer memmove(&Info->CurBuffer[Info->BufferWritePos-1], &Info->CurBuffer[Info->BufferWritePos], @@ -321,12 +321,12 @@ int Readline_int_ParseCharacter(tReadline *Info, char *Input) buf[2] += (delta/100) % 10; buf[3] += (delta/10) % 10; buf[4] += (delta) % 10; - write(STDOUT_FD, 1, &ch); // Print new character + write(STDOUT_FD, &ch, 1); // Print new character write(STDOUT_FD, - Info->BufferUsed - Info->BufferWritePos, - &Info->CurBuffer[Info->BufferWritePos] + &Info->CurBuffer[Info->BufferWritePos], + Info->BufferUsed - Info->BufferWritePos ); - write(STDOUT_FD, 7, buf); // Update Cursor + write(STDOUT_FD, buf, 7); // Update Cursor // Move buffer right memmove( &Info->CurBuffer[Info->BufferWritePos+1], @@ -336,7 +336,7 @@ int Readline_int_ParseCharacter(tReadline *Info, char *Input) } // Simple append else { - write(STDOUT_FD, 1, &ch); + write(STDOUT_FD, &ch, 1); } Info->CurBuffer[ Info->BufferWritePos ++ ] = ch; Info->BufferUsed ++; diff --git a/Usermode/include/acess/sys.h b/Usermode/include/acess/sys.h index c26cec1a..6d7751b9 100644 --- a/Usermode/include/acess/sys.h +++ b/Usermode/include/acess/sys.h @@ -58,9 +58,9 @@ extern void setgid(int id); extern int chdir(const char *dir); extern int open(const char *path, int flags); extern int reopen(int fd, const char *path, int flags); -extern void close(int fd); -extern uint read(int fd, uint length, void *buffer); -extern uint write(int fd, uint length, void *buffer); +extern int close(int fd); +extern uint read(int fd, void *buffer, uint length); +extern uint write(int fd, const void *buffer, uint length); extern int seek(int fd, int64_t offset, int whence); extern uint64_t tell(int fd); extern int ioctl(int fd, int id, void *data); @@ -72,7 +72,7 @@ extern int _SysMount(const char *Device, const char *Directory, const char *Type extern int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *errfds, time_t *timeout); // --- IPC --- -extern int SysSendMessage(pid_t dest, uint length, void *Data); +extern int SysSendMessage(pid_t dest, uint length, const void *Data); extern int SysGetMessage(pid_t *src, void *Data); // --- MEMORY --- diff --git a/Usermode/include/stdint.h b/Usermode/include/stdint.h index adfb101e..0e43363d 100644 --- a/Usermode/include/stdint.h +++ b/Usermode/include/stdint.h @@ -17,12 +17,15 @@ typedef signed long long int64_t; #ifdef __LP64__ typedef uint64_t intptr_t; typedef uint64_t uintptr_t; +typedef int64_t ptrdiff_t; #else typedef uint32_t intptr_t; typedef uint32_t uintptr_t; +typedef int32_t ptrdiff_t; #endif #if 0 # error "Unknown pointer size" #endif + #endif diff --git a/Usermode/include/stdio.h b/Usermode/include/stdio.h index ce42a159..552cd4f9 100644 --- a/Usermode/include/stdio.h +++ b/Usermode/include/stdio.h @@ -1,45 +1,47 @@ -/* - * AcessOS LibC - * stdlib.h - */ -#ifndef __STDIO_H +/* + * AcessOS LibC + * stdlib.h + */ +#ifndef __STDIO_H #define __STDIO_H - -#include -#include - + +#include +#include + // === Types === typedef struct sFILE FILE; - -// === CONSTANTS === -#define EOF (-1) - -// --- Standard IO --- -extern int printf(const char *format, ...); -extern int vsnprintf(char *buf, size_t __maxlen, const char *format, va_list args); -extern int vsprintf(char *buf, const char *format, va_list args); -extern int sprintf(char *buf, const char *format, ...); -extern int snprintf(char *buf, size_t maxlen, const char *format, ...); - -extern FILE *fopen(const char *file, const char *mode); -extern FILE *freopen(const char *file, const char *mode, FILE *fp); + +// === CONSTANTS === +#define EOF (-1) + +// --- Standard IO --- +extern int printf(const char *format, ...); +extern int vsnprintf(char *buf, size_t __maxlen, const char *format, va_list args); +extern int vsprintf(char *buf, const char *format, va_list args); +extern int sprintf(char *buf, const char *format, ...); +extern int snprintf(char *buf, size_t maxlen, const char *format, ...); + +extern FILE *fopen(const char *file, const char *mode); +extern FILE *freopen(const char *file, const char *mode, FILE *fp); extern FILE *fdopen(int fd, const char *modes); -extern void fclose(FILE *fp); -extern void fflush(FILE *fp); -extern long int ftell(FILE *fp); -extern int fseek(FILE *fp, long int amt, int whence); - -extern size_t fread(void *buf, size_t size, size_t n, FILE *fp); -extern size_t fwrite(void *buf, size_t size, size_t n, FILE *fp); -extern int fgetc(FILE *fp); -extern int fputc(int ch, FILE *fp); - -extern int fprintf(FILE *fp, const char *format, ...); -extern int vfprintf(FILE *fp, const char *format, va_list args); - -extern FILE *stdin; -extern FILE *stdout; -extern FILE *stderr; - -#endif +extern int fclose(FILE *fp); +extern void fflush(FILE *fp); +extern long int ftell(FILE *fp); +extern int fseek(FILE *fp, long int amt, int whence); + +extern size_t fread(void *buf, size_t size, size_t n, FILE *fp); +extern size_t fwrite(void *buf, size_t size, size_t n, FILE *fp); +extern int fgetc(FILE *fp); +extern int fputc(int ch, FILE *fp); +extern int getchar(void); +extern int putchar(int ch); + +extern int fprintf(FILE *fp, const char *format, ...); +extern int vfprintf(FILE *fp, const char *format, va_list args); + +extern FILE *stdin; +extern FILE *stdout; +extern FILE *stderr; + +#endif diff --git a/Usermode/include/sys/sys.h b/Usermode/include/sys/sys.h index a934f75a..08bf31e9 100644 --- a/Usermode/include/sys/sys.h +++ b/Usermode/include/sys/sys.h @@ -4,8 +4,18 @@ #ifndef _SYS_SYS_H_ #define _SYS_SYS_H_ +#include + #include +#define O_RDONLY OPENFLAG_READ +#define O_WRONLY OPENFLAG_WRITE +#define O_CREAT (OPENFLAG_CREATE|OPENFLAG_WRITE) +#define O_TRUNC OPENFLAG_WRITE +#define O_APPEND OPENFLAG_WRITE + + +#if 0 #define OPEN_FLAG_READ 1 #define OPEN_FLAG_WRITE 2 #define OPEN_FLAG_EXEC 4 @@ -41,5 +51,6 @@ extern int getpid(); // Get Process ID extern int sendmsg(int dest, unsigned int *Data); extern int pollmsg(int *src, unsigned int *Data); extern int getmsg(int *src, unsigned int *Data); +#endif #endif