Back to index

lshw  02.16
Defines | Functions | Variables
osutils.cc File Reference
#include "version.h"
#include "osutils.h"
#include <sstream>
#include <iomanip>
#include <stack>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <wchar.h>
#include <sys/utsname.h>
#include <linux/kdev_t.h>

Go to the source code of this file.

Defines

#define putchar(c)   ((char)((c) & 0xff))
#define REPLACEMENT   "\357\277\275"

Functions

 __ID ("@(#) $Id: osutils.cc 2433 2012-01-10 22:01:30Z lyonel $")
bool pushd (const string &dir)
string popd ()
string pwd ()
size_t splitlines (const string &s, vector< string > &lines, char separator)
bool exists (const string &path)
bool loadfile (const string &file, vector< string > &list)
string get_string (const string &path, const string &def)
long get_number (const string &path, long def)
int selectdir (const struct dirent *d)
int selectlink (const struct dirent *d)
int selectfile (const struct dirent *d)
static int selectdevice (const struct dirent *d)
static bool matches (string name, mode_t mode, dev_t device)
static string find_deventry (string basepath, mode_t mode, dev_t device)
string find_deventry (mode_t mode, dev_t device)
string get_devid (const string &name)
bool samefile (const string &path1, const string &path2)
string uppercase (const string &s)
string lowercase (const string &s)
string tostring (unsigned long long n)
string tohex (unsigned long long n)
string join (const string &j, const string &s1, const string &s2)
bool matches (const string &s, const string &pattern, int cflags)
string readlink (const string &path)
string realpath (const string &path)
string spaces (unsigned int count, const string &space)
string escape (const string &s)
string escapeJSON (const string &s)
string escapecomment (const string &s)
unsigned short be_short (const void *from)
unsigned short le_short (const void *from)
unsigned long be_long (const void *from)
unsigned long le_long (const void *from)
unsigned long long be_longlong (const void *from)
unsigned long long le_longlong (const void *from)
int open_dev (dev_t dev, const string &name)
string utf8 (wchar_t c)
string utf8 (uint16_t *s, ssize_t length, bool forcelittleendian)
string utf8_sanitize (const string &s)
string decimalkilos (unsigned long long value)
string kilobytes (unsigned long long value)
string operating_system ()
string platform ()

Variables

static stack< string > dirs

Define Documentation

#define putchar (   c)    ((char)((c) & 0xff))

Definition at line 595 of file osutils.cc.

#define REPLACEMENT   "\357\277\275"

Definition at line 642 of file osutils.cc.


Function Documentation

__ID ( "@(#) $Id: osutils.cc 2433 2012-01-10 22:01:30Z lyonel $"  )
unsigned long be_long ( const void *  from)

Definition at line 515 of file osutils.cc.

{
  __uint8_t *p = (__uint8_t*)from;

  return ((__uint32_t)(p[0]) << 24) +
    ((__uint32_t)(p[1]) << 16) +
    ((__uint32_t)(p[2]) << 8) +
    (__uint32_t)p[3];
}

Here is the caller graph for this function:

unsigned long long be_longlong ( const void *  from)

Definition at line 538 of file osutils.cc.

{
  __uint8_t *p = (__uint8_t*)from;

  return ((unsigned long long)(p[0]) << 56) +
    ((unsigned long long)(p[1]) << 48) +
    ((unsigned long long)(p[2]) << 40) +
    ((unsigned long long)(p[3]) << 32) +
    ((unsigned long long)(p[4]) << 24) +
    ((unsigned long long)(p[5]) << 16) +
    ((unsigned long long)(p[6]) << 8) +
    (unsigned long long)p[7];
}
unsigned short be_short ( const void *  from)

Definition at line 497 of file osutils.cc.

{
  __uint8_t *p = (__uint8_t*)from;

  return ((__uint16_t)(p[0]) << 8) +
    (__uint16_t)p[1];
}

Here is the caller graph for this function:

string decimalkilos ( unsigned long long  value)

Definition at line 712 of file osutils.cc.

{
  const char *prefixes = "KMGTPEZY";
  unsigned int i = 0;
  ostringstream out;

  while ((i <= strlen(prefixes)) && ((value > 10000) || (value % 1000 == 0)))
  {
    value = value / 1000;
    i++;
  }

  out << value;
  if ((i > 0) && (i <= strlen(prefixes)))
    out << prefixes[i - 1];

  return out.str();
}

Here is the caller graph for this function:

string escape ( const string &  s)

Definition at line 425 of file osutils.cc.

{
  string result = "";

  for (unsigned int i = 0; i < s.length(); i++)
    // #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
    if (s[i] == 0x9
        || s[i] == 0xA
        || s[i] == 0xD
        || s[i] >= 0x20)
      switch (s[i])
      {
        case '<':
          result += "&lt;";
          break;
        case '>':
          result += "&gt;";
          break;
        case '&':
          result += "&amp;";
          break;
        case '"':
          result += "&quot;";
          break;
      default:
        result += s[i];
    }

  return result;
}

Here is the caller graph for this function:

string escapecomment ( const string &  s)

Definition at line 482 of file osutils.cc.

{
  string result = "";
  char previous = 0;

  for (unsigned int i = 0; i < s.length(); i++)
    if(!(previous == '-' && s[i] == '-'))
    {
      result += s[i];
      previous = s[i];
    }

  return result;
}

Here is the caller graph for this function:

string escapeJSON ( const string &  s)

Definition at line 456 of file osutils.cc.

{
  string result = "";

  for (unsigned int i = 0; i < s.length(); i++)
    switch (s[i])
    {
      case '\r':
        result += "\\r";
        break;
      case '\n':
        result += "\\n";
        break;
      case '\t':
        result += "\\t";
        break;
      case '"':
        result += "\\\"";
        break;
    default:
      result += s[i];
  }

  return result;
}

Here is the caller graph for this function:

bool exists ( const string &  path)

Definition at line 109 of file osutils.cc.

{
  return access(path.c_str(), F_OK) == 0;
}

Here is the caller graph for this function:

static string find_deventry ( string  basepath,
mode_t  mode,
dev_t  device 
) [static]

Definition at line 237 of file osutils.cc.

{
  struct dirent **namelist;
  int n, i;
  string result = "";

  pushd(basepath);

  n = scandir(".", &namelist, selectdevice, alphasort);

  if (n < 0)
  {
    popd();
    return "";
  }

  for (i = 0; i < n; i++)
  {
    if (result == "" && matches(namelist[i]->d_name, mode, device))
      result = string(namelist[i]->d_name);
    free(namelist[i]);
  }
  free(namelist);

  popd();

  if (result != "")
    return basepath + "/" + result;

  pushd(basepath);
  n = scandir(".", &namelist, selectdir, alphasort);
  popd();

  if (n < 0)
    return "";

  for (i = 0; i < n; i++)
  {
    if (result == "")
      result =
        find_deventry(basepath + "/" + string(namelist[i]->d_name), mode,
        device);
    free(namelist[i]);
  }
  free(namelist);

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

string find_deventry ( mode_t  mode,
dev_t  device 
)

Definition at line 289 of file osutils.cc.

{
  return find_deventry("/dev", mode, device);
}

Here is the call graph for this function:

string get_devid ( const string &  name)

Definition at line 296 of file osutils.cc.

{
  struct stat buf;

  if((stat(name.c_str(), &buf)==0) && (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode)))
  {
    char devid[80];

    snprintf(devid, sizeof(devid), "%u:%u", (unsigned int)MAJOR(buf.st_rdev), (unsigned int)MINOR(buf.st_rdev));
    return string(devid);
  }
  else
    return "";
}

Here is the caller graph for this function:

long get_number ( const string &  path,
long  def 
)

Definition at line 160 of file osutils.cc.

{
  string s = get_string(path, "");

  if(s=="") return def;

  return strtol(s.c_str(), NULL, 10);
}

Here is the call graph for this function:

Here is the caller graph for this function:

string get_string ( const string &  path,
const string &  def 
)

Definition at line 137 of file osutils.cc.

{
  int fd = open(path.c_str(), O_RDONLY);
  string result = def;

  if (fd >= 0)
  {
    char buffer[1024];
    size_t count = 0;

    memset(buffer, 0, sizeof(buffer));
    result = "";

    while ((count = read(fd, buffer, sizeof(buffer))) > 0)
      result += string(buffer, count);

    close(fd);
  }

  return result;
}

Here is the caller graph for this function:

string join ( const string &  j,
const string &  s1,
const string &  s2 
)

Definition at line 367 of file osutils.cc.

{
  if(s1 == "") return s2;
  if(s2 == "") return s1;

  return s1 + j + s2;
}
string kilobytes ( unsigned long long  value)

Definition at line 732 of file osutils.cc.

