예제 #1
0
파일: Battery.c 프로젝트: 520lly/htop
static unsigned long int parseBatInfo(const char *fileName, const unsigned short int lineNum, const unsigned short int wordNum) {
   const char batteryPath[] = PROCDIR "/acpi/battery/";
   DIR* batteryDir = opendir(batteryPath);
   if (!batteryDir)
      return 0;

   #define MAX_BATTERIES 64
   char* batteries[MAX_BATTERIES];
   unsigned int nBatteries = 0;
   memset(batteries, 0, MAX_BATTERIES * sizeof(char*));

   struct dirent result;
   struct dirent* dirEntry;
   while (nBatteries < MAX_BATTERIES) {
      int err = readdir_r(batteryDir, &result, &dirEntry);
      if (err || !dirEntry)
         break;
      char* entryName = dirEntry->d_name;
      if (strncmp(entryName, "BAT", 3))
         continue;
      batteries[nBatteries] = xStrdup(entryName);
      nBatteries++;
   }
   closedir(batteryDir);

   unsigned long int total = 0;
   for (unsigned int i = 0; i < nBatteries; i++) {
      char infoPath[30];
      snprintf(infoPath, sizeof infoPath, "%s%s/%s", batteryPath, batteries[i], fileName);

      FILE* file = fopen(infoPath, "r");
      if (!file) {
         break;
      }

      char line[50] = "";
      for (unsigned short int i = 0; i < lineNum; i++) {
         char* ok = fgets(line, sizeof line, file);
         if (!ok) break;
      }

      fclose(file);

      char *foundNumStr = String_getToken(line, wordNum);
      const unsigned long int foundNum = atoi(foundNumStr);
      free(foundNumStr);

      total += foundNum;
   }

   for (unsigned int i = 0; i < nBatteries; i++) {
      free(batteries[i]);
   }

   return total;
}
예제 #2
0
파일: Battery.c 프로젝트: 520lly/htop
static ACPresence procAcpiCheck() {
   ACPresence isOn = AC_ERROR;
   const char *power_supplyPath = PROCDIR "/acpi/ac_adapter";
   DIR *dir = opendir(power_supplyPath);
   if (!dir) {
      return AC_ERROR;
   }

   struct dirent result;
   struct dirent* dirEntry;
   for (;;) {
      int err = readdir_r((DIR *) dir, &result, &dirEntry);
      if (err || !dirEntry)
         break;

      char* entryName = (char *) dirEntry->d_name;

      if (entryName[0] != 'A')
         continue;

      char statePath[50];
      snprintf((char *) statePath, sizeof statePath, "%s/%s/state", power_supplyPath, entryName);
      FILE* file = fopen(statePath, "r");

      if (!file) {
         isOn = AC_ERROR;
         continue;
      }

      char line[100];
      char* ok = fgets(line, sizeof line, file);
      if (!ok) continue;
      line[sizeof(line) - 1] = '\0';

      fclose(file);

      const char *isOnline = String_getToken(line, 2);

      if (strcmp(isOnline, "on-line") == 0) {
         isOn = AC_PRESENT;
      } else {
         isOn = AC_ABSENT;
      }
      free((char *) isOnline);
      if (isOn == AC_PRESENT) {
         break;
      }
   }

   if (dir)
      closedir(dir);
   return isOn;
}
예제 #3
0
파일: BatteryMeter.c 프로젝트: kAworu/stop
static unsigned long int parseBatInfo(const char *fileName, const unsigned short int lineNum, const unsigned short int wordNum) {
   const DIR *batteryDir;
   const struct dirent *dirEntries;

   const char batteryPath[] = PROCDIR "/acpi/battery/";
   batteryDir = opendir(batteryPath);

   if (batteryDir == NULL) {
      return 0;
   }

   char *entryName;
   typedef struct listLbl {
      char *content;
      struct listLbl *next;
   } list;

   list *myList = NULL;
   list *newEntry;

   /*
      Some of this is based off of code found in kismet (they claim it came from gkrellm).
      Written for multi battery use...
    */
   for (dirEntries = readdir((DIR *) batteryDir); dirEntries; dirEntries = readdir((DIR *) batteryDir)) {
      entryName = (char *) dirEntries->d_name;

      if (strncmp(entryName, "BAT", 3))
         continue;

      newEntry = calloc(1, sizeof(list));
      newEntry->next = myList;
      newEntry->content = entryName;
      myList = newEntry;
   }

   unsigned long int total = 0;
   for (newEntry = myList; newEntry; newEntry = newEntry->next) {
      const char infoPath[30];
      const FILE *file;
      char line[50];

      snprintf((char *) infoPath, sizeof infoPath, "%s%s/%s", batteryPath, newEntry->content, fileName);

      if ((file = fopen(infoPath, "r")) == NULL) {
         return 0;
      }

      for (unsigned short int i = 0; i < lineNum; i++) {
         fgets(line, sizeof line, (FILE *) file);
      }

      fclose((FILE *) file);

      const char *foundNumTmp = String_getToken(line, wordNum);
      const unsigned long int foundNum = atoi(foundNumTmp);
      free((char *) foundNumTmp);

      total += foundNum;
   }

   free(myList);
   free(newEntry);
   closedir((DIR *) batteryDir);
   return total;
}