SysInfo.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "memory/Allocator.h"
  16. #include "util/SysInfo.h"
  17. #include "util/CString.h"
  18. #include "util/Bits.h"
  19. #include "util/Defined.h"
  20. #include <stdio.h>
  21. struct SysInfo SysInfo_detect(void)
  22. {
  23. struct SysInfo out = { .os = 0 };
  24. if (Defined(linux)) {
  25. out.os = SysInfo_Os_LINUX;
  26. } else if (Defined(sunos)) {
  27. out.os = SysInfo_Os_SUNOS;
  28. } else if (Defined(darwin)) {
  29. out.os = SysInfo_Os_DARWIN;
  30. } else if (Defined(freebsd)) {
  31. out.os = SysInfo_Os_FREEBSD;
  32. } else if (Defined(win32)) {
  33. out.os = SysInfo_Os_WIN32;
  34. } else {
  35. out.os = SysInfo_Os_UNKNOWN;
  36. }
  37. return out;
  38. }
  39. static char* getName(enum SysInfo_Os os)
  40. {
  41. switch (os) {
  42. case SysInfo_Os_LINUX: return "linux";
  43. case SysInfo_Os_SUNOS: return "sunos";
  44. case SysInfo_Os_DARWIN: return "darwin";
  45. case SysInfo_Os_FREEBSD: return "freebsd";
  46. case SysInfo_Os_WIN32: return "win32";
  47. default: return "os_unknown";
  48. }
  49. }
  50. #define BUFF_SZ 1024
  51. char* SysInfo_describe(struct SysInfo si, struct Allocator* alloc)
  52. {
  53. uint8_t buff[BUFF_SZ];
  54. snprintf(buff, BUFF_SZ, "%s",
  55. getName(si.os));
  56. int len = CString_strlen(buff)+1;
  57. char* out = Allocator_malloc(alloc, len);
  58. Bits_memcpy(out, buff, len);
  59. return out;
  60. }