Код:
#include <unistd.h>
#include <stdio.h>
typedef struct hddinfo
{
char szDevice[4];
char szMfg[64];
char szModel[64];
int nTemp;
} hddinfo;
int main( int argc, char *argv[] )
{
setuid( 0 );
hddinfo hdi[4] = {0};
FILE *fp = popen("/usr/sbin/hddtemp /dev/sd[abcd]","r");
if( NULL != fp )
{
for( int i = 0; i < 4; i++ )
fscanf( fp,"/dev/%3[^:]: %64s %64[^:]: %d°C\n",&hdi[i].szDevice, &hdi[i].szMfg, &hdi[i].szModel, &hdi[i].nTemp );
pclose( fp );
printf( "Content-type: text/html\n\n" );
printf( "<table id=\"hddtemp\">\n\t<tr>\n\t\t<th>Device</th>\n\t\t<th>Mfg</th>\n\t\t<th>Model</th>\n\t\t<th>Temp</th>\n\t</tr>\n" );
for( int i = 0; i < 4; i++ )
printf( "\t<tr>\n\t\t<td>%s</td>\n\t\t<td>%s</td>\n\t\t<td>%s</td>\n\t\t<td>%d°C</td>\n\t</tr>\n", hdi[i].szDevice, hdi[i].szMfg, hdi[i].szModel, hdi[i].nTemp );
printf( "</table>" );
}
else
printf( "Status: 400 Bad Request\nContent-type: text/html\n\n" );
return 0;
}
Вывод при запуске из командной строки как www-data
пользователь:
<table id="hddtemp">
<tr>
<th>Device</th>
<th>Mfg</th>
<th>Model</th>
<th>Temp</th>
</tr>
<tr>
<td>sda</td>
<td>WDC</td>
<td>WD2500AAKX-001CA0</td>
<td>29°C</td>
</tr>
<tr>
<td>sdb</td>
<td>WDC</td>
<td>WD20EURS-63S48Y0</td>
<td>30°C</td>
</tr>
<tr>
<td>sdc</td>
<td>WDC</td>
<td>WD20EURS-63S48Y0</td>
<td>30°C</td>
</tr>
<tr>
<td>sdd</td>
<td>WDC</td>
<td>WD20EURS-63S48Y0</td>
<td>30°C</td>
</tr>
</table>
Вывод при доступе через веб-браузер:
<table id="hddtemp">
<tr>
<th>Device</th>
<th>Mfg</th>
<th>Model</th>
<th>Temp</th>
</tr>
<tr>
<td>sda</td>
<td>WDC</td>
<td>WD2500AAKX-001CA0</td>
<td>29°C</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>0°C</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>0°C</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>0°C</td>
</tr>
</table>