Advertisement
BSDG33KCLUB

finfo.c

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