Index: config.h.in ================================================================== --- config.h.in +++ config.h.in @@ -30,10 +30,13 @@ /* Define to 1 if you have the header file. */ #undef HAVE_STRINGS_H /* Define to 1 if you have the header file. */ #undef HAVE_STRING_H + +/* Define if your sysinfo structure has a mem_info unit */ +#undef HAVE_SYSINFO_MEM_UNIT /* Define to 1 if you have the header file. */ #undef HAVE_SYS_STAT_H /* Define to 1 if you have the header file. */ Index: configure ================================================================== --- configure +++ configure @@ -1420,10 +1420,67 @@ fi eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno as_fn_set_status $ac_retval } # ac_fn_c_try_compile + +# ac_fn_c_check_member LINENO AGGR MEMBER VAR INCLUDES +# ---------------------------------------------------- +# Tries to find if the field MEMBER exists in type AGGR, after including +# INCLUDES, setting cache variable VAR accordingly. +ac_fn_c_check_member () +{ + as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for $2.$3" >&5 +$as_echo_n "checking for $2.$3... " >&6; } +if eval \${$4+:} false; then : + $as_echo_n "(cached) " >&6 +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$5 +int +main () +{ +static $2 ac_aggr; +if (ac_aggr.$3) +return 0; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + eval "$4=yes" +else + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ +$5 +int +main () +{ +static $2 ac_aggr; +if (sizeof ac_aggr.$3) +return 0; + ; + return 0; +} +_ACEOF +if ac_fn_c_try_compile "$LINENO"; then : + eval "$4=yes" +else + eval "$4=no" +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext +fi +eval ac_res=\$$4 + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5 +$as_echo "$ac_res" >&6; } + eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno + +} # ac_fn_c_check_member # ac_fn_c_try_link LINENO # ----------------------- # Try to link conftest.$ac_ext, and return whether this succeeded. ac_fn_c_try_link () @@ -2961,10 +3018,21 @@ test -z "$INSTALL_SCRIPT" && INSTALL_SCRIPT='${INSTALL}' test -z "$INSTALL_DATA" && INSTALL_DATA='${INSTALL} -m 644' + +ac_fn_c_check_member "$LINENO" "struct sysinfo" "mem_unit" "ac_cv_member_struct_sysinfo_mem_unit" "#include +" +if test "x$ac_cv_member_struct_sysinfo_mem_unit" = xyes; then : + + +$as_echo "#define HAVE_SYSINFO_MEM_UNIT 1" >>confdefs.h + + +fi + ac_ext=c ac_cpp='$CPP $CPPFLAGS' ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' Index: configure.in ================================================================== --- configure.in +++ configure.in @@ -4,9 +4,13 @@ dnl Checks for programs. AC_PROG_CC AC_PROG_MAKE_SET AC_PROG_INSTALL + +AC_CHECK_MEMBER([struct sysinfo.mem_unit], [ + AC_DEFINE([HAVE_SYSINFO_MEM_UNIT], [1], [Define if your sysinfo structure has a mem_info unit]) +],, [[#include ]]) DC_ASK_STATGRAB AC_OUTPUT(Makefile) Index: freecolor.c ================================================================== --- freecolor.c +++ freecolor.c @@ -29,10 +29,12 @@ #include "config.h" #endif #if defined(HAVE_LIBSTATGRAB) && !defined(HAVE_LIBSTATGRAB_H) #undef HAVE_LIBSTATGRAB #endif +#include +#include #ifndef HAVE_LIBSTATGRAB #include #include #ifndef PROC_MEMINFO @@ -54,32 +56,39 @@ extern char *optarg; extern int optind, opterr, optopt; struct freecolor_meminfo { - unsigned long totalram; /* Total usable main memory size */ - unsigned long freeram; /* Available memory size */ - unsigned long sharedram; /* Amount of shared memory */ - unsigned long bufferram; /* Memory used by buffers */ - unsigned long cachedram; /* Memory used for I/O cache */ - unsigned long totalswap; /* Total swap space size */ - unsigned long freeswap; /* swap space still available */ + uint64_t totalram; /* Total usable main memory size */ + uint64_t freeram; /* Available memory size */ + uint64_t sharedram; /* Amount of shared memory */ + uint64_t bufferram; /* Memory used by buffers */ + uint64_t cachedram; /* Memory used for I/O cache */ + uint64_t totalswap; /* Total swap space size */ + uint64_t freeswap; /* swap space still available */ }; -void bargraph(float percent, float secondper, char marks[BARLEN + HEADERLEN + 1], int usefull) { +void bargraph(double percent, double secondper, char marks[BARLEN + HEADERLEN + 1], int usefull) { char percentone[BARLEN + 1], percenttwo[BARLEN + 1], remain[BARLEN + 1]; unsigned int numberofmarks, numofmarkstwo, remainnum; - numberofmarks=(int) ((float) (BARLEN * (percent / 100))); + numberofmarks=(int) ((double) (BARLEN * (percent / 100))); + + assert(numberofmarks <= BARLEN); if (!usefull) { - numofmarkstwo = (int) ((float) (BARLEN * (secondper / 100))); + numofmarkstwo = (int) ((double) (BARLEN * (secondper / 100))); } else { numofmarkstwo = (BARLEN - numberofmarks); } + + assert(numofmarkstwo <= BARLEN); remainnum = BARLEN - (numberofmarks + numofmarkstwo); + + assert(remainnum <= BARLEN); + memset(percentone, '#', numberofmarks); memset(percenttwo, '%', numofmarkstwo); memset(remain, '.', remainnum); percentone[numberofmarks] = 0; @@ -121,21 +130,28 @@ return(results); } int get_all_meminfo(struct freecolor_meminfo *retval) { struct sysinfo sinfo; - unsigned long meminfo_cached; + uint64_t meminfo_cached; + uint64_t multiplier; meminfo_cached = get_meminfo("Cached:"); sysinfo(&sinfo); - retval->totalram = sinfo.totalram; - retval->freeram = sinfo.freeram; - retval->sharedram = sinfo.sharedram; - retval->bufferram = sinfo.bufferram; - retval->totalswap = sinfo.totalswap; - retval->freeswap = sinfo.freeswap; +#ifdef HAVE_SYSINFO_MEM_UNIT + multiplier = sinfo.mem_unit; +#else + multiplier = 1; +#endif + + retval->totalram = sinfo.totalram * multiplier; + retval->freeram = sinfo.freeram * multiplier; + retval->sharedram = sinfo.sharedram * multiplier; + retval->bufferram = sinfo.bufferram * multiplier; + retval->totalswap = sinfo.totalswap * multiplier; + retval->freeswap = sinfo.freeswap * multiplier; retval->cachedram = meminfo_cached; return(0); } @@ -185,13 +201,13 @@ } #endif int main(int argc, char **argv) { struct freecolor_meminfo sinfo; - unsigned long cachedbuffer, divisor; - float percentram, percentswap, percentbuffer, percentused, percentfree; - float ramfree, ramtotal, swapfree, swaptotal, doloop, totaltotal; + uint64_t cachedbuffer, divisor; + double percentram, percentswap, percentbuffer, percentused, percentfree; + double ramfree, ramtotal, swapfree, swaptotal, doloop, totaltotal; char realbarchart[BARLEN +HEADERLEN + 1], swapbarchart[BARLEN + HEADERLEN + 1], totalbarchart[BARLEN + HEADERLEN + 1]; int i, dototals, doold, linestoup; int gam_ret; /* Set defaults */ @@ -247,17 +263,17 @@ swaptotal = sinfo.totalswap; totaltotal = sinfo.totalram + sinfo.totalswap; cachedbuffer = sinfo.bufferram + sinfo.cachedram; - percentram = (float) ((ramfree / ramtotal) * 100); - percentbuffer = (float) ((cachedbuffer / ramtotal) * 100); - percentfree = (float) (((sinfo.freeram + sinfo.freeswap + cachedbuffer) / totaltotal) * 100); + percentram = (double) ((ramfree / ramtotal) * 100); + percentbuffer = (double) ((cachedbuffer / ramtotal) * 100); + percentfree = (double) (((sinfo.freeram + sinfo.freeswap + cachedbuffer) / totaltotal) * 100); percentused = (int) (100 - ((int) percentfree)); if (swaptotal!=0) { - percentswap = (float) (( swapfree / swaptotal) * 100); + percentswap = (double) (( swapfree / swaptotal) * 100); } else { percentswap = 0; } bargraph(percentswap, 0, swapbarchart, 0); @@ -266,25 +282,25 @@ if (!doold) { printf("Physical : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n", realbarchart, (int) (percentram + percentbuffer), - (unsigned long) (ramfree + cachedbuffer) / divisor, - (unsigned long) ramtotal/divisor + (unsigned long) ((ramfree + cachedbuffer) / divisor), + (unsigned long) (ramtotal / divisor) ); printf("Swap : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m %i\033[0;31m%%\033[0m\t(%lu/%lu)\n", swapbarchart, (int) percentswap, - (unsigned long) swapfree / divisor, - (unsigned long) swaptotal/divisor + (unsigned long) (swapfree / divisor), + (unsigned long) (swaptotal/divisor) ); if (dototals) { printf("Total : \033[1;30m[\033[1;32m%s\033[1;30m]\033[1;37m \033[0m(%lu=%lu+%lu)\n", totalbarchart, - (unsigned long) (sinfo.totalram + sinfo.totalswap) / divisor, + (unsigned long) ((sinfo.totalram + sinfo.totalswap) / divisor), (unsigned long) ((sinfo.freeram + sinfo.freeswap) / divisor), (unsigned long) ((totaltotal - ((sinfo.freeram + sinfo.freeswap + cachedbuffer))) / divisor) ); } } else { @@ -305,12 +321,12 @@ ); if (dototals) { printf("Total: %11lu = (%8lu (used) + %8lu (free))\n", (unsigned long) (totaltotal / divisor), - (((long) totaltotal) - ((sinfo.freeram+sinfo.freeswap))) / divisor, - (sinfo.freeram+sinfo.freeswap+((unsigned long) cachedbuffer)) / divisor + (unsigned long) ((((long) totaltotal) - ((sinfo.freeram+sinfo.freeswap))) / divisor), + (unsigned long) ((sinfo.freeram+sinfo.freeswap+((unsigned long) cachedbuffer)) / divisor) ); } } if (doloop==0) {