I am writing a C program that calculates the remaining battery life or charge time, depending on if the battery is charging. The way I do this is by reading some files in /sys/class/power_supply/BAT0. To calculate the remaining battery life:

battery life = charge_now / current_now

To calculate the charge time:

charge time = (charge_full - charge_now) / current_now

The two formulas above yield the exact same results as the output of the acpi command. However, I noticed that sometimes, for some reason, the files charge_now, charge_full, and current_now are not present in the directory, but other files such as capacity and status still are. When this happens, I am unable to calculate the battery life or the charge time. But acpi is still able to calculate it. Why do these files sometimes disappear and how is acpi still able to calculate the battery life and charge time when that happens?

  • acpi is open source. Download the source, unpack the source, read the source.
    – waltinator
    Oct 18, 2023 at 0:09

1 Answer 1


As waltinator said in the comments, this is best answered by reading the source code of the acpi command. But where can it be found?

On Debian and related distributions, you can query for "which package owns this command?" with e.g.

dpkg-query -S /usr/bin/acpi
acpi: /usr/bin/acpi

Once you know the package name, you can find the name of the corresponding source code package and usually the URL of the project it came from:

apt info acpi
Homepage: http://sourceforge.net/projects/acpiclient

So, the project name is acpiclient and it's on Sourceforge. Clicking on the Code tab on the project webpage allows us to browse its Git repository: https://sourceforge.net/p/acpiclient/code/

The main header file for the command, acpi.h, might give a good overview of the structure of the program. On line 64, we find the prototype for the print_battery_information() function, which sounds promising...

..and that function can be found starting at line 251 of file acpi.c.

It parses a number of fields, each named to match the virtual files that may be available at /sys/class/power_supply/BAT0 (as you can verify by a more thorough read through the code). It appears that the information on remaining battery capacity can be found from multiple files:

  • remaining capacity (in milliamp-hours)
  • or charge_now (in microamp-hours)
  • or energy_now (in microwatt-hours).

Likewise, the rate of power consumption can be found as:

  • present rate (in milliamps)
  • current_now (in microamps)
  • power_now (in microwatts)

Using any of these, the remaining runtime on battery (at the current consumption level) can be calculated, after applying any appropriate unit conversions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .