You are not logged in.

#1 2015-04-07 05:25:26

castler
Member
From: 'Ull
Registered: 2015-04-06
Posts: 2

help with dirent.h

hi im new to c++ and to programming in general arch though ive been using for a few years, (finally decided i should bother to learn) anyway... To change the brightness on a laptop at the CLI you have to echo your chosen value to the brightness file through the graphics card symbolic link which is in sys/class/backlight my issue is that the symbolic link name changes with each graphics card so my question is how can i get the symbolic links name without using external tools like ls?


A task is only as large as you perceive it to be.

Offline

#2 2015-04-07 08:38:12

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: help with dirent.h

Hi castler and welcome to the Forums big_smile

I think you can use the function realpath along with dirent to retrieve the absolute path to which the symlink points to. Since you are new to programming i'll post a little example that maybe can be helpful:

#include <dirent.h>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

string getBacklightSymlink() {

  string pname = "/sys/class/backlight";

  DIR *parent = NULL;             // parent directory to search in
  struct dirent *child = NULL;    // child file or directory

  DIR *linkdir = NULL;            // symlink directory
  struct dirent *linkchild = NULL; // symlink child

  string backlight = string();
  char bl_found = false;

  // try to open the parent directory ...
  if ((parent = opendir(pname.c_str())) != NULL) 
  {
    // ... and search for the backlight symlink.
    while (((child = readdir(parent)) != NULL) && !bl_found) 
    {
      // converting the directory name to an std string
      string child_name = string(child->d_name);

      // if the child is a symlink ...
      if (child->d_type == DT_LNK)
      {
        // ... then get its full path ...
        string symlink_path = pname+"/"+child_name;

        // ... and the directory to wich it points to
        char *abs_link_path = realpath(symlink_path.c_str(), NULL); 

        // If child contains a file named 'brightness' ...
        if ((linkdir = opendir(abs_link_path)) != NULL)
        {
          while ((linkchild = readdir(linkdir)) != NULL)
          {      
            if (linkchild->d_name == "brightness");
            {
              // then we found the backlight!
              backlight = string(abs_link_path);
              bl_found = true;
              break;
            }
          }
          closedir(linkdir);
        }
      }
    }
    closedir (parent);
  }
  else
  {    
    /* ... this should not happen in a linux environment
     * since /sys/class/backlight should always exists, but
     * if it fails then we ask poletely to the user if he is
     * actually running linux ...
     */
    cerr << "Aren't you using linux?" << std::endl;
  }
  return backlight;
}

int main(void) {
  cout << getBacklightSymlink() << endl;
}

About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#3 2015-04-07 11:19:47

castler
Member
From: 'Ull
Registered: 2015-04-06
Posts: 2

Re: help with dirent.h

Thanks mauritiusdadd! I appreciate the help I shall study this in great detail over the next day and report back how I get on big_smile


A task is only as large as you perceive it to be.

Offline

Board footer

Powered by FluxBB