// 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;
}
// Apply Defaults
if(!gsDirectory) gsDirectory = ".";
-
- printf("gsDirectory = '%s'\n", gsDirectory);
}
/**
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
}
/*\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
#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
}\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
\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
\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
\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
}\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
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
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
*/\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