{
  const char *prefixes = "KMGTPEZY";
  unsigned int i = 0;
  ostringstream out;

  while ((i <= strlen(prefixes)) && ((value > 10240) || (value % 1024 == 0)))
  {
    value = value >> 10;
    i++;
  }

  out << value;
  if ((i > 0) && (i <= strlen(prefixes)))
    out << prefixes[i - 1];
  out << "iB";

  return out.str();
}

Here is the caller graph for this function:

unsigned long le_long ( const void *  from)

Definition at line 526 of file osutils.cc.

{
  __uint8_t *p = (__uint8_t*)from;

  return ((__uint32_t)(p[3]) << 24) +
    ((__uint32_t)(p[2]) << 16) +
    ((__uint32_t)(p[1]) << 8) +
    (__uint32_t)p[0];

}

Here is the caller graph for this function:

unsigned long long le_longlong ( const void *  from)

Definition at line 553 of file osutils.cc.

{
  __uint8_t *p = (__uint8_t*)from;

  return ((unsigned long long)(p[7]) << 56) +
    ((unsigned long long)(p[6]) << 48) +
    ((unsigned long long)(p[5]) << 40) +
    ((unsigned long long)(p[4]) << 32) +
    ((unsigned long long)(p[3]) << 24) +
    ((unsigned long long)(p[2]) << 16) +
    ((unsigned long long)(p[1]) << 8) +
    (unsigned long long)p[0];
}

Here is the caller graph for this function:

unsigned short le_short ( const void *  from)

Definition at line 506 of file osutils.cc.

{
  __uint8_t *p = (__uint8_t*)from;

  return ((__uint16_t)(p[1]) << 8) +
    (__uint16_t)p[0];
}

Here is the caller graph for this function:

bool loadfile ( const string &  file,
vector< string > &  list 
)

Definition at line 115 of file osutils.cc.

