unixwz0r

dinfo.c

Feb 11th, 2016
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.59 KB | None | 0 0
  1. /*
  2.  *
  3.  * Darwin Info
  4.  * Created by RAZ0REDGE
  5.  * Learning from yrmt info.c thanx for the help with this open source code.
  6.  *
  7.  */
  8.  #include <stdio.h>
  9.  #include <stdlib.h>
  10.  #include <string.h>
  11.  #include <stdbool.h>
  12.  #include <getopt.h>
  13.  #include <pwd.h>
  14.  #include <time.h>
  15.  #include <sys/sysctl.h>
  16.  #include <sys/statvfs.h>
  17.  #include <mach/mach.h>
  18.  #include <CoreFoundation/CoreFoundation.h>
  19.  #include <IOKit/IOKitlib.h>
  20.  
  21.  #define C0 "\x1B[0m"     //Reset
  22.  #define C1 "\x1b[0;32m"  //Green
  23.  #define C2 "\x1b[0;31m"  //Red
  24.  #define GRN C1
  25.  #define NOR C0
  26.  
  27.  static const struct {
  28.     const char *ctls;
  29.     const char *names;
  30. } values[] = {
  31.     { "hw.model", "Model" },
  32.     { "machdep.cpu.brand_string", "Processor" },
  33.     { "hw.memsize", "Memory" },
  34.     { "kern.ostype", "OS" },
  35.     { "kern.osrelease", "Kernel" },
  36.     { "kern.hostname", "Hostname" },
  37. };
  38.  
  39. static void sysctls(int i);
  40. static void disk(void);
  41. static void uptime(time_t *nowp);
  42. static void gpu(void);
  43. static void mem(void);
  44. static void curtime(void);
  45.  
  46. static void print_apple(void) { // The Darwin UNIX is done by me.  I find OS X UNIX is what makes OS X great so express Hexley
  47. time_t now;
  48. time(&now);                                          
  49. printf(C2"                   N                                                     \n");      
  50. printf(C2"                  $7                                                     \n");      
  51. printf(C2"              ?   N7N                          Z                         \n");      
  52. printf(C2"             NN   $77N   7Z~                 ?777                        \n");      
  53. printf(C2"           =778  D777$   777N          INNNNNN$77N                       \n");        
  54. printf(C2"          N777$  N777ON  $77Z8      7Z7777777777Z87777777$N              \n");      
  55. printf(C2"           N7N8=   Z7    7 87N      $777DZ$OD$77777N77777777D            \n");        
  56. printf(C2"           7Z      878      77N     7777777$MMMND$777Z77777777N          \n");        
  57. printf(C2"          D7N      D7N      N77=    Z777M88OOOOOOOOD77N77777777$         \n");      
  58. printf(C2"          N7N      N7N      ?77N    ~77MOO8OOOOOOOOOO$7Z77777777Z        \n");      
  59. printf(C2"          87N      N7Z       $7O     7NM~OOOOOOOOOOMOO77N77777777N       \n");      
  60. printf(C2"          87N      N77      Z77D      M  OOOOOOMMDOOMOD77Z7777$NO8NNZ7D  \n");      
  61. printf(C2"          N77N     N77=     $77D        OOOOOM    :O8OO777777D77777777N  \n");      
  62. printf(C2"           777$N   N77N  DD777N     MM ~8OOO,      DOON777777O7777777$   \n");      
  63. printf(C2"            O777777777777777O$   NDOOOMMOOOM       DOO8777777N777777D    \n");      
  64. printf(C2"              N$7777777778N  :88 OOOOOOOOOO$    MMMOOO77777777DD77Z8     \n");      
  65. printf(C2"                    $7D       888MND888888OD    MMMOOD77777777777N       \n");      
  66. printf(C2"                    O7Z        ZM8888888888O    MNOOOD77777777777N       \n");          
  67. printf(C2"                    N77          O888888M888DOOOOOOOO77777777777N        ");curtime();          
  68. printf(C2"                   N77+           888888888888888MON777777777NI          ");uptime(&now);          
  69. printf(C2"                    N77D            ~D8888888888MM8ON777777ND            \n");      
  70. printf(C2"                    877N             8$8MNDND+$8888O8777$N               \n");      
  71. printf(C2"                    ?77N            Z8$$$$N7788MOOODZ777$      ████████▄     ▄████████    ▄████████  ▄█     █▄   ▄█  ███▄▄▄▄        ███    █▄  ███▄▄▄▄    ▄█  ▀████    ▐████▀ \n");
  72. printf(C2"                   :O77NN           M8$$$$$8MOOD    $7777      ███   ▀███   ███    ███   ███    ███ ███     ███ ███  ███▀▀▀██▄      ███    ███ ███▀▀▀██▄ ███    ███▌   ████▀  \n");              
  73. printf(C2"                   MOOMOOOO         NO8888M~OO8$    Z7777N     ███    ███   ███    ███   ███    ███ ███     ███ ███▌ ███   ███      ███    ███ ███   ███ ███▌    ███  ▐███    \n");            
  74. printf(C2"                   8OOMZOOOD     MOOO~~~~~~OOOOOO+  D77777O    ███    ███   ███    ███  ▄███▄▄▄▄██▀ ███     ███ ███▌ ███   ███      ███    ███ ███   ███ ███▌    ▀███▄███▀    \n");            
  75. printf(C2"                    N877OOOOOMMOOOOO~~~~~~~OOOOOOOO.N777777:   ███    ███ ▀███████████ ▀▀███▀▀▀▀▀   ███     ███ ███▌ ███   ███      ███    ███ ███   ███ ███▌    ████▀██      \n");          
  76. printf(C2"                    8OO78OOOOOOOOOO~~~~~~~~OOOOOOOOO?DN8N$     ███    ███   ███    ███ ▀███████████ ███     ███ ███  ███   ███      ███    ███ ███   ███ ███    ▐███  ▀███    \n");            
  77. printf(C2"                     N77OOOOOOOOOO$~~~~~~~~OOOOOOOOOOM         ███   ▄███   ███    ███   ███    ███ ███ ▄█▄ ███ ███  ███   ███      ███    ███ ███   ███ ███   ▄███     ███▄  \n");              
  78. printf(C2"                     877N$OOOOOOON~~~~~~~~~OOO8OOOOOOOM        ████████▀    ███    █▀    ███    ███  ▀███▀███▀  █▀    ▀█   █▀       ████████▀   ▀█   █▀  █▀   ████       ███▄ \n");                
  79. printf(C2"                     +77N DOOOOOO~~~~~~~~~~~OOONOOOOOOO8                \n");                                                                                                  
  80. printf(C2"                      77D  .D8M8$~~~~~~~~~~~OOOOMOMOOOOM                \n");          
  81. printf(C2"                      77Z       ~~~~~~~~~~~~ZOOOOOOOOOO                 ");sysctls(0);          
  82. printf(C2"                      $77      .~~~~~~~~~~~~~OOOOONOOM                  ");sysctls(3);          
  83. printf(C2"                      Z77       ~~~~~~~~~~~~~OOOOOO$                    ");sysctls(4);          
  84. printf(C2"                      877:      ~~~~~~~~~~~~~?OOOOOO                    ");sysctls(5);          
  85. printf(C2"                      N77Z      M~~~~~~~~~~~~~OOOOOOO                   ");mem();          
  86. printf(C2"                      N77N     DO=~~~~~~~~~~~IOOOOOOO                   ");sysctls(1);          
  87. printf(C2"                      N77N    DOOO=~~~~~~~~~DOOOOOOOO                   ");gpu();          
  88. printf(C2"                      Z77D   MOOOOOD~~~~~~=OOOOOOOOON      MDOODMI      ");disk();        
  89. printf(C2"                      :77Z   MOOOOOOOD~~~MOOOOOOOOODO MOOOOOOOOOOOO     \n");          
  90. printf(C2"                       $77.  DOM8OOOOOOOMOOOOOOOOO8OOMOOOOOOOOO8OO7     \n");          
  91. printf(C2"                       Z778888MMOOOOOOOO8OOOOOOOOONOOOOOOOOOOOON        \n");          
  92. printf(C2"                       D77N8M8888OOOMD    ~MOOO8OOO     :               \n");          
  93. printf(C2"                       N7$OI88NN        M88NMDOODOO                     \n");          
  94. printf(C2"                                       88M888D888D                      \n");          
  95. printf(C2"                                         NMO M888                       \n");          
  96. }
  97. static void curtime(void) {
  98.     time_t t;
  99.     t =time(NULL);
  100.     printf(GRN"Time      :: "NOR"%s", ctime(&t));
  101. }
  102. static void mem(void) {
  103.     size_t len;
  104.     mach_port_t myHost;
  105.     vm_statistics64_data_t vm_stat;
  106.     vm_size_t pageSize = 4096; /* set to 4k default */
  107.     unsigned int  count = HOST_VM_INFO64_COUNT;
  108.     kern_return_t ret;
  109.     myHost = mach_host_self();
  110.     uint64_t value64;
  111.     len = sizeof(value64);
  112.     sysctlbyname(values[2].ctls, &value64, &len, NULL, 0);
  113.     if(host_page_size(mach_host_self(), &pageSize) == KERN_SUCCESS) {
  114.         if((ret = host_statistics64(myHost, HOST_VM_INFO64, (host_info64_t)&
  115.                         vm_stat, &count) == KERN_SUCCESS)) {
  116.             printf(GRN"%s    :: "NOR"%llu MB of %.f MB\n",
  117.                     values[2].names,
  118.                     (uint64_t)(vm_stat.active_count +
  119.                         vm_stat.inactive_count +
  120.                         vm_stat.wire_count)*pageSize >> 20,
  121.                     value64 / 1e+06);
  122.         }
  123.     }
  124. }      
  125. static void gpu(void) { // Thank you yrmt -- quite complex for just showing your gpu status ;)
  126.     io_iterator_t Iterator;
  127.     kern_return_t err = IOServiceGetMatchingServices(kIOMasterPortDefault,
  128.             IOServiceMatching("IOPCIDevice"),
  129.             &Iterator);
  130.     if(err != KERN_SUCCESS) {
  131.         fprintf(stderr, "IOServiceGetMatchingServices failed: %u\n", err);
  132.     }
  133.     for(io_service_t Device; IOIteratorIsValid(Iterator) &&
  134.             (Device = IOIteratorNext(Iterator)); IOObjectRelease(Device)) {
  135.         CFStringRef Name = IORegistryEntrySearchCFProperty(Device,
  136.                 kIOServicePlane,
  137.                 CFSTR("IOName"),
  138.                 kCFAllocatorDefault,
  139.                 kNilOptions);
  140.         if(Name) {
  141.             if(CFStringCompare(Name,CFSTR("display"), 0) == kCFCompareEqualTo) {
  142.                 CFDataRef Model = IORegistryEntrySearchCFProperty(Device,
  143.                         kIOServicePlane,
  144.                         CFSTR("model"),
  145.                         kCFAllocatorDefault,
  146.                         kNilOptions);
  147.                 if(Model) {
  148.                     bool ValueInBytes = true;
  149.                     CFTypeRef VRAM = IORegistryEntrySearchCFProperty(Device,
  150.                             kIOServicePlane,
  151.                             CFSTR("VRAM,totalsize"),
  152.                             kCFAllocatorDefault,
  153.                             kIORegistryIterateRecursively);
  154.                     if(!VRAM) {
  155.                         ValueInBytes = false;
  156.                         VRAM = IORegistryEntrySearchCFProperty(Device,
  157.                                 kIOServicePlane,
  158.                                 CFSTR("VRAM,totalMB"),
  159.                                 kCFAllocatorDefault,
  160.                                 kIORegistryIterateRecursively);
  161.                     }
  162.                     if(VRAM) {
  163.                         mach_vm_size_t Size = 0;
  164.                         CFTypeID Type = CFGetTypeID(VRAM);
  165.                         if(Type==CFDataGetTypeID())
  166.                             Size=(CFDataGetLength(VRAM) == sizeof(uint32_t) ?
  167.                                     (mach_vm_size_t)*(const uint32_t*)CFDataGetBytePtr(VRAM):
  168.                                     *(const uint64_t*)CFDataGetBytePtr(VRAM));
  169.                         else if(Type == CFNumberGetTypeID())
  170.                             CFNumberGetValue(VRAM,
  171.                                     kCFNumberSInt64Type, &Size);
  172.                         if(ValueInBytes) Size >>= 20;
  173.                         printf(GRN"Graphics  :: "NOR"%s @ %llu MB\n",
  174.                                 CFDataGetBytePtr(Model),Size);
  175.                         CFRelease(Model);
  176.                     }
  177.                         else printf(GRN"Graphic   :: "NOR"%s @ I Really don't know the VRAM Size of the GPU eh\n",
  178.                             CFDataGetBytePtr(Model));
  179.                         CFRelease(Model);
  180.                     }
  181.                 }
  182.                 CFRelease(Name);
  183.             }
  184.          }  
  185.     }
  186.     void help(void) {
  187.         printf("╔════════════════════════════════════════════════╗\n"
  188.                "║ Darwin Info C Program By: RAZ0REDGE June. 2014 ║\n"
  189.                "╚════════════════════════════════════════════════╝\n"
  190.                "-a shows the epic Darwin/BSD UNIX Logo\n"
  191.                "-h shows this help msg which isn't any help haha\n");
  192.         exit(0);
  193.     }
  194.     static void sysctls(int i) {
  195.         size_t len;
  196.         if(i==2) {
  197.             mem();
  198.         } else {
  199.             sysctlbyname(values[i].ctls, NULL, &len, NULL, 0);
  200.             char *type = malloc(len);
  201.             sysctlbyname(values[i].ctls, type, &len, NULL, 0);
  202.             printf(GRN"%-10s:: "NOR"%s\n", values[i].names, type);
  203.             free(type);
  204.         }
  205.     }
  206.     static void disk(void) {
  207.         struct statvfs info;
  208.         if(!statvfs("/", &info)) {
  209.             unsigned long left = (info.f_bavail * info.f_frsize);
  210.             unsigned long total = (info.f_files * info.f_frsize);
  211.             unsigned long used = total - left;
  212.             float perc  = (float)used / (float)total;
  213.             printf(GRN"Disk      :: "NOR"%.2f%% of %.2f GB\n",
  214.                 perc * 100, (float)total / 1e+09);
  215.         }
  216.     }  
  217.     static void uptime(time_t *nowp) {
  218.         struct timeval boottime;
  219.         time_t uptime;
  220.         int days, hrs, mins, secs;
  221.         int mib[2];
  222.         size_t size;
  223.         char buf[256];
  224.         if(strftime(buf, sizeof(buf), NULL, localtime(nowp)))
  225.             mib[0] = CTL_KERN;
  226.         mib[1] = KERN_BOOTTIME;
  227.         size = sizeof(boottime);
  228.         if(sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
  229.                 boottime.tv_sec) {
  230.             uptime = *nowp - boottime.tv_sec;
  231.             if(uptime > 60)
  232.                 uptime += 30;
  233.             days = (int)uptime / 86400;
  234.             uptime %= 86400;
  235.             hrs = (int)uptime / 3600;
  236.             uptime %= 3600;
  237.             mins = (int)uptime / 60;
  238.             secs = uptime % 60;
  239.             printf(GRN"Uptime    :: "NOR);
  240.             if(days > 0)
  241.                 printf("%d day%s", days, days > 1? "s " : " ");
  242.             if (hrs > 0 && mins > 0)
  243.                 printf("%02d:%02d", hrs, mins);
  244.             else if(hrs == 0 && mins > 0)
  245.                 printf("0:%02d", mins);
  246.             else
  247.                 printf("0:00");
  248.             putchar('\n');
  249.         }
  250.     }
  251.     int main(int argc, char **argv) {
  252.         bool print_with_apple = false;
  253.         if(argc >= 2) {
  254.             int c;
  255.             while((c = getopt(argc, argv, "ha")) != -1) {
  256.                 switch(c) {
  257.                     case 'a':
  258.                         print_with_apple = true;
  259.                         break;
  260.                      case 'h':
  261.                      default:
  262.                         help();
  263.                         break;
  264.                 }
  265.             }
  266.         }
  267.         if(print_with_apple) {
  268.             print_apple();
  269.         } else {
  270.             time_t now;
  271.             time(&now);
  272.             curtime();
  273.             sysctls(0);
  274.             sysctls(3);
  275.             sysctls(4);
  276.             sysctls(5);
  277.             disk();
  278.             mem();
  279.             sysctls(1);
  280.             gpu();
  281.             uptime(&now);
  282.         }
  283.         return 0;
  284.  }
Advertisement
Add Comment
Please, Sign In to add comment