| 1 | #include "os.h" |
| 2 | #include "common.h" |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | |
| 7 | const char *display_os() { |
| 8 | const char *osname = NULL; |
| 9 | const char *osversion = NULL; |
| 10 | const char *osarch = NULL; |
| 11 | char *cmd; |
| 12 | |
| 13 | #if defined(__APPLE__) |
| 14 | if (!from_cache("/tmp/farfetch/os")) { |
| 15 | osname = run_command_s("sw_vers | grep \"ProductName\" | awk '{print $2}'"); |
| 16 | osversion = run_command_s("sw_vers | grep \"ProductVersion\" | awk '{print $2}'"); |
| 17 | osarch = run_command_s("uname -m"); |
| 18 | |
| 19 | cmd = (char *)malloc(128 * sizeof(char)); |
| 20 | if (!cmd) { |
| 21 | perror("malloc"); |
| 22 | free((void *)osname); |
| 23 | free((void *)osversion); |
| 24 | free((void *)osarch); |
| 25 | return NULL; |
| 26 | } |
| 27 | snprintf(cmd, 128, "%s %s %s", osname, osversion, osarch); |
| 28 | |
| 29 | to_cache("/tmp/farfetch/os", cmd); |
| 30 | } else { |
| 31 | cmd = (char *)from_cache("/tmp/farfetch/os"); |
| 32 | } |
| 33 | #elif defined(__HAIKU__) |
| 34 | osname = run_command_s("uname -s"); |
| 35 | osversion = run_command_s("uname -r"); |
| 36 | osarch = run_command_s("uname -m"); |
| 37 | |
| 38 | cmd = (char *)malloc(128 * sizeof(char)); |
| 39 | if (!cmd) { |
| 40 | perror("malloc"); |
| 41 | free((void *)osname); |
| 42 | free((void *)osversion); |
| 43 | free((void *)osarch); |
| 44 | return NULL; |
| 45 | } |
| 46 | snprintf(cmd, 128, "%s %s %s", osname, osversion, osarch); |
| 47 | #else |
| 48 | if (!from_cache("/tmp/farfetch/os")) { |
| 49 | osname = run_command_s("uname -s"); |
| 50 | osversion = run_command_s("uname -r"); |
| 51 | osarch = run_command_s("uname -m"); |
| 52 | |
| 53 | cmd = (char *)malloc(128 * sizeof(char)); |
| 54 | if (!cmd) { |
| 55 | perror("malloc"); |
| 56 | free((void *)osname); |
| 57 | free((void *)osversion); |
| 58 | free((void *)osarch); |
| 59 | return NULL; |
| 60 | } |
| 61 | snprintf(cmd, 128, "%s %s %s", osname, osversion, osarch); |
| 62 | |
| 63 | to_cache("/tmp/farfetch/os", cmd); |
| 64 | } else { |
| 65 | cmd = (char *)from_cache("/tmp/farfetch/os"); |
| 66 | } |
| 67 | #endif |
| 68 | |
| 69 | free((void *)osname); |
| 70 | free((void *)osversion); |
| 71 | free((void *)osarch); |
| 72 | |
| 73 | return cmd; |
| 74 | } |