Advertisement
Guest User

Untitled

a guest
May 19th, 2012
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.20 KB | None | 0 0
  1. /* This program is free software; you can redistribute it and/or modify
  2.  * it under the terms of the GNU General Public License as published by
  3.  * the Free Software Foundation; either version 2 of the License, or
  4.  * (at your option) any later version.
  5.  *
  6.  * This program is distributed in the hope that it will be useful,
  7.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.  * GNU General Public License for more details.
  10.  *
  11.  * You should have received a copy of the GNU General Public License
  12.  * along with this program; if not, write to the Free Software
  13.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  14.  
  15.  Copyright (C) 2011 Andrew Inishev <inish777@gmail.com>
  16. */
  17.  
  18. #include "core/os/os.h"
  19. #include "core/os/kernel.h"
  20. #include "core/os/users.h"
  21. #include <ctype.h>
  22.  
  23. #define PRINTF_MACRO(x,y) printf("\t\"%s\":\"%s\",\n", x, y);
  24. #define PRINTF_MACRO_END(x,y) printf("\t\"%s\":\"%s\"\n", x, y);
  25.                    
  26. int print_os_info(os_info_t* data);
  27. //static char* escape_symbols(char* trololo);
  28.  
  29.  
  30. static void put_escaped_char(char *dest, char ch, size_t *index)
  31. {
  32.     dest[*index] = '\\';
  33.     ++(*index);
  34.     dest[*index] = ch;
  35.     ++(*index);
  36. }
  37.  
  38. /**
  39.  * @brief strdup_escaped - duplicates string and escapes it's characters
  40.  * @param source - source unescaped string
  41.  * @return pointer to string allocated with malloc (so you can apply free())
  42.  */
  43. static char *strdup_escaped(const char *source)
  44. {
  45.     size_t len = strlen(source);
  46.     size_t index = 0, retIndex = 0;
  47.     char *ret = calloc(2 * len, sizeof(char));
  48.     for (index = 0; index < len; ++index)
  49.     {
  50.         switch (source[index])
  51.         {
  52.         case '\"':
  53.         case '\\':
  54.         case '\'':
  55.             put_escaped_char(ret, source[index], &retIndex);
  56.             break;
  57.         case '\f':
  58.             put_escaped_char(ret, 'f', &retIndex);
  59.             break;
  60.         case '\b':
  61.             put_escaped_char(ret, 'b', &retIndex);
  62.             break;
  63.         case '\n':
  64.             put_escaped_char(ret, 'n', &retIndex);
  65.             break;
  66.         case '\v':
  67.             put_escaped_char(ret, 'v', &retIndex);
  68.             break;
  69.         case '\r':
  70.             put_escaped_char(ret, 'r', &retIndex);
  71.             break;
  72.         case '\t':
  73.             put_escaped_char(ret, 't', &retIndex);
  74.             break;
  75.         default:
  76.             ret[retIndex] = source[index];
  77.             ++retIndex;
  78.         }
  79.     }
  80.     ret[retIndex] = '\0'; // paranoia
  81.     return ret;
  82. }
  83.  
  84. void os_info ()
  85. {
  86.     os_info_t* os_info = (os_info_t*)calloc(1, sizeof(os_info_t));
  87.     os_version(os_info);
  88.     os_uptime(os_info);
  89.     os_env(os_info);
  90.     kernel_info(os_info);
  91.     users_info(os_info);
  92.    
  93.     print_os_info(os_info);
  94. }
  95.  
  96. void os_version (os_info_t* data)
  97. {
  98.     FILE* osversionfp;
  99.     char ch[MAXLEN];
  100.     if ((osversionfp = fopen (OS_VERSION_FILE, "r")) != NULL)
  101.     {
  102.         fgets (ch, MAXLEN, osversionfp);
  103.         ch[strlen(ch) - 1] = '\0';
  104.         data->distro = strdup_escaped(ch);
  105.         fclose(osversionfp);
  106.     }
  107.     else
  108.     {
  109.         data->distro = (char*)calloc(sizeof("Unknown\n"), sizeof(char));
  110.         strcpy(data->distro, "Unknown\n");
  111.     }
  112. }
  113.  
  114. void os_uptime (os_info_t* data)
  115. {
  116.     FILE* uptimefp;
  117.     data->uptime = (char*)calloc(1, MAXLEN);
  118.     long uptime;
  119.     int days = 0;
  120.     int hours = 0;
  121.     int minutes = 0;
  122.     int seconds = 0;
  123.     if ((uptimefp = fopen (OS_UPTIME_FILE, "r")) == NULL)
  124.     {
  125.         printf ("%s %s\n", "Error opening", OS_UPTIME_FILE);
  126.         exit (-1);
  127.     }
  128.     fscanf (uptimefp, "%ld", &uptime);
  129.     if ((days = uptime / 86400) >= 1)
  130.         uptime %= 86400;
  131.     if ((hours = uptime / 3600) >= 1)
  132.         uptime %= 3600;
  133.     if ((minutes = uptime / 60) >= 1)
  134.         uptime %= 60;
  135.     seconds = uptime;
  136.  
  137.     if (days >= 1)
  138.         sprintf (data->uptime, "%dd%dh%dm%ds", days, hours, minutes, seconds);
  139.     else if (hours >= 1)
  140.         sprintf (data->uptime, "%dh%dm%ds", hours, minutes, seconds);
  141.     else if (minutes >=0)
  142.         sprintf (data->uptime, "%dm%ds", minutes, seconds);
  143.     fclose (uptimefp);
  144. }
  145.  
  146. void os_env(os_info_t* data)
  147. {
  148.     extern char** environ;
  149.     int item_count = 0;
  150.     int i = 0;
  151.     while (environ[item_count]) item_count++;
  152.     data->env = (char ** ) calloc(item_count + 1, sizeof(char * ));
  153.     for (i; i < item_count; i++)
  154.     {
  155.         data->env[i] = strdup_escaped(environ[i]);
  156.     }
  157.     data->env[i + 1] = NULL;
  158. }
  159.  
  160. int print_os_info(os_info_t* data)
  161. {
  162.     puts("{");
  163.     PRINTF_MACRO("Distro", data->distro);
  164.     PRINTF_MACRO("Uptime", data->uptime);
  165.  
  166.     puts("\"Environment variables\":[");
  167.     int x = 0;
  168.     while(data->env[x])
  169.     {
  170.         if(data->env[x+1])
  171.             printf("\t\"%s\",\n", data->env[x]);
  172.         else
  173.             printf("\t\"%s\"\n", data->env[x]);
  174.         x++;
  175.     }
  176.     puts("],");
  177.  
  178.     puts("\"Users\":[");
  179.     x = 0;
  180.     while(data->users_info[x])
  181.     {
  182.         puts("{");
  183.         PRINTF_MACRO("User", data->users_info[x]->user);
  184.         PRINTF_MACRO("UID", data->users_info[x]->uid);
  185.         PRINTF_MACRO("GID", data->users_info[x]->gid);
  186.         PRINTF_MACRO("Home directory", data->users_info[x]->homedir);
  187.         if(data->users_info[x]->comment)
  188.         {
  189.             PRINTF_MACRO("Shell", data->users_info[x]->shell);
  190.             PRINTF_MACRO_END("Comment", data->users_info[x]->comment);
  191.         }
  192.         else
  193.         {
  194.             PRINTF_MACRO_END("Shell", data->users_info[x]->shell);
  195.         }
  196.         if(data->users_info[x + 1])
  197.             puts("},");
  198.         else
  199.             puts("}");
  200.         x++;
  201.     }
  202.     puts("],");
  203.  
  204.     PRINTF_MACRO("Linux version", data->kernel_info->version);
  205.     PRINTF_MACRO("Boot options", data->kernel_info->cmd_line);
  206.  
  207.     puts("\"List of loaded modules\":[");
  208.     x = 0;
  209.     while(data->kernel_info->modules[x])
  210.     {
  211.         if(data->kernel_info->modules[x + 1])
  212.             printf("\t\"%s\",\n", data->kernel_info->modules[x]);
  213.         else
  214.             printf("\t\"%s\"\n", data->kernel_info->modules[x]);
  215.         x++;    
  216.     }
  217.     puts("]");
  218.  
  219.     puts("}");
  220.     return 0;
  221. }
  222.  
  223. #if 0
  224. static char* escape_symbols(char* trololo)
  225. {
  226.     #define PUTC(ch) buf[y] = '\\'; \
  227.                      buf[++y] = ch;
  228.     int x = 0;
  229.     int y = 0;
  230.  
  231.     char* buf = (char*)malloc(strlen(trololo) * 2);
  232.     for(x; trololo[x]; x++)
  233.     {
  234.         switch(trololo[x])
  235.         {
  236.             case '\"':
  237.             case '\\':
  238.             case '\'':
  239.                 PUTC(trololo[x]);
  240.                 break;
  241.             case '\f':
  242.                 PUTC('f');
  243.                 break;
  244.             case '\b':
  245.                 PUTC('b');
  246.                 break;
  247.             case '\n':
  248.                 PUTC('n');
  249.                 break;
  250.             case '\v':
  251.                 PUTC('v');
  252.                 break;
  253.             case '\r':
  254.                 PUTC('r');
  255.                 break;
  256.             case '\t':
  257.                 PUTC('t');
  258.                 break;
  259.             default:
  260.                 buf[y] = trololo[x];
  261.                 break;
  262.         }
  263.         y++;
  264.     }
  265.     buf[++y] = '\0';
  266.     return buf;
  267. }
  268. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement