/home/fernape/Projects/lkmonitor/src/others.c

Go to the documentation of this file.
00001 
00002 /*
00003         lkmonitor (Linux Kernel Monitor)
00004 
00005         Application for monitoring and tuning a Linux kernel.
00006 
00007         Copyright (C) 2005-2008  Fernando ApesteguĂ­a
00008 
00009         This program is free software; you can redistribute it and/or
00010         modify it under the terms of the GNU General Public License
00011         as published by the Free Software Foundation; either version 2
00012         of the License, or (at your option) any later version.
00013 
00014         This program is distributed in the hope that it will be useful,
00015         but WITHOUT ANY WARRANTY; without even the implied warranty of
00016         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017         GNU General Public License for more details.
00018 
00019         You should have received a copy of the GNU General Public License
00020         along with this program; if not, write to the Free Software
00021         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #  include <config.h>
00026 #endif
00027 
00028 #include <gnome.h>
00029 #include <string.h>
00030 #include <stdio.h>
00031 #include <sys/types.h>
00032 #include <dirent.h>
00033 #include <glib.h>
00034 #include <pthread.h>
00035 #include <gtk/gtk.h>
00036 
00037 #include "lininfo.h"
00038 #include "processes.h"
00039 
00045 int numsort(const void *stra,const void *strb)
00046 {
00047         int a,b;
00048         a=atoi(stra);
00049         b=atoi(strb);
00050         
00051         
00052         return(a<b?(-1):(a==b?0:1));
00053 
00054 }
00055 
00062 int read_values_from_file(int parm,char *path, ...)
00063 {
00064         g_assert(parm!=0 && path!=NULL);
00065         
00066         int i;
00067         int *val;
00068         va_list list;
00069         gchar *contents;
00070         char *p;
00071         
00072         if(g_file_get_contents((gchar *)path,&contents,NULL,NULL)==FALSE){
00073                 va_start(list,path);
00074                 for(i=0; i<parm;i++){
00075                         val=va_arg(list, int *);
00076                         *val=-1;
00077                 }
00078                 va_end(list);
00079                 g_warning("Failed to open %s file",path);
00080                 return -1;
00081         }
00082         
00083         p=contents;
00084         
00085         va_start(list,path);
00086         for(i=0; i<parm;i++){
00087                 sscanf(p,"%d",va_arg(list, int *));
00088                 p=strchr(p,'\t')+1;
00089         }
00090         va_end(list);
00091         
00092         g_free(contents);
00093         return(0);
00094 }
00095 
00102 int write_values_to_file(int parm, char *path,...)
00103 {
00104         g_assert(parm!=0 && path!=NULL);
00105         
00106         int i;
00107         va_list list;
00108         FILE *target;
00109         gchar num_trans[15]; //Large enough
00110         GString *ms;
00111         
00112         ms=g_string_new("");
00113         
00114         if((target=fopen(path,"w"))==NULL){
00115                 g_warning("Can't write to %s\n",path);
00116                 return(-1);
00117         }
00118         
00119         /* File is ready to be written */
00120 
00121         va_start(list,path);
00122         for(i=0;i<parm;i++){
00123                 sprintf(num_trans,"%d",va_arg(list,int));
00124                 g_string_append(ms,num_trans);
00125                 g_string_append(ms," ");
00126         }
00127         va_end(list);
00128 
00129         /* Write */
00130         fprintf(target,"%s",ms->str);
00131         fclose(target);
00132         return(0);
00133 }
00134 
00140 int write_string_to_file(const char *str,char *path)
00141 {
00142   g_assert(str!=NULL && path!=NULL);
00143         
00144   FILE *f;
00145   
00146   if((f=fopen(path,"w"))==NULL){
00147           g_warning("Couldn't open file %s",path);
00148           return -1;
00149   }
00150   
00151   fprintf(f,"%s",str);
00152   fclose(f);
00153   
00154   return(0);
00155 }
00156 
00157 
00162 char *
00163 trim (char *str)
00164 {
00165   g_assert(str!=NULL);
00166   return (str + strspn (str, " "));
00167 }                               
00168 
00169 
00174 char *
00175 rtrim(char* str)
00176 {
00177         g_assert(str!=NULL);
00178         
00179         char* p;
00180         if((p=rindex(str,' '))==NULL)
00181                 return(str);
00182         while(p>str){
00183                 if(*p==' '){
00184                         *p='\0';
00185                         p--;
00186                 }
00187                 else return(str);
00188         }
00189         return(str);//For gcc warning   
00190 }
00191 
00197 char *
00198 skip_lines (char* contents, int num_lines)
00199 {
00200   g_assert(contents!=NULL && num_lines>=0);
00201         
00202   int i, c;
00203 
00204   for (i = 0; i < num_lines; c++){
00205     if (*contents=='\n')
00206       i++;
00207 
00208     contents++;
00209         
00210   }
00211 
00212   return (contents+1);
00213 }
00214 
00222 void
00223 find_data_for (const char* pattern,char* member, const char* contents)
00224 {       
00225    #define TAM 45
00226         g_assert(pattern!=NULL && member!=NULL && contents!=NULL);
00227         
00228         char p[TAM];
00229         char *line;
00230         
00231         sscanf(pattern,"%[^\t:]",p);
00232         //p has the name of the attribute (model, model name...)
00233         //strncpy(p,rtrim(p),TAM);
00234         rtrim(p);
00235         if((line=strstr(contents,p))==NULL){
00236                 strncpy(member,_("Unknown"),strlen(_("Unknown")));
00237                 *(member+strlen(_("Unknown")))='\0';
00238                 return;
00239         }//Position in the correct line
00240 
00241         sscanf(line, pattern,member);
00242         
00243 }                               
00244 
00251 char *parse_batt(const char *str,char *contents, char *dest)
00252 {
00253 
00254         g_assert(str!=NULL && contents!=NULL);
00255         
00256         char data[10];
00257         char *p;
00258         
00259         p=strstr(contents,str);
00260         if(p==NULL){
00261                 return(_("Unknown"));
00262         }
00263         p=strstr(p,":")+1;
00264         sscanf(p,"%s",data);
00265         strncpy(dest,data,sizeof(dest));
00266 
00267         return dest;
00268 }
00269 
00275 gint list_com(gconstpointer a,gconstpointer b)
00276 {
00277         struct lnode *la,*lb;
00278         
00279         la=(struct lnode *)a;
00280         lb=(struct lnode *)b;
00281         
00282         if(la->pid==lb->pid)
00283                 return 0;
00284         
00285         return -1;
00286 }
00287 
00288 

Generated on Tue Apr 1 22:52:52 2008 for lkmonitor by  doxygen 1.5.1