Fixed correctness in stdio, minor changes in ls to handle dirs with negative sizes
authorJohn Hodge <[email protected]>
Sat, 3 Oct 2009 03:37:01 +0000 (11:37 +0800)
committerJohn Hodge <[email protected]>
Sat, 3 Oct 2009 03:37:01 +0000 (11:37 +0800)
Usermode/Applications/CLIShell_src/main.c
Usermode/Applications/ls_src/main.c
Usermode/Libraries/libc.so_src/Makefile
Usermode/Libraries/libc.so_src/env.c
Usermode/Libraries/libc.so_src/fileIO.c
Usermode/Libraries/libgcc.so_src/libgcc.c
Usermode/include/stdint.h
Usermode/include/stdio.h

index 914557a..2e51419 100644 (file)
@@ -469,10 +469,7 @@ void Command_Dir(int argc, char **argv)
        // Check if file opened\r
        if(dp == -1)\r
        {\r
-               //printf("Unable to open directory `%s', File cannot be found\n", tmpPath);\r
-               write(_stdout, 27, "Unable to open directory `");\r
-               write(_stdout, strlen(tmpPath)+1, tmpPath);\r
-               write(_stdout, 25, "', File cannot be found\n");\r
+               printf("Unable to open directory `%s', File cannot be found\n", tmpPath);\r
                return;\r
        }\r
        // Get File Stats\r
index fc99a40..3ae63fd 100644 (file)
@@ -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
 }
index c2979a0..32eb52b 100644 (file)
@@ -6,7 +6,7 @@
 CPPFLAGS += \r
 CFLAGS   += \r
 ASFLAGS  +=\r
-LDFLAGS  += -soname libc.so.1 -Map map.txt\r
+LDFLAGS  += -soname libc.so.1 -Map map.txt -lgcc\r
 \r
 OBJ_LIBC = heap.o stdlib.o stub.o env.o fileIO.o string.o\r
 # signals.o\r
index c0f8ea3..3dca4df 100644 (file)
@@ -1,8 +1,9 @@
 /*
-AcessOS C Library
-- Environment Handler
+ * Acess C Library
+ * - Environment Handler
 */
 #include <stdlib.h>
+#include <string.h>
 
 // === GLOBALS ===
 char **_envp = NULL;
index b8cdb11..23a760d 100644 (file)
@@ -1,6 +1,7 @@
 /*\r
-AcessOS Basic C Library\r
-*/\r
+ * AcessOS Basic C Library\r
+ * stdio.c\r
+ */\r
 #include "config.h"\r
 #include <acess/sys.h>\r
 #include <stdlib.h>\r
@@ -16,7 +17,7 @@ AcessOS Basic C Library
 #define        _stdout 1\r
 \r
 // === PROTOTYPES ===\r
-EXPORT void    itoa(char *buf, unsigned long num, int base, int minLength, char pad);\r
+EXPORT void    itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned);\r
 struct sFILE   *get_file_struct();\r
 \r
 // === GLOBALS ===\r
@@ -124,10 +125,10 @@ EXPORT void fflush(FILE *fp)
 }\r
 \r
 /**\r
- * \fn int fprintfv(FILE *fp, const char *format, va_list args)\r
+ * \fn EXPORT int vfprintf(FILE *fp, const char *format, va_list args)\r
  * \brief Print to a file from a variable argument list\r
  */\r
-EXPORT int fprintfv(FILE *fp, const char *format, va_list args)\r
+EXPORT int vfprintf(FILE *fp, const char *format, va_list args)\r
 {\r
        va_list tmpList = args;\r
         int    size;\r
@@ -135,13 +136,13 @@ EXPORT int fprintfv(FILE *fp, const char *format, va_list args)
         \r
        if(!fp || !format)      return -1;\r
        \r
-       size = ssprintfv((char*)format, tmpList);\r
+       size = vsprintf(NULL, (char*)format, tmpList);\r
        \r
        buf = (char*)malloc(size+1);\r
        buf[size] = '\0';\r
        \r
        // Print\r
-       sprintfv(buf, (char*)format, args);\r
+       vsprintf(buf, (char*)format, args);\r
        \r
        // Write to stream\r
        write(fp->FD, size+1, buf);\r
@@ -164,7 +165,7 @@ EXPORT int fprintf(FILE *fp, const char *format, ...)
        \r
        // Get Size\r
        va_start(args, format);\r
-       ret = fprintfv(fp, (char*)format, args);\r
+       ret = vfprintf(fp, (char*)format, args);\r
        va_end(args);\r
        \r
        return ret;\r
@@ -238,33 +239,38 @@ EXPORT int        puts(const char *str)
 \r
 //sprintfv\r
 /**\r
- \fn EXPORT void sprintfv(char *buf, const char *format, va_list args)\r
+ \fn EXPORT void vsprintf(char *buf, const char *format, va_list args)\r
  \brief Prints a formatted string to a buffer\r
  \param buf    Pointer - Destination Buffer\r
  \param format String - Format String\r
  \param args   VarArgs List - Arguments\r
 */\r
-EXPORT void sprintfv(char *buf, const char *format, va_list args)\r
+EXPORT int vsprintf(char *buf, const char *format, va_list args)\r
 {\r
        char    tmp[33];\r
-        int    c, arg, minSize;\r
+        int    c, minSize;\r
         int    pos = 0;\r
        char    *p;\r
        char    pad;\r
+       uint64_t        arg;\r
+        int    bLongLong;\r
 \r
        tmp[32] = '\0';\r
        \r
        while((c = *format++) != 0)\r
        {\r
-               //SysDebug("c = '%c'\n", c);\r
+               // Non-control character\r
                if (c != '%') {\r
-                       buf[pos++] = c;\r
+                       if(buf) buf[pos] = c;\r
+                       pos ++;\r
                        continue;\r
                }\r
                \r
+               // Control Character\r
                c = *format++;\r
-               if(c == '%') {\r
-                       buf[pos++] = '%';\r
+               if(c == '%') {  // Literal %\r
+                       if(buf) buf[pos] = '%';\r
+                       pos ++;\r
                        continue;\r
                }\r
                \r
@@ -285,156 +291,104 @@ EXPORT void sprintfv(char *buf, const char *format, va_list args)
                        }\r
                }\r
        \r
-               p = tmp;\r
-       \r
-               // Get Argument\r
-               arg = va_arg(args, int);\r
-               // Get Type\r
-               switch (c) {\r
-               case 'd':\r
-               case 'i':\r
-                       if(arg < 0) {\r
-                               buf[pos++] = '-';\r
-                               arg = -arg;\r
-                       }\r
-                       itoa(tmp, arg, 10, minSize, pad);\r
-                       goto sprintf_puts;\r
-               //      break;\r
-               case 'u':\r
-                       itoa(tmp, arg, 10, minSize, pad);\r
-                       goto sprintf_puts;\r
-               //      break;\r
-               case 'p':       // Pointer\r
-                       buf[pos++] = '*';\r
-                       buf[pos++] = '0';\r
-                       buf[pos++] = 'x';\r
-               case 'x':\r
-                       itoa(tmp, arg, 16, minSize, pad);\r
-                       goto sprintf_puts;\r
-               //      break;\r
-               case 'o':\r
-                       itoa(tmp, arg, 8, minSize, pad);\r
-                       goto sprintf_puts;\r
-               //      break;\r
-               case 'b':\r
-                       itoa(tmp, arg, 2, minSize, pad);\r
-                       goto sprintf_puts;\r
-               //      break;\r
-\r
-               case 's':\r
-                       p = (void*)arg;\r
-               sprintf_puts:\r
-                       if(!p)  p = "(null)";\r
-                       while(*p)       buf[pos++] = *p++;\r
-                       break;\r
-\r
-               default:\r
-                       buf[pos++] = arg;\r
-                       break;\r
-               }\r
-    }\r
-       buf[pos++] = '\0';\r
-}\r
-/*\r
-ssprintfv\r
-- Size, Stream, Print Formated, Variable Argument List\r
-*/\r
-/**\r
- \fn EXPORT int ssprintfv(char *format, va_list args)\r
- \brief Gets the total character count from a formatted string\r
- \param format String - Format String\r
- \param args   VarArgs - Argument List\r
-*/\r
-EXPORT int ssprintfv(char *format, va_list args)\r
-{\r
-       char    tmp[33];\r
-        int    c, arg, minSize;\r
-        int    len = 0;\r
-       char    *p;\r
-       char    pad;\r
-\r
-       tmp[32] = '\0';\r
-       \r
-       while((c = *format++) != 0)\r
-       {\r
-               if (c != '%') {\r
-                       len++;\r
-                       continue;\r
-               }\r
-               \r
-               c = *format++;\r
-               \r
-               // Literal '%'\r
-               if(c == '%') {\r
-                       len++;\r
-                       continue;\r
-               }\r
-               \r
-               // Padding\r
-               if(c == '0') {\r
-                       pad = '0';\r
-                       c = *format++;\r
-               } else\r
-                       pad = ' ';\r
-               minSize = 0;\r
-               if('1' <= c && c <= '9')\r
+               // Check for long long\r
+               bLongLong = 0;\r
+               if(c == 'l')\r
                {\r
-                       while('0' <= c && c <= '9')\r
-                       {\r
-                               minSize *= 10;\r
-                               minSize += c - '0';\r
-                               c = *format++;\r
+                       c = *format++;\r
+                       if(c == 'l') {\r
+                               bLongLong = 1;\r
                        }\r
                }\r
-               \r
+                       \r
                p = tmp;\r
-               arg = va_arg(args, int);\r
-               switch (c) {                    \r
-               case 'd':\r
-               case 'i':\r
-                       if(arg < 0) {\r
-                               len ++;\r
-                               arg = -arg;\r
-                       }\r
-                       itoa(tmp, arg, 10, minSize, pad);\r
+               \r
+               // Get Type\r
+               switch( c )\r
+               {\r
+               // Signed Integer\r
+               case 'd':       case 'i':\r
+                       // Get Argument\r
+                       if(bLongLong)   arg = va_arg(args, int64_t);\r
+                       else                    arg = va_arg(args, int32_t);\r
+                       itoa(tmp, arg, 10, minSize, pad, 1);\r
                        goto sprintf_puts;\r
+               \r
+               // Unsigned Integer\r
                case 'u':\r
-                       itoa(tmp, arg, 10, minSize, pad);\r
+                       // Get Argument\r
+                       if(bLongLong)   arg = va_arg(args, uint64_t);\r
+                       else                    arg = va_arg(args, uint32_t);\r
+                       itoa(tmp, arg, 10, minSize, pad, 0);\r
                        goto sprintf_puts;\r
-               case 'p':       // Pointer\r
-                       len += 3;\r
+               \r
+               // Pointer\r
+               case 'p':\r
+                       if(buf) {\r
+                               buf[pos] = '*';\r
+                               buf[pos+1] = '0';\r
+                               buf[pos+2] = 'x';\r
+                       }\r
+                       pos += 3;\r
+                       // Fall through to hex\r
+               // Unsigned Hexadecimal\r
                case 'x':\r
-                       itoa(tmp, arg, 16, minSize, pad);\r
+                       if(bLongLong)   arg = va_arg(args, uint64_t);\r
+                       else                    arg = va_arg(args, uint32_t);\r
+                       itoa(tmp, arg, 16, minSize, pad, 0);\r
                        goto sprintf_puts;\r
+               \r
+               // Unsigned Octal\r
                case 'o':\r
-                       itoa(tmp, arg, 8, minSize, pad);\r
-                       p = tmp;\r
+                       if(bLongLong)   arg = va_arg(args, uint64_t);\r
+                       else                    arg = va_arg(args, uint32_t);\r
+                       itoa(tmp, arg, 8, minSize, pad, 0);\r
                        goto sprintf_puts;\r
+               \r
+               // Unsigned binary\r
                case 'b':\r
-                       itoa(tmp, arg, 2, minSize, pad);\r
+                       if(bLongLong)   arg = va_arg(args, uint64_t);\r
+                       else                    arg = va_arg(args, uint32_t);\r
+                       itoa(tmp, arg, 2, minSize, pad, 0);\r
                        goto sprintf_puts;\r
 \r
+               // String\r
                case 's':\r
-                       p = (char*)arg;\r
+                       arg = va_arg(args, uint32_t);\r
+                       p = (void*)(intptr_t)arg;\r
                sprintf_puts:\r
                        if(!p)  p = "(null)";\r
-                       while(*p)       len++, p++;\r
+                       if(buf) {\r
+                               while(*p) {\r
+                                       buf[pos++] = *p++;\r
+                               }\r
+                       }\r
+                       else {\r
+                               while(*p) {\r
+                                       pos++; p++;\r
+                               }\r
+                       }\r
                        break;\r
 \r
+               // Unknown, just treat it as a character\r
                default:\r
-                       len ++;\r
+                       arg = va_arg(args, uint32_t);\r
+                       if(buf) buf[pos] = arg;\r
+                       pos ++;\r
                        break;\r
                }\r
     }\r
-       return len;\r
+       if(buf) buf[pos] = '\0';\r
+       \r
+       return pos;\r
 }\r
 \r
 const char cUCDIGITS[] = "0123456789ABCDEF";\r
 /**\r
- * \fn static void itoa(char *buf, unsigned long num, int base, int minLength, char pad)\r
+ * \fn static void itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned)\r
  * \brief Convert an integer into a character string\r
  */\r
-EXPORT void itoa(char *buf, unsigned long num, int base, int minLength, char pad)\r
+EXPORT void itoa(char *buf, uint64_t num, int base, int minLength, char pad, int bSigned)\r
 {\r
        char    tmpBuf[32];\r
         int    pos=0, i;\r
@@ -445,17 +399,25 @@ EXPORT void itoa(char *buf, unsigned long num, int base, int minLength, char pad
                return;\r
        }\r
        \r
+       if(bSigned && (int64_t)num < 0)\r
+       {\r
+               num = -num;\r
+               bSigned = 1;\r
+       } else\r
+               bSigned = 0;\r
+       \r
        while(num > base-1) {\r
                tmpBuf[pos] = cUCDIGITS[ num % base ];\r
-               num = (long) num / base;                //Shift {number} right 1 digit\r
+               num = (long) num / base;                // Shift {number} right 1 digit\r
                pos++;\r
        }\r
 \r
-       tmpBuf[pos++] = cUCDIGITS[ num % base ];                //Last digit of {number}\r
+       tmpBuf[pos++] = cUCDIGITS[ num % base ];                // Last digit of {number}\r
+       if(bSigned)     tmpBuf[pos++] = '-';    // Append sign symbol if needed\r
        i = 0;\r
        minLength -= pos;\r
        while(minLength-- > 0)  buf[i++] = pad;\r
-       while(pos-- > 0)                buf[i++] = tmpBuf[pos]; //Reverse the order of characters\r
+       while(pos-- > 0)                buf[i++] = tmpBuf[pos]; // Reverse the order of characters\r
        buf[i] = 0;\r
 }\r
 \r
@@ -470,21 +432,26 @@ EXPORT int printf(const char *format, ...)
        char    *buf;\r
        va_list args;\r
        \r
+       // Get final size\r
        va_start(args, format);\r
-       size = ssprintfv((char*)format, args);\r
+       size = vsprintf(NULL, (char*)format, args);\r
        va_end(args);\r
        \r
+       // Allocate buffer\r
        buf = (char*)malloc(size+1);\r
        buf[size] = '\0';\r
        \r
+       // Fill Buffer\r
        va_start(args, format);\r
-       sprintfv(buf, (char*)format, args);\r
+       vsprintf(buf, (char*)format, args);\r
        va_end(args);\r
        \r
-       \r
+       // Send to stdout\r
        write(_stdout, size+1, buf);\r
        \r
+       // Free buffer\r
        free(buf);\r
+       // Return\r
        return size;\r
        \r
        #else\r
@@ -504,9 +471,10 @@ EXPORT int printf(const char *format, ...)
  */\r
 EXPORT int sprintf(char *buf, const char *format, ...)\r
 {\r
+        int    ret;\r
        va_list args;\r
        va_start(args, format);\r
-       sprintfv((char*)buf, (char*)format, args);\r
+       ret = vsprintf((char*)buf, (char*)format, args);\r
        va_end(args);\r
-       return 1;\r
+       return ret;\r
 }\r
index 2127b55..010cc35 100644 (file)
@@ -1,9 +1,8 @@
 /* Acess GCC Helper Library
  *
  */
-#include <sys/sys.h>
-
-typedef unsigned long long int uint64_t;
+#include <acess/sys.h>
+#include <stdint.h>
 
 // === 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(;;);
 }
index 3ad94bd..ea837ba 100644 (file)
@@ -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
index 2d7f31e..78d1a59 100644 (file)
@@ -4,12 +4,13 @@
  */\r
 #ifndef __STDIO_H\r
 #define __STDIO_H
+\r
+#include <stdarg.h>\r
 
 typedef struct sFILE   FILE;
 \r
 extern int     printf(const char *format, ...);\r
-extern void sprintfv(char *buf, const char *format, va_list args);\r
-extern int     ssprintfv(char *format, va_list args);\r
+extern int     vsprintf(char *buf, const char *format, va_list args);\r
 extern int     sprintf(char *buf, const char *format, ...);
 \r
 extern FILE    *fopen(char *file, char *mode);\r
@@ -23,6 +24,7 @@ extern int    fgetc(FILE *fp);
 extern int     fputc(int ch, FILE *fp);\r
 \r
 extern int     fprintf(FILE *fp, const char *format, ...);\r
+extern int     vfprintf(FILE *fp, const char *format, va_list args);\r
 \r
 extern FILE    *stdin;\r
 extern FILE    *stdout;\r

UCC git Repository :: git.ucc.asn.au