unixwz0r

binfo.c

May 25th, 2015
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.24 KB | None | 0 0
  1. /*
  2.  *
  3.  * BSD Info Program -- A Simple BSD Information Program
  4.  * This program only works on FreeBSD type Systems like FreeBSD, DragonFlyBSD & Darwin
  5.  * Created by RAZ0REDGE
  6.  * July. 2014
  7.  *
  8. */
  9. #include <sys/utsname.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <pwd.h>
  14. #include <time.h>
  15. #include <sys/sysctl.h>
  16. #include <sys/statvfs.h>
  17. #define C0 "\x1b[0m"     //Reset
  18. #define C1 "\x1b[0;32m"  //Green
  19. #define C2 "\x1b[0;31m"  //Red
  20. #define C3 "\x1b[0;36m"  //Cyan
  21. #define C4 "\x1b[0;35m"  //Magenta
  22. #define C5 "\x1b[0;33m"  //Yellow
  23. #define CYN C3
  24. #define GRN C1
  25. #define NOR C0
  26. #define ANSI_COLOR_RED     "\x1b[31m" // Another Example to show ANSI colours thanks vinnie01
  27. #define ANSI_COLOR_GREEN   "\x1b[32m"
  28. #define ANSI_COLOR_YELLOW  "\x1b[33m"
  29. #define ANSI_COLOR_BLUE    "\x1b[34m"
  30. #define ANSI_COLOR_MAGENTA "\x1b[35m"
  31. #define ANSI_COLOR_CYAN    "\x1b[36m"
  32. #define ANSI_COLOR_RESET   "\x1b[0m"
  33.  
  34. static const struct {
  35.   const char *ctls;
  36.   const char *names;
  37. } values[] = {
  38.   { "hw.model", "CPU" },
  39.   { "hw.memsize", "Memory" },
  40.   { "kern.ostype", "OS" },
  41.   { "kern.osrelease", "Kernel" },
  42.   { "kern.hostname", "Hostname" },
  43. };
  44.  
  45. static void sysctls(int i);
  46. static void disk(void);
  47. static void mem(void);
  48. static void envs(int i);
  49.  
  50. // The help information. When you execute the program using binfo -v
  51. void help(void) {
  52.       printf("╔═══════════════════════════════════════╗\n"
  53.              "║ BSD Info - By: RAZ0REDGE - July. 2014 ║\n"
  54.              "╚═══════════════════════════════════════╝\n");
  55.                exit(0);
  56. }
  57.  
  58. // FreeBSD sysctl
  59. static void sysctls(int i) {
  60.       size_t len;
  61.       if(i==2) {
  62.        // mem();
  63.       } else {
  64.         sysctlbyname(values[i].ctls, NULL, &len, NULL, 0);
  65.         char *type = malloc(len);
  66.         sysctlbyname(values[i].ctls, type, &len, NULL, 0);
  67.         printf(GRN"║ %-10s╠═%s\n", values[i].names, type);
  68.         free(type);
  69.   }
  70. }
  71. // User Terminal Shell status
  72. static void envs(int i) {
  73.     char *type;
  74.     struct passwd *passwd;
  75.     if(i == 1) {
  76.         type = getenv("TERM");
  77.         printf(GRN"Terminal  : "NOR"%s\n", type);
  78.     } else if(i ==2) {
  79.         passwd = getpwuid(getuid());
  80.         printf(GRN"Shell     : "NOR"%s\n",passwd->pw_shell);
  81.     } else if(i == 3) {
  82.         passwd = getpwuid(getuid());
  83.         printf(GRN"%s\n",passwd->pw_name);
  84.     }
  85. }
  86.  
  87. // Disk usage in % of total disk size
  88. static void disk(void) { // Now works for all BSD Systems lulz took awhile to figured out :P
  89.     struct statvfs info;
  90.     if(!statvfs("/", &info)) {
  91.         unsigned long left  = (info.f_bfree * info.f_frsize);
  92.         unsigned long total = (info.f_blocks * info.f_frsize);
  93.         unsigned long used  = total - left;
  94.         float perc  = (float)used / (float)total;
  95.         printf(GRN"║ Disk      ╠═%.2f%% of %.2f GB\n",
  96.                 perc * 100, (float)total / 1e+09);
  97.   }
  98. }
  99. // Uptime Information by: yrmt <-- Still learning C I will redo this code once I get better.
  100. static void uptime(time_t *nowp) { // Thank you yrmt
  101.         struct timeval boottime;
  102.         time_t uptime;
  103.         int days, hrs, mins, secs;
  104.         int mib[2];
  105.         size_t size;
  106.         char buf[256];
  107.         if(strftime(buf, sizeof(buf), NULL, localtime(nowp)))
  108.              mib[0] = CTL_KERN;
  109.         mib[1] = KERN_BOOTTIME;
  110.         size = sizeof(boottime);
  111.         if(sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
  112.                         boottime.tv_sec) {
  113.                 uptime = *nowp - boottime.tv_sec;
  114.                 if(uptime > 60)
  115.                         uptime += 30;
  116.                 days = (int)uptime / 86400;
  117.                 uptime %= 86400;
  118.                 hrs = (int)uptime / 3600;
  119.                 uptime %= 3600;
  120.                 mins = (int)uptime / 60;
  121.                 secs = uptime % 60;
  122.                
  123.                 /* Welcome User */
  124.  
  125.                 printf("\n");
  126.                 printf(C1"Welcome to BSD UNIX: ");envs(3);
  127.                 printf("\n");
  128.                 printf(C1"╔═══════════╗\n");
  129.                 printf(C1"║ System Up ╠═");
  130.                 if(days > 0)
  131.                         printf("%d day%s", days, days > 1? "s " : " ");
  132.                 if (hrs > 0 && mins > 0)
  133.                         printf("%02d:%02d", hrs, mins);
  134.                 else if(hrs == 0 && mins > 0)
  135.                         printf("0:%02d", mins);
  136.                 else
  137.                         printf("0:00");
  138.                 putchar('\n');
  139.   }
  140. }
  141. int main(int argc, char **argv) { // Took me awhile to get this sorted with the help of Vincent0ne- cheers!
  142.  
  143.     if (argc >= 2) {
  144.         int c;
  145.         while ((c = getopt(argc, argv, "v")) != -1) {
  146.             switch (c) {
  147.                 case 'v':
  148.                 default:
  149.                     help();
  150.                     break;
  151.     }
  152.   }
  153. }
  154. time_t now;
  155. time(&now);
  156. uptime(&now);  
  157. {                                                                                      
  158.         char computer[256];
  159.         struct utsname uts;
  160.         time_t timeval;
  161.        
  162.         (void)time(&timeval);
  163.        
  164.         if(gethostname(computer, 255) != 0 || uname(&uts) < 0) {  
  165.                 fprintf(stderr, "Could not get host information, so fuck off\n"); // Lulz very funny Vincent0ne-
  166.                 exit(1);
  167.          }
  168.          
  169.          printf(ANSI_COLOR_GREEN"║ User      ╠═%s\n", getlogin()); // Another example to show info thanks vinnie01
  170.          printf(ANSI_COLOR_GREEN"║ Date      ╠═%s", ctime(&timeval));
  171.          printf(ANSI_COLOR_GREEN"║ Hostname  ╠═%s\n", computer);
  172.          printf(ANSI_COLOR_GREEN"║ OS        ╠═%s\n", uts.sysname);
  173.          printf(ANSI_COLOR_GREEN"║ Version   ╠═%s\n", uts.release);
  174.          printf(ANSI_COLOR_GREEN"║ Hardware  ╠═%s\n", uts.machine);
  175.          sysctls(0); // Shows CPU Model or System Model in Darwin
  176.          disk(); // Using statvfs interesting how it works much like df.
  177.          printf(ANSI_COLOR_GREEN"╚═══════════╝\n");
  178.          printf("\n");                
  179.  
  180.   }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment