如何添加一个命令来检查linux shell中的电池级别?

时间:2022-07-24 21:46:06

I'm writing a mini UNIX shell, which supports the built-in UNIX commands and also some custom ones. I need to check the battery level inside my C-shell code in the style of

我正在编写一个迷你UNIX shell,它支持内置的UNIX命令和一些定制的命令。我需要检查一下我的C-shell代码中的电池级别。

if (strcmp("BatteryLevel", commandArgv[0]) == 0) {

                printf("The battery level is ",);
                return 1;
        }  

I've written the chunk of the shell, all the parsing and built-in commands are working. I also know how to check the battery level from terminal(https://askubuntu.com/questions/69556/how-to-check-battery-status-using-terminal), but i can't get my head around how i can do this inside the code. Thanks for any help.

我已经编写了shell的大部分,所有的解析和内置命令都在工作。我还知道如何从终端(https://askubuntu.com/questions/69556/how to check- batteries - state - use -terminal)检查电池电量,但我不知道如何在代码中这么做。感谢任何帮助。

1 个解决方案

#1


3  

For 3.4.NN kernels current battery charge level and the maximum it can reach are available in /sys/class/power_supply/BAT* (usually BAT0 as you normally have just one battery) in files charge_now and charge_full. So something along the lines of following should meet your needs.

为3.4。NN内核当前的电池充电水平和它能达到的最大限度可以在/sys/class/power_supply/BAT*(通常是BAT0,因为您通常只有一个电池)文件charge_now和charge_full中找到。因此,下面的一些事情应该满足你的需要。

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <linux/limits.h>
#include <regex.h>

#define _DATADIR "/sys/class/power_supply"

int main(int argc, char **argv) {
  FILE *f_c, *f_f;
  long current, full;
  DIR *d;
  struct dirent *dp;
  char b[PATH_MAX]; 

  if((d = opendir(_DATADIR)) == NULL) {
    fprintf(stderr, "opendir: %s\n", strerror(errno));
    return 3;
  }

  while((dp = readdir(d)) != NULL) {
    snprintf(b, PATH_MAX, "%s/%s", _DATADIR, dp->d_name);

    regex_t regex;
    if(regcomp(&regex, "BAT[[:alnum:]]+", REG_EXTENDED) != 0) {
      fprintf(stderr, "regcomp: %s\n", strerror(errno));
      return 4;
    }
    if(regexec(&regex, b, 0, NULL, 0) == 0) {
      snprintf(b, PATH_MAX, "%s/%s/%s", _DATADIR, dp->d_name, "charge_now");
      f_c = fopen(b, "r");
      snprintf(b, PATH_MAX, "%s/%s/%s", _DATADIR, dp->d_name, "charge_full");
      f_f = fopen(b, "r");
      if(f_c != NULL && f_f != NULL) {
        if(fscanf(f_c, "%ld", &current) != 1 || fscanf(f_f, "%ld", &full) != 1)
          fprintf(stderr, "fscanf: %s\n", strerror(errno));
        else
          fprintf(stdout, "charge for %s %.2f\n", dp->d_name,
                  (current / full) * 100.0);
        fclose(f_c);
        fclose(f_f);
      }
    }
    regfree(&regex);
  }

  return 0;
}

#1


3  

For 3.4.NN kernels current battery charge level and the maximum it can reach are available in /sys/class/power_supply/BAT* (usually BAT0 as you normally have just one battery) in files charge_now and charge_full. So something along the lines of following should meet your needs.

为3.4。NN内核当前的电池充电水平和它能达到的最大限度可以在/sys/class/power_supply/BAT*(通常是BAT0,因为您通常只有一个电池)文件charge_now和charge_full中找到。因此,下面的一些事情应该满足你的需要。

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <linux/limits.h>
#include <regex.h>

#define _DATADIR "/sys/class/power_supply"

int main(int argc, char **argv) {
  FILE *f_c, *f_f;
  long current, full;
  DIR *d;
  struct dirent *dp;
  char b[PATH_MAX]; 

  if((d = opendir(_DATADIR)) == NULL) {
    fprintf(stderr, "opendir: %s\n", strerror(errno));
    return 3;
  }

  while((dp = readdir(d)) != NULL) {
    snprintf(b, PATH_MAX, "%s/%s", _DATADIR, dp->d_name);

    regex_t regex;
    if(regcomp(&regex, "BAT[[:alnum:]]+", REG_EXTENDED) != 0) {
      fprintf(stderr, "regcomp: %s\n", strerror(errno));
      return 4;
    }
    if(regexec(&regex, b, 0, NULL, 0) == 0) {
      snprintf(b, PATH_MAX, "%s/%s/%s", _DATADIR, dp->d_name, "charge_now");
      f_c = fopen(b, "r");
      snprintf(b, PATH_MAX, "%s/%s/%s", _DATADIR, dp->d_name, "charge_full");
      f_f = fopen(b, "r");
      if(f_c != NULL && f_f != NULL) {
        if(fscanf(f_c, "%ld", &current) != 1 || fscanf(f_f, "%ld", &full) != 1)
          fprintf(stderr, "fscanf: %s\n", strerror(errno));
        else
          fprintf(stdout, "charge for %s %.2f\n", dp->d_name,
                  (current / full) * 100.0);
        fclose(f_c);
        fclose(f_f);
      }
    }
    regfree(&regex);
  }

  return 0;
}