Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *
- * BSD Info Program -- A Simple BSD Information Program
- * This program only works on FreeBSD type Systems like FreeBSD, DragonFlyBSD & Darwin
- * Created by RAZ0REDGE
- * July. 2014
- *
- */
- #include <sys/utsname.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <pwd.h>
- #include <time.h>
- #include <sys/sysctl.h>
- #include <sys/statvfs.h>
- #define C0 "\x1b[0m" //Reset
- #define C1 "\x1b[0;32m" //Green
- #define C2 "\x1b[0;31m" //Red
- #define C3 "\x1b[0;36m" //Cyan
- #define C4 "\x1b[0;35m" //Magenta
- #define C5 "\x1b[0;33m" //Yellow
- #define CYN C3
- #define GRN C1
- #define NOR C0
- #define ANSI_COLOR_RED "\x1b[31m" // Another Example to show ANSI colours thanks vinnie01
- #define ANSI_COLOR_GREEN "\x1b[32m"
- #define ANSI_COLOR_YELLOW "\x1b[33m"
- #define ANSI_COLOR_BLUE "\x1b[34m"
- #define ANSI_COLOR_MAGENTA "\x1b[35m"
- #define ANSI_COLOR_CYAN "\x1b[36m"
- #define ANSI_COLOR_RESET "\x1b[0m"
- static const struct {
- const char *ctls;
- const char *names;
- } values[] = {
- { "hw.model", "CPU" },
- { "hw.memsize", "Memory" },
- { "kern.ostype", "OS" },
- { "kern.osrelease", "Kernel" },
- { "kern.hostname", "Hostname" },
- };
- static void sysctls(int i);
- static void disk(void);
- static void mem(void);
- static void envs(int i);
- // The help information. When you execute the program using binfo -v
- void help(void) {
- printf("╔═══════════════════════════════════════╗\n"
- "║ BSD Info - By: RAZ0REDGE - July. 2014 ║\n"
- "╚═══════════════════════════════════════╝\n");
- exit(0);
- }
- // FreeBSD sysctl
- static void sysctls(int i) {
- size_t len;
- if(i==2) {
- // mem();
- } else {
- sysctlbyname(values[i].ctls, NULL, &len, NULL, 0);
- char *type = malloc(len);
- sysctlbyname(values[i].ctls, type, &len, NULL, 0);
- printf(GRN"║ %-10s╠═%s\n", values[i].names, type);
- free(type);
- }
- }
- // User Terminal Shell status
- static void envs(int i) {
- char *type;
- struct passwd *passwd;
- if(i == 1) {
- type = getenv("TERM");
- printf(GRN"Terminal : "NOR"%s\n", type);
- } else if(i ==2) {
- passwd = getpwuid(getuid());
- printf(GRN"Shell : "NOR"%s\n",passwd->pw_shell);
- } else if(i == 3) {
- passwd = getpwuid(getuid());
- printf(GRN"%s\n",passwd->pw_name);
- }
- }
- // Disk usage in % of total disk size
- static void disk(void) { // Now works for all BSD Systems lulz took awhile to figured out :P
- struct statvfs info;
- if(!statvfs("/", &info)) {
- unsigned long left = (info.f_bfree * info.f_frsize);
- unsigned long total = (info.f_blocks * info.f_frsize);
- unsigned long used = total - left;
- float perc = (float)used / (float)total;
- printf(GRN"║ Disk ╠═%.2f%% of %.2f GB\n",
- perc * 100, (float)total / 1e+09);
- }
- }
- // Uptime Information by: yrmt <-- Still learning C I will redo this code once I get better.
- static void uptime(time_t *nowp) { // Thank you yrmt
- struct timeval boottime;
- time_t uptime;
- int days, hrs, mins, secs;
- int mib[2];
- size_t size;
- char buf[256];
- if(strftime(buf, sizeof(buf), NULL, localtime(nowp)))
- mib[0] = CTL_KERN;
- mib[1] = KERN_BOOTTIME;
- size = sizeof(boottime);
- if(sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
- boottime.tv_sec) {
- uptime = *nowp - boottime.tv_sec;
- if(uptime > 60)
- uptime += 30;
- days = (int)uptime / 86400;
- uptime %= 86400;
- hrs = (int)uptime / 3600;
- uptime %= 3600;
- mins = (int)uptime / 60;
- secs = uptime % 60;
- /* Welcome User */
- printf("\n");
- printf(C1"Welcome to BSD UNIX: ");envs(3);
- printf("\n");
- printf(C1"╔═══════════╗\n");
- printf(C1"║ System Up ╠═");
- if(days > 0)
- printf("%d day%s", days, days > 1? "s " : " ");
- if (hrs > 0 && mins > 0)
- printf("%02d:%02d", hrs, mins);
- else if(hrs == 0 && mins > 0)
- printf("0:%02d", mins);
- else
- printf("0:00");
- putchar('\n');
- }
- }
- int main(int argc, char **argv) { // Took me awhile to get this sorted with the help of Vincent0ne- cheers!
- if (argc >= 2) {
- int c;
- while ((c = getopt(argc, argv, "v")) != -1) {
- switch (c) {
- case 'v':
- default:
- help();
- break;
- }
- }
- }
- time_t now;
- time(&now);
- uptime(&now);
- {
- char computer[256];
- struct utsname uts;
- time_t timeval;
- (void)time(&timeval);
- if(gethostname(computer, 255) != 0 || uname(&uts) < 0) {
- fprintf(stderr, "Could not get host information, so fuck off\n"); // Lulz very funny Vincent0ne-
- exit(1);
- }
- printf(ANSI_COLOR_GREEN"║ User ╠═%s\n", getlogin()); // Another example to show info thanks vinnie01
- printf(ANSI_COLOR_GREEN"║ Date ╠═%s", ctime(&timeval));
- printf(ANSI_COLOR_GREEN"║ Hostname ╠═%s\n", computer);
- printf(ANSI_COLOR_GREEN"║ OS ╠═%s\n", uts.sysname);
- printf(ANSI_COLOR_GREEN"║ Version ╠═%s\n", uts.release);
- printf(ANSI_COLOR_GREEN"║ Hardware ╠═%s\n", uts.machine);
- sysctls(0); // Shows CPU Model or System Model in Darwin
- disk(); // Using statvfs interesting how it works much like df.
- printf(ANSI_COLOR_GREEN"╚═══════════╝\n");
- printf("\n");
- }
- }
Add Comment
Please, Sign In to add comment