How to Write a Program in C to detect the Ethernet Cable?

This is a discussion on How to Write a Program in C to detect the Ethernet Cable? within the C Programming forums, part of the General Programming Boards category; I wish to write a C program to detect whether the Ethernet cable is plugged or unplugged. I found out ...

How to Write a Program in C to detect the Ethernet Cable?

I wish to write a C program to detect whether the Ethernet cable is plugged or unplugged. I found out by using a command "nm-tool" in Linux terminal will show me whether a Ethernet cable is plugged or not. If Ethernet cable is plugged, in the device part of eth0, the Hardware Link of Wired Settings will indicate a "yes" and "no" if no Ethernet cable.

Hence, in my previous code, I use one function called popen to read the state as shown below:

However, now my project wish to not use the NetworkManager (where the "nm-tool" command comes from) because I have to set a static IP address permanently for eth0 and once I set the static IP, nm-tool cannot function. And this gives me trouble to detect the Ethernet cable. So is there any other method for me to detect the Ethernet cable in C programming?

I'm very convinced that attempting to do this in C is the wrong thing. There is a high likelihood that there is an existing published mechanism for detecting whether the cable is connected or not.

Edit: I believe (from some googling) that /sys/devices/... will have the information you need (where .... is the appropriate path to your network device). The information in /sys/devices is in text-form, so tools like grep will find what you want.