From a2210987109ab5a6337c72b45f7e52cfc9092f8f Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 3 Oct 2009 11:37:01 +0800 Subject: [PATCH] Fixed correctness in stdio, minor changes in ls to handle dirs with negative sizes --- Usermode/Applications/CLIShell_src/main.c | 5 +- Usermode/Applications/ls_src/main.c | 25 ++- Usermode/Libraries/libc.so_src/Makefile | 2 +- Usermode/Libraries/libc.so_src/env.c | 5 +- Usermode/Libraries/libc.so_src/fileIO.c | 256 ++++++++++------------ Usermode/Libraries/libgcc.so_src/libgcc.c | 7 +- Usermode/include/stdint.h | 2 + Usermode/include/stdio.h | 6 +- 8 files changed, 141 insertions(+), 167 deletions(-) diff --git a/Usermode/Applications/CLIShell_src/main.c b/Usermode/Applications/CLIShell_src/main.c index 914557a5..2e51419f 100644 --- a/Usermode/Applications/CLIShell_src/main.c +++ b/Usermode/Applications/CLIShell_src/main.c @@ -469,10 +469,7 @@ void Command_Dir(int argc, char **argv) // Check if file opened if(dp == -1) { - //printf("Unable to open directory `%s', File cannot be found\n", tmpPath); - write(_stdout, 27, "Unable to open directory `"); - write(_stdout, strlen(tmpPath)+1, tmpPath); - write(_stdout, 25, "', File cannot be found\n"); + printf("Unable to open directory `%s', File cannot be found\n", tmpPath); return; } // Get File Stats diff --git a/Usermode/Applications/ls_src/main.c b/Usermode/Applications/ls_src/main.c index fc99a405..3ae63fd9 100644 --- a/Usermode/Applications/ls_src/main.c +++ b/Usermode/Applications/ls_src/main.c @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) // Check that it is a directory finfo(fd, &info, 0); if( !(info.flags & FILEFLAG_DIRECTORY) ) { - fprintf(stderr, "'%s' is a directory\n", gsDirectory); + fprintf(stderr, "'%s' is not a directory\n", gsDirectory); close(fd); return EXIT_FAILURE; } @@ -167,8 +167,6 @@ void ParseArguments(int argc, char *argv[]) // Apply Defaults if(!gsDirectory) gsDirectory = "."; - - printf("gsDirectory = '%s'\n", gsDirectory); } /** @@ -280,26 +278,33 @@ void DisplayFile(char *Filename) if(perms & 0002) permStr[8] = 'w'; if(perms & 0001) permStr[9] = 'x'; printf("%s %4i %4i ", permStr, owner, group); - if(gbViewHumanReadable) { + if(gbViewHumanReadable && type != FTYPE_DIR) { if(size < 2048) { // < 2 KiB - printf("%8lli B ", size); + printf("%4i B ", size); } else if(size < 2048*1024) { // < 2 MiB - printf("%8lli KiB ", size>>10); + printf("%4i KiB ", size>>10); } else if(size < (uint64_t)2048*1024*1024) { // < 2 GiB - printf("%8lli MiB ", size>>20); + printf("%4i MiB ", size>>20); } else if(size < (uint64_t)2048*1024*1024*1024) { // < 2 TiB - printf("%8lli GiB ", size>>30); + printf("%4i GiB ", size>>30); } else { // Greater than 2 TiB - printf("%8i TiB ", size>>40); + printf("%4i TiB ", size>>40); } } else { printf("%8i ", size); } } - printf("%s\n", Filename); + switch(type) + { + case FTYPE_DIR: printf("\x1B[32m"); break; // Green + case FTYPE_SYMLINK: printf("\x1B[34m"); break; // Blue + case FTYPE_NORMAL: break; + } + printf("%s%s\n", Filename, (type==FTYPE_DIR?"/":"")); + printf("\x1B[00m"); // Reset } diff --git a/Usermode/Libraries/libc.so_src/Makefile b/Usermode/Libraries/libc.so_src/Makefile index c2979a07..32eb52b4 100644 --- a/Usermode/Libraries/libc.so_src/Makefile +++ b/Usermode/Libraries/libc.so_src/Makefile @@ -6,7 +6,7 @@ CPPFLAGS += CFLAGS += ASFLAGS += -LDFLAGS += -soname libc.so.1 -Map map.txt +LDFLAGS += -soname libc.so.1 -Map map.txt -lgcc OBJ_LIBC = heap.o stdlib.o stub.o env.o fileIO.o string.o # signals.o diff --git a/Usermode/Libraries/libc.so_src/env.c b/Usermode/Libraries/libc.so_src/env.c index c0f8ea3f..3dca4df7 100644 --- a/Usermode/Libraries/libc.so_src/env.c +++ b/Usermode/Libraries/libc.so_src/env.c @@ -1,8 +1,9 @@ /* -AcessOS C Library -- Environment Handler + * Acess C Library + * - Environment Handler */ #include +#include // === GLOBALS === char **_envp = NULL; diff --git a/Usermode/Libraries/libc.so_src/fileIO.c b/Usermode/Libraries/libc.so_src/fileIO.c index b8cdb116..23a760d6 100644 --- a/Usermode/Libraries/libc.so_src/fileIO.c +++ b/Usermode/Libraries/libc.so_src/fileIO.c @@ -1,6 +1,7 @@ /* -AcessOS Basic C Library -*/ + * AcessOS Basic C Library + * stdio.c + */ #include "config.h" #include #include @@ -16,7 +17,7 @@ AcessOS Basic C Library #define _stdout 1 // === PROTOTYPES === -EXPORT void itoa(char *buf, unsigned long num, int base, int minLength, char pad); +EXPORT void itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned); struct sFILE *get_file_struct(); // === GLOBALS === @@ -124,10 +125,10 @@ EXPORT void fflush(FILE *fp) } /** - * \fn int fprintfv(FILE *fp, const char *format, va_list args) + * \fn EXPORT int vfprintf(FILE *fp, const char *format, va_list args) * \brief Print to a file from a variable argument list */ -EXPORT int fprintfv(FILE *fp, const char *format, va_list args) +EXPORT int vfprintf(FILE *fp, const char *format, va_list args) { va_list tmpList = args; int size; @@ -135,13 +136,13 @@ EXPORT int fprintfv(FILE *fp, const char *format, va_list args) if(!fp || !format) return -1; - size = ssprintfv((char*)format, tmpList); + size = vsprintf(NULL, (char*)format, tmpList); buf = (char*)malloc(size+1); buf[size] = '\0'; // Print - sprintfv(buf, (char*)format, args); + vsprintf(buf, (char*)format, args); // Write to stream write(fp->FD, size+1, buf); @@ -164,7 +165,7 @@ EXPORT int fprintf(FILE *fp, const char *format, ...) // Get Size va_start(args, format); - ret = fprintfv(fp, (char*)format, args); + ret = vfprintf(fp, (char*)format, args); va_end(args); return ret; @@ -238,33 +239,38 @@ EXPORT int puts(const char *str) //sprintfv /** - \fn EXPORT void sprintfv(char *buf, const char *format, va_list args) + \fn EXPORT void vsprintf(char *buf, const char *format, va_list args) \brief Prints a formatted string to a buffer \param buf Pointer - Destination Buffer \param format String - Format String \param args VarArgs List - Arguments */ -EXPORT void sprintfv(char *buf, const char *format, va_list args) +EXPORT int vsprintf(char *buf, const char *format, va_list args) { char tmp[33]; - int c, arg, minSize; + int c, minSize; int pos = 0; char *p; char pad; + uint64_t arg; + int bLongLong; tmp[32] = '\0'; while((c = *format++) != 0) { - //SysDebug("c = '%c'\n", c); + // Non-control character if (c != '%') { - buf[pos++] = c; + if(buf) buf[pos] = c; + pos ++; continue; } + // Control Character c = *format++; - if(c == '%') { - buf[pos++] = '%'; + if(c == '%') { // Literal % + if(buf) buf[pos] = '%'; + pos ++; continue; } @@ -285,156 +291,104 @@ EXPORT void sprintfv(char *buf, const char *format, va_list args) } } - p = tmp; - - // Get Argument - arg = va_arg(args, int); - // Get Type - switch (c) { - case 'd': - case 'i': - if(arg < 0) { - buf[pos++] = '-'; - arg = -arg; - } - itoa(tmp, arg, 10, minSize, pad); - goto sprintf_puts; - // break; - case 'u': - itoa(tmp, arg, 10, minSize, pad); - goto sprintf_puts; - // break; - case 'p': // Pointer - buf[pos++] = '*'; - buf[pos++] = '0'; - buf[pos++] = 'x'; - case 'x': - itoa(tmp, arg, 16, minSize, pad); - goto sprintf_puts; - // break; - case 'o': - itoa(tmp, arg, 8, minSize, pad); - goto sprintf_puts; - // break; - case 'b': - itoa(tmp, arg, 2, minSize, pad); - goto sprintf_puts; - // break; - - case 's': - p = (void*)arg; - sprintf_puts: - if(!p) p = "(null)"; - while(*p) buf[pos++] = *p++; - break; - - default: - buf[pos++] = arg; - break; - } - } - buf[pos++] = '\0'; -} -/* -ssprintfv -- Size, Stream, Print Formated, Variable Argument List -*/ -/** - \fn EXPORT int ssprintfv(char *format, va_list args) - \brief Gets the total character count from a formatted string - \param format String - Format String - \param args VarArgs - Argument List -*/ -EXPORT int ssprintfv(char *format, va_list args) -{ - char tmp[33]; - int c, arg, minSize; - int len = 0; - char *p; - char pad; - - tmp[32] = '\0'; - - while((c = *format++) != 0) - { - if (c != '%') { - len++; - continue; - } - - c = *format++; - - // Literal '%' - if(c == '%') { - len++; - continue; - } - - // Padding - if(c == '0') { - pad = '0'; - c = *format++; - } else - pad = ' '; - minSize = 0; - if('1' <= c && c <= '9') + // Check for long long + bLongLong = 0; + if(c == 'l') { - while('0' <= c && c <= '9') - { - minSize *= 10; - minSize += c - '0'; - c = *format++; + c = *format++; + if(c == 'l') { + bLongLong = 1; } } - + p = tmp; - arg = va_arg(args, int); - switch (c) { - case 'd': - case 'i': - if(arg < 0) { - len ++; - arg = -arg; - } - itoa(tmp, arg, 10, minSize, pad); + + // Get Type + switch( c ) + { + // Signed Integer + case 'd': case 'i': + // Get Argument + if(bLongLong) arg = va_arg(args, int64_t); + else arg = va_arg(args, int32_t); + itoa(tmp, arg, 10, minSize, pad, 1); goto sprintf_puts; + + // Unsigned Integer case 'u': - itoa(tmp, arg, 10, minSize, pad); + // Get Argument + if(bLongLong) arg = va_arg(args, uint64_t); + else arg = va_arg(args, uint32_t); + itoa(tmp, arg, 10, minSize, pad, 0); goto sprintf_puts; - case 'p': // Pointer - len += 3; + + // Pointer + case 'p': + if(buf) { + buf[pos] = '*'; + buf[pos+1] = '0'; + buf[pos+2] = 'x'; + } + pos += 3; + // Fall through to hex + // Unsigned Hexadecimal case 'x': - itoa(tmp, arg, 16, minSize, pad); + if(bLongLong) arg = va_arg(args, uint64_t); + else arg = va_arg(args, uint32_t); + itoa(tmp, arg, 16, minSize, pad, 0); goto sprintf_puts; + + // Unsigned Octal case 'o': - itoa(tmp, arg, 8, minSize, pad); - p = tmp; + if(bLongLong) arg = va_arg(args, uint64_t); + else arg = va_arg(args, uint32_t); + itoa(tmp, arg, 8, minSize, pad, 0); goto sprintf_puts; + + // Unsigned binary case 'b': - itoa(tmp, arg, 2, minSize, pad); + if(bLongLong) arg = va_arg(args, uint64_t); + else arg = va_arg(args, uint32_t); + itoa(tmp, arg, 2, minSize, pad, 0); goto sprintf_puts; + // String case 's': - p = (char*)arg; + arg = va_arg(args, uint32_t); + p = (void*)(intptr_t)arg; sprintf_puts: if(!p) p = "(null)"; - while(*p) len++, p++; + if(buf) { + while(*p) { + buf[pos++] = *p++; + } + } + else { + while(*p) { + pos++; p++; + } + } break; + // Unknown, just treat it as a character default: - len ++; + arg = va_arg(args, uint32_t); + if(buf) buf[pos] = arg; + pos ++; break; } } - return len; + if(buf) buf[pos] = '\0'; + + return pos; } const char cUCDIGITS[] = "0123456789ABCDEF"; /** - * \fn static void itoa(char *buf, unsigned long num, int base, int minLength, char pad) + * \fn static void itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned) * \brief Convert an integer into a character string */ -EXPORT void itoa(char *buf, unsigned long num, int base, int minLength, char pad) +EXPORT void itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned) { char tmpBuf[32]; int pos=0, i; @@ -445,17 +399,25 @@ EXPORT void itoa(char *buf, unsigned long num, int base, int minLength, char pad return; } + if(bSigned && (int64_t)num < 0) + { + num = -num; + bSigned = 1; + } else + bSigned = 0; + while(num > base-1) { tmpBuf[pos] = cUCDIGITS[ num % base ]; - num = (long) num / base; //Shift {number} right 1 digit + num = (long) num / base; // Shift {number} right 1 digit pos++; } - tmpBuf[pos++] = cUCDIGITS[ num % base ]; //Last digit of {number} + tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of {number} + if(bSigned) tmpBuf[pos++] = '-'; // Append sign symbol if needed i = 0; minLength -= pos; while(minLength-- > 0) buf[i++] = pad; - while(pos-- > 0) buf[i++] = tmpBuf[pos]; //Reverse the order of characters + while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters buf[i] = 0; } @@ -470,21 +432,26 @@ EXPORT int printf(const char *format, ...) char *buf; va_list args; + // Get final size va_start(args, format); - size = ssprintfv((char*)format, args); + size = vsprintf(NULL, (char*)format, args); va_end(args); + // Allocate buffer buf = (char*)malloc(size+1); buf[size] = '\0'; + // Fill Buffer va_start(args, format); - sprintfv(buf, (char*)format, args); + vsprintf(buf, (char*)format, args); va_end(args); - + // Send to stdout write(_stdout, size+1, buf); + // Free buffer free(buf); + // Return return size; #else @@ -504,9 +471,10 @@ EXPORT int printf(const char *format, ...) */ EXPORT int sprintf(char *buf, const char *format, ...) { + int ret; va_list args; va_start(args, format); - sprintfv((char*)buf, (char*)format, args); + ret = vsprintf((char*)buf, (char*)format, args); va_end(args); - return 1; + return ret; } diff --git a/Usermode/Libraries/libgcc.so_src/libgcc.c b/Usermode/Libraries/libgcc.so_src/libgcc.c index 2127b551..010cc35a 100644 --- a/Usermode/Libraries/libgcc.so_src/libgcc.c +++ b/Usermode/Libraries/libgcc.so_src/libgcc.c @@ -1,9 +1,8 @@ /* Acess GCC Helper Library * */ -#include - -typedef unsigned long long int uint64_t; +#include +#include // === CODE === int SoMain() @@ -14,7 +13,7 @@ int SoMain() // --- Errors --- void __stack_chk_fail() { - write(1, 32, "FATAL ERROR: Stack Check Failed\n"); + write(2, 32, "FATAL ERROR: Stack Check Failed\n"); _exit(-1); for(;;); } diff --git a/Usermode/include/stdint.h b/Usermode/include/stdint.h index 3ad94bda..ea837ba8 100644 --- a/Usermode/include/stdint.h +++ b/Usermode/include/stdint.h @@ -14,4 +14,6 @@ typedef signed short int16_t; typedef signed long int32_t; typedef signed long long int64_t; +typedef uint32_t intptr_t; + #endif diff --git a/Usermode/include/stdio.h b/Usermode/include/stdio.h index 2d7f31eb..78d1a59e 100644 --- a/Usermode/include/stdio.h +++ b/Usermode/include/stdio.h @@ -4,12 +4,13 @@ */ #ifndef __STDIO_H #define __STDIO_H + +#include typedef struct sFILE FILE; extern int printf(const char *format, ...); -extern void sprintfv(char *buf, const char *format, va_list args); -extern int ssprintfv(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 FILE *fopen(char *file, char *mode); @@ -23,6 +24,7 @@ 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; -- 2.20.1