Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
- Copyright (C) 2011 Andrew Inishev <inish777@gmail.com>
- */
- #include "core/os/os.h"
- #include "core/os/kernel.h"
- #include "core/os/users.h"
- #include <ctype.h>
- #define PRINTF_MACRO(x,y) printf("\t\"%s\":\"%s\",\n", x, y);
- #define PRINTF_MACRO_END(x,y) printf("\t\"%s\":\"%s\"\n", x, y);
- int print_os_info(os_info_t* data);
- //static char* escape_symbols(char* trololo);
- static void put_escaped_char(char *dest, char ch, size_t *index)
- {
- dest[*index] = '\\';
- ++(*index);
- dest[*index] = ch;
- ++(*index);
- }
- /**
- * @brief strdup_escaped - duplicates string and escapes it's characters
- * @param source - source unescaped string
- * @return pointer to string allocated with malloc (so you can apply free())
- */
- static char *strdup_escaped(const char *source)
- {
- size_t len = strlen(source);
- size_t index = 0, retIndex = 0;
- char *ret = calloc(2 * len, sizeof(char));
- for (index = 0; index < len; ++index)
- {
- switch (source[index])
- {
- case '\"':
- case '\\':
- case '\'':
- put_escaped_char(ret, source[index], &retIndex);
- break;
- case '\f':
- put_escaped_char(ret, 'f', &retIndex);
- break;
- case '\b':
- put_escaped_char(ret, 'b', &retIndex);
- break;
- case '\n':
- put_escaped_char(ret, 'n', &retIndex);
- break;
- case '\v':
- put_escaped_char(ret, 'v', &retIndex);
- break;
- case '\r':
- put_escaped_char(ret, 'r', &retIndex);
- break;
- case '\t':
- put_escaped_char(ret, 't', &retIndex);
- break;
- default:
- ret[retIndex] = source[index];
- ++retIndex;
- }
- }
- ret[retIndex] = '\0'; // paranoia
- return ret;
- }
- void os_info ()
- {
- os_info_t* os_info = (os_info_t*)calloc(1, sizeof(os_info_t));
- os_version(os_info);
- os_uptime(os_info);
- os_env(os_info);
- kernel_info(os_info);
- users_info(os_info);
- print_os_info(os_info);
- }
- void os_version (os_info_t* data)
- {
- FILE* osversionfp;
- char ch[MAXLEN];
- if ((osversionfp = fopen (OS_VERSION_FILE, "r")) != NULL)
- {
- fgets (ch, MAXLEN, osversionfp);
- ch[strlen(ch) - 1] = '\0';
- data->distro = strdup_escaped(ch);
- fclose(osversionfp);
- }
- else
- {
- data->distro = (char*)calloc(sizeof("Unknown\n"), sizeof(char));
- strcpy(data->distro, "Unknown\n");
- }
- }
- void os_uptime (os_info_t* data)
- {
- FILE* uptimefp;
- data->uptime = (char*)calloc(1, MAXLEN);
- long uptime;
- int days = 0;
- int hours = 0;
- int minutes = 0;
- int seconds = 0;
- if ((uptimefp = fopen (OS_UPTIME_FILE, "r")) == NULL)
- {
- printf ("%s %s\n", "Error opening", OS_UPTIME_FILE);
- exit (-1);
- }
- fscanf (uptimefp, "%ld", &uptime);
- if ((days = uptime / 86400) >= 1)
- uptime %= 86400;
- if ((hours = uptime / 3600) >= 1)
- uptime %= 3600;
- if ((minutes = uptime / 60) >= 1)
- uptime %= 60;
- seconds = uptime;
- if (days >= 1)
- sprintf (data->uptime, "%dd%dh%dm%ds", days, hours, minutes, seconds);
- else if (hours >= 1)
- sprintf (data->uptime, "%dh%dm%ds", hours, minutes, seconds);
- else if (minutes >=0)
- sprintf (data->uptime, "%dm%ds", minutes, seconds);
- fclose (uptimefp);
- }
- void os_env(os_info_t* data)
- {
- extern char** environ;
- int item_count = 0;
- int i = 0;
- while (environ[item_count]) item_count++;
- data->env = (char ** ) calloc(item_count + 1, sizeof(char * ));
- for (i; i < item_count; i++)
- {
- data->env[i] = strdup_escaped(environ[i]);
- }
- data->env[i + 1] = NULL;
- }
- int print_os_info(os_info_t* data)
- {
- puts("{");
- PRINTF_MACRO("Distro", data->distro);
- PRINTF_MACRO("Uptime", data->uptime);
- puts("\"Environment variables\":[");
- int x = 0;
- while(data->env[x])
- {
- if(data->env[x+1])
- printf("\t\"%s\",\n", data->env[x]);
- else
- printf("\t\"%s\"\n", data->env[x]);
- x++;
- }
- puts("],");
- puts("\"Users\":[");
- x = 0;
- while(data->users_info[x])
- {
- puts("{");
- PRINTF_MACRO("User", data->users_info[x]->user);
- PRINTF_MACRO("UID", data->users_info[x]->uid);
- PRINTF_MACRO("GID", data->users_info[x]->gid);
- PRINTF_MACRO("Home directory", data->users_info[x]->homedir);
- if(data->users_info[x]->comment)
- {
- PRINTF_MACRO("Shell", data->users_info[x]->shell);
- PRINTF_MACRO_END("Comment", data->users_info[x]->comment);
- }
- else
- {
- PRINTF_MACRO_END("Shell", data->users_info[x]->shell);
- }
- if(data->users_info[x + 1])
- puts("},");
- else
- puts("}");
- x++;
- }
- puts("],");
- PRINTF_MACRO("Linux version", data->kernel_info->version);
- PRINTF_MACRO("Boot options", data->kernel_info->cmd_line);
- puts("\"List of loaded modules\":[");
- x = 0;
- while(data->kernel_info->modules[x])
- {
- if(data->kernel_info->modules[x + 1])
- printf("\t\"%s\",\n", data->kernel_info->modules[x]);
- else
- printf("\t\"%s\"\n", data->kernel_info->modules[x]);
- x++;
- }
- puts("]");
- puts("}");
- return 0;
- }
- #if 0
- static char* escape_symbols(char* trololo)
- {
- #define PUTC(ch) buf[y] = '\\'; \
- buf[++y] = ch;
- int x = 0;
- int y = 0;
- char* buf = (char*)malloc(strlen(trololo) * 2);
- for(x; trololo[x]; x++)
- {
- switch(trololo[x])
- {
- case '\"':
- case '\\':
- case '\'':
- PUTC(trololo[x]);
- break;
- case '\f':
- PUTC('f');
- break;
- case '\b':
- PUTC('b');
- break;
- case '\n':
- PUTC('n');
- break;
- case '\v':
- PUTC('v');
- break;
- case '\r':
- PUTC('r');
- break;
- case '\t':
- PUTC('t');
- break;
- default:
- buf[y] = trololo[x];
- break;
- }
- y++;
- }
- buf[++y] = '\0';
- return buf;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement