/src/common.c

https://github.com/Interfere/gOWNOS · C · 78 lines · 53 code · 13 blank · 12 comment · 11 complexity · 095d57c4816cc3b07b59d31ba4f469fc MD5 · raw file

  1. // common.c -- Defines some global functions
  2. #include "common.h"
  3. // write a byte out to the specified port
  4. void outb(u16int port, u8int value)
  5. {
  6. __asm__ volatile ("outb %1, %0" : : "dN" (port), "a" (value));
  7. }
  8. u8int inb(u16int port)
  9. {
  10. u8int ret;
  11. __asm__ volatile ("inb %1, %0" : "=a" (ret) : "dN" (port));
  12. return ret;
  13. }
  14. u16int inw(u16int port)
  15. {
  16. u16int ret;
  17. __asm__ volatile ("inw %1, %0" : "=a" (ret) : "dN" (port));
  18. return ret;
  19. }
  20. // Copy len bytes from src to dest.
  21. void memcpy(void *dest, const void *src, u32int len)
  22. {
  23. const u8int *sp = (const u8int *)src;
  24. u8int *dp = (u8int *)dest;
  25. while(len--)
  26. *dp++ = *sp++;
  27. }
  28. // Write len copies of val into dest.
  29. void memset(void *dest, u8int val, u32int len)
  30. {
  31. u8int *temp = (u8int *)dest;
  32. while (len--)
  33. *temp++ = val;
  34. }
  35. // Returns an integral value indicating the relationship between the strings:
  36. // A zero value indicates that both strings are equal.
  37. // A value greater than zero indicates that the first character that does not
  38. // match has a greater value in str1 than in str2; And a value less than zero indicates the opposite.
  39. int strcmp(const char *str1, const char *str2)
  40. {
  41. register signed char __res;
  42. while(1) {
  43. if((__res = *str1 - *str2++) != 0 || *str1++)
  44. break;
  45. }
  46. return __res;
  47. }
  48. // Copy the NULL-terminated string src into dest, and
  49. // return dest.
  50. char *strcpy(char *dest, const char *src)
  51. {
  52. char* tmp = dest;
  53. while((*dest++ = *src++) != '\0');
  54. return tmp;
  55. }
  56. // Concatenate the NULL-terminated string src onto
  57. // the end of dest, and return dest.
  58. char *strcat(char *dest, const char *src)
  59. {
  60. char* tmp = dest;
  61. while(*dest)
  62. dest++;
  63. while((*dest++ = *src++) != '\0');
  64. return tmp;
  65. }