Updated the build files to use $(xCP), allows use of mcopy to a mtools disk
[tpg/acess2.git] / Kernel / lib.c
index d3fff54..f614965 100644 (file)
@@ -56,7 +56,7 @@ void itoa(char *buf, Uint num, int base, int minLength, char pad)
 }
 
 /**
- * \fn int tolower(int __c)
+ * \fn int tolower(int c)
  * \brief Converts a character to lower case
  */
 int tolower(int c)
@@ -78,7 +78,7 @@ int strucmp(const char *Str1, const char *Str2)
 }
 
 /**
- * \fn int strpos(char *Str, char Ch)
+ * \fn int strpos(const char *Str, char Ch)
  * \brief Search a string for an ascii character
  */
 int strpos(const char *Str, char Ch)
@@ -92,6 +92,8 @@ int strpos(const char *Str, char Ch)
 }
 
 /**
+ * \fn int ByteSum(void *Ptr, int Size)
+ * \brief Adds the bytes in a memory region and returns the sum
  */
 int ByteSum(void *Ptr, int Size)
 {
@@ -101,7 +103,7 @@ int ByteSum(void *Ptr, int Size)
 }
 
 /**
- * \fn Uint strlen(char *__str)
+ * \fn Uint strlen(const char *__str)
  * \brief Get the length of string
  */
 Uint strlen(const char *__str)
@@ -112,7 +114,7 @@ Uint strlen(const char *__str)
 }
 
 /**
- * \fn char *strcpy(char *__str1, char *__str2)
+ * \fn char *strcpy(const char *__str1, const char *__str2)
  * \brief Copy a string to a new location
  */
 char *strcpy(char *__str1, const char *__str2)
@@ -124,7 +126,7 @@ char *strcpy(char *__str1, const char *__str2)
 }
 
 /**
- * \fn int strcmp(char *str1, char *str2)
+ * \fn int strcmp(const char *str1, const char *str2)
  * \brief Compare two strings return the difference between
  *        the first non-matching characters.
  */
@@ -136,7 +138,7 @@ int strcmp(const char *str1, const char *str2)
 }
 
 /**
- * \fn int strncmp(char *Str1, char *Str2, size_t num)
+ * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
  * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
  */
 int strncmp(const char *Str1, const char *Str2, size_t num)
@@ -148,14 +150,14 @@ int strncmp(const char *Str1, const char *Str2, size_t num)
 }
 
 /**
- * \fn char *strdup(char *str)
+ * \fn char *strdup(const char *Str)
  * \brief Duplicates a string
  */
-char *strdup(const char *str)
+char *strdup(const char *Str)
 {
        char    *ret;
-       ret = malloc(strlen(str)+1);
-       strcpy(ret, str);
+       ret = malloc(strlen(Str)+1);
+       strcpy(ret, Str);
        return ret;
 }
 
@@ -171,7 +173,7 @@ int DivUp(int num, int dem)
 }
 
 /**
- * \fn int strpos8(char *str, Uint32 search)
+ * \fn int strpos8(const char *str, Uint32 search)
  * \brief Search a string for a UTF-8 character
  */
 int strpos8(const char *str, Uint32 Search)
@@ -339,5 +341,95 @@ Uint rand()
        return giRandomState;
 }
 
+/// \name Memory Validation
+/// \{
+/**
+ * \brief Checks if a string resides fully in valid memory
+ */
+int CheckString(char *String)
+{
+       // Check 1st page
+       if( MM_IsUser( (tVAddr)String ) )
+       {
+               // Traverse String
+               while( *String )
+               {
+                       if( !MM_IsUser( (tVAddr)String ) )
+                               return 0;
+                       // Increment string pointer
+                       String ++;
+               }
+               return 1;
+       }
+       else if( MM_GetPhysAddr( (tVAddr)String ) )
+       {
+               // Traverse String
+               while( *String )
+               {
+                       if( !MM_GetPhysAddr( (tVAddr)String ) )
+                               return 0;
+                       // Increment string pointer
+                       String ++;
+               }
+               return 1;
+       }
+       return 0;
+}
+
+/**
+ * \brief Check if a sized memory region is valid memory
+ */
+int CheckMem(void *Mem, int NumBytes)
+{
+       tVAddr  addr = (tVAddr)Mem;
+       
+       if( MM_IsUser( addr ) )
+       {
+               while( NumBytes-- )
+               {
+                       if( !MM_IsUser( addr ) )
+                               return 0;
+                       addr ++;
+               }
+               return 1;
+       }
+       else if( MM_GetPhysAddr( addr ) )
+       {
+               while( NumBytes-- )
+               {
+                       if( !MM_GetPhysAddr( addr ) )
+                               return 0;
+                       addr ++;
+               }
+               return 1;
+       }
+       return 0;
+}
+/// \}
+
+/**
+ * \brief Search a string array for \a Needle
+ * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
+ */
+int LookupString(char **Array, char *Needle)
+{
+        int    i;
+       for( i = 0; Array[i]; i++ )
+       {
+               if(strcmp(Array[i], Needle) == 0)       return i;
+       }
+       return -1;
+}
+
+EXPORT(strlen);
+EXPORT(strdup);
+EXPORT(strcmp);
+EXPORT(strncmp);
+EXPORT(strcpy);
+//EXPORT(strncpy);
+
 EXPORT(timestamp);
 EXPORT(ReadUTF8);
+EXPORT(CheckMem);
+EXPORT(CheckString);
+EXPORT(LookupString);

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