{
  char buffer[1024];
  string buffer_str = "";
  size_t count = 0;
  int fd = open(file.c_str(), O_RDONLY);

  if (fd < 0)
    return false;

  while ((count = read(fd, buffer, sizeof(buffer))) > 0)
    buffer_str += string(buffer, count);

  splitlines(buffer_str, list);

  close(fd);

  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

string lowercase ( const string &  s)

Definition at line 337 of file osutils.cc.

{
  string result;

  for (unsigned int i = 0; i < s.length(); i++)
    result += tolower(s[i]);

  return result;
}

Here is the caller graph for this function:

static bool matches ( string  name,
mode_t  mode,
dev_t  device 
) [static]

Definition at line 223 of file osutils.cc.

{
  struct stat buf;

  if (lstat(name.c_str(), &buf) != 0)
    return false;

  return ((S_ISCHR(buf.st_mode) && S_ISCHR(mode)) ||
    (S_ISBLK(buf.st_mode) && S_ISBLK(mode))) && (buf.st_dev == device);
}

Here is the caller graph for this function:

bool matches ( const string &  s,
const string &  pattern,
int  cflags 
)

Definition at line 376 of file osutils.cc.

{
  regex_t r;
  bool result = false;

  if(regcomp(&r, pattern.c_str(), REG_EXTENDED | REG_NOSUB | cflags) != 0)
    return false;

  result = (regexec(&r, s.c_str(), 0, NULL, 0) == 0);

  regfree(&r);

  return result;
}
int open_dev ( dev_t  dev,
const string &  name 
)

Definition at line 568 of file osutils.cc.

{
  static const char *paths[] =
  {
    "/usr/tmp", "/var/tmp", "/var/run", "/dev", "/tmp", NULL
  };
  char const **p;
  char fn[64];
  int fd;

  for (p = paths; *p; p++)
  {
    if(name=="")
      snprintf(fn, sizeof(fn), "%s/lshw-%d", *p, getpid());
    else
      snprintf(fn, sizeof(fn), "%s", name.c_str());
    if ((mknod(fn, (S_IFCHR | S_IREAD), dev) == 0) || (errno == EEXIST))
    {
      fd = open(fn, O_RDONLY);
      if(name=="") unlink(fn);
      if (fd >= 0)
        return fd;
    }
  }
  return -1;
}                                                 /* open_dev */
string operating_system ( )

Definition at line 752 of file osutils.cc.

{
  vector<string> osinfo;
  struct utsname u;
  string os = "";

  if(loadfile("/etc/lsb-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/lsb_release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/system-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/arch-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/arklinux-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/aurox-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/conectiva-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/debian_version", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/fedora-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/gentoo-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/linuxppc-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/mandrake-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/mandriva-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/novell-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/pld-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/redhat-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/slackware-version", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/sun-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/SuSE-release", osinfo))
    os = osinfo[0];
  else if(loadfile("/etc/yellowdog-release", osinfo))
    os = osinfo[0];

  if(uname(&u) != 0) return "";

  os += (os == ""?"":" ; ") + string(u.sysname)+" "+string(u.release);

#if defined(__GLIBC__) && defined(_CS_GNU_LIBC_VERSION)
  char version[PATH_MAX];

      if(confstr(_CS_GNU_LIBC_VERSION, version, sizeof(version))>0)
        os += " ; "+string(version);
#endif

  return os;
}

Here is the call graph for this function:

string platform ( )

Definition at line 815 of file osutils.cc.

{
  string p = "";
  struct utsname u;

#ifdef __i386__
  p = "i386";
#endif

  if(uname(&u) != 0)
    return p;
  else
    return p + (p!=""?"/":"") + string(u.machine);
}
string popd ( )

Definition at line 58 of file osutils.cc.

{
  string curdir = pwd();

  if (dirs.size() == 0)
    return curdir;

  if (chdir(dirs.top().c_str()) == 0)
    dirs.pop();

  return curdir;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool pushd ( const string &  dir)

Definition at line 29 of file osutils.cc.

{
  string curdir = pwd();

  if (dir == "")
  {
    if (dirs.size() == 0)
      return true;

    if (chdir(dirs.top().c_str()) == 0)
    {
      dirs.pop();
      dirs.push(curdir);
      return true;
    }
    else
      return false;
  }

  if (chdir(dir.c_str()) == 0)
  {
    dirs.push(curdir);
    return true;
  }
  else
    return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

string pwd ( )

Definition at line 72 of file osutils.cc.

{
  char curdir[PATH_MAX + 1];

  if (getcwd(curdir, sizeof(curdir)))
    return string(curdir);
  else
    return "";
}

Here is the caller graph for this function:

string readlink ( const string &  path)

Definition at line 392 of file osutils.cc.

{
  char buffer[PATH_MAX+1];

  memset(buffer, 0, sizeof(buffer));
  if(readlink(path.c_str(), buffer, sizeof(buffer)-1)>0)
    return string(buffer);
  else
    return path;
}

Here is the call graph for this function:

Here is the caller graph for this function:

string realpath ( const string &  path)

Definition at line 404 of file osutils.cc.

{
  char buffer[PATH_MAX+1];

  memset(buffer, 0, sizeof(buffer));
  if(realpath(path.c_str(), buffer))
    return string(buffer);
  else
    return path;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool samefile ( const string &  path1,
const string &  path2 
)

Definition at line 312 of file osutils.cc.

{
  struct stat stat1;
  struct stat stat2;

  if (stat(path1.c_str(), &stat1) != 0)
    return false;
  if (stat(path2.c_str(), &stat2) != 0)
    return false;

  return (stat1.st_dev == stat2.st_dev) && (stat1.st_ino == stat2.st_ino);
}

Here is the caller graph for this function:

static int selectdevice ( const struct dirent *  d) [static]

Definition at line 209 of file osutils.cc.

{
  struct stat buf;

  if (d->d_name[0] == '.')
    return 0;

  if (lstat(d->d_name, &buf) != 0)
    return 0;

  return S_ISCHR(buf.st_mode) || S_ISBLK(buf.st_mode);
}

Here is the caller graph for this function:

int selectdir ( const struct dirent *  d)

Definition at line 169 of file osutils.cc.

{
  struct stat buf;

  if (d->d_name[0] == '.')
    return 0;

  if (lstat(d->d_name, &buf) != 0)
    return 0;

  return S_ISDIR(buf.st_mode);
}

Here is the caller graph for this function:

int selectfile ( const struct dirent *  d)

Definition at line 196 of file osutils.cc.

{
  struct stat buf;

  if (d->d_name[0] == '.')
    return 0;

  if (lstat(d->d_name, &buf) != 0)
    return 0;

  return S_ISREG(buf.st_mode);
}

Here is the caller graph for this function:

int selectlink ( const struct dirent *  d)

Definition at line 183 of file osutils.cc.

{
  struct stat buf;

  if (d->d_name[0] == '.')
    return 0;

  if (lstat(d->d_name, &buf) != 0)
    return 0;

  return S_ISLNK(buf.st_mode);
}

Here is the caller graph for this function:

string spaces ( unsigned int  count,
const string &  space 
)

Definition at line 416 of file osutils.cc.

{
  string result = "";
  while (count-- > 0)
    result += space;

  return result;
}

Here is the caller graph for this function:

size_t splitlines ( const string &  s,
vector< string > &  lines,
char  separator 
)

Definition at line 83 of file osutils.cc.

{
  size_t i = 0, j = 0;
  size_t count = 0;

  lines.clear();

  while ((j < s.length()) && ((i = s.find(separator, j)) != string::npos))
  {
    lines.push_back(s.substr(j, i - j));
    count++;
    i++;
    j = i;
  }
  if (j < s.length())
  {
    lines.push_back(s.substr(j));
    count++;
  }

  return count;
}

Here is the caller graph for this function:

string tohex ( unsigned long long  n)

Definition at line 358 of file osutils.cc.

{
  char buffer[80];

  snprintf(buffer, sizeof(buffer), "%llX", n);

  return string(buffer);
}

Here is the caller graph for this function:

string tostring ( unsigned long long  n)

Definition at line 348 of file osutils.cc.

{
  char buffer[80];

  snprintf(buffer, sizeof(buffer), "%lld", n);

  return string(buffer);
}

Here is the caller graph for this function:

string uppercase ( const string &  s)

Definition at line 326 of file osutils.cc.

{
  string result;

  for (unsigned int i = 0; i < s.length(); i++)
    result += toupper(s[i]);

  return result;
}

Here is the caller graph for this function:

string utf8 ( wchar_t  c)

Definition at line 597 of file osutils.cc.

{
  string result = "";

  if (c < 0x80)
  {
    result += putchar (c);
  }
  else if (c < 0x800)
  {
    result += putchar (0xC0 | c>>6);
    result += putchar (0x80 | (c & 0x3F));
  }
  else if (c < 0x10000)
  {
    result += putchar (0xE0 | c>>12);
    result += putchar (0x80 | (c>>6 & 0x3F));
    result += putchar (0x80 | (c & 0x3F));
  }
  else if (c < 0x200000)
  {
    result += putchar (0xF0 | c>>18);
    result += putchar (0x80 | (c>>12 & 0x3F));
    result += putchar (0x80 | (c>>6 & 0x3F));
    result += putchar (0x80 | (c & 0x3F));
  }

  return result;
}

Here is the caller graph for this function:

string utf8 ( uint16_t *  s,
ssize_t  length,
bool  forcelittleendian 
)

Definition at line 627 of file osutils.cc.

{
  string result = "";
  ssize_t i;

  for(i=0; (length<0) || (i<length); i++)
    if(s[i])
      result += utf8(forcelittleendian?le_short(s+i):s[i]);
    else
      break;  // NUL found

  return result;
}

Here is the call graph for this function:

string utf8_sanitize ( const string &  s)

Definition at line 644 of file osutils.cc.

{
  unsigned int i = 0;
  unsigned int remaining = 0;
  string result = "";
  string emit = "";
  unsigned char c = 0;

  while(i<s.length())
  {
    c = s[i];
    switch(remaining)
    {
      case 3:
      case 2:
      case 1:
        if((0x80<=c) && (c<=0xbf))
        {
          emit += s[i];
          remaining--;
        }
        else         // invalid sequence (truncated)
        {
          emit = REPLACEMENT;
          emit += s[i];
          remaining = 0;
        }
        break;

      case 0:
        result += emit;
        emit = "";

        if(c<=0x7f)
          emit = s[i];
        else
        if((0xc2<=c) && (c<=0xdf)) // start 2-byte sequence
        {
          remaining = 1;
          emit = s[i];
        }
        else
        if((0xe0<=c) && (c<=0xef)) // start 3-byte sequence
        {
          remaining = 2;
          emit = s[i];
        }
        else
        if((0xf0<=c) && (c<=0xf4)) // start 4-byte sequence
        {
          remaining = 3;
          emit = s[i];
        }
        else
          emit = REPLACEMENT;      // invalid character

        break;
    }

    i++;
  }

  if(remaining == 0)
    result += emit;

  return result;
}

Here is the caller graph for this function:


Variable Documentation

stack< string > dirs [static]

Definition at line 27 of file osutils.cc.