Posted on 2008-08-12 22:10
Prayer 阅读(293)
评论(0) 编辑 收藏 引用 所属分类:
Shell
C文件如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sqlite3.h>
int main( void ){
printf("Content-Type:text/html;charset=UTF-8\n\n");
float diskFloat;
char disk[1024]="";
FILE *stream;
memset( disk, '\0', sizeof(disk) );//初始化buf,以免后面写入乱码到文件中
stream = popen( "/mnt/bin/disk.sh", "r" ); //将“/mnt/disk”命令的输出 通过管道读取(“r”参数)到FILE* stream
fread( disk, sizeof(char), sizeof(disk), stream); //将刚刚FILE* stream的数据流读取到disk中
char sum[50],used[50],available[50],percent[50];
printf( "%s" , "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" );
printf( "%s" , "<html>" );
printf( "%s" , "<head>" );
printf( "%s" , "<title></title>" );
printf( "<link rel='stylesheet' type='text/css' href='../css/main.css'>" );
//检测用户权限
printf( "%s" , "<script type=\"text/javascript\" src=\"../javascript/cookie.js\"></script>" );
printf( "%s" , "<script language=\"javascript\" type=\"text/javascript\">\n" );
printf( "%s" , "userPermission(1);\n" );
printf( "%s" , "</script>\n" );
printf( "%s" , "</head>" );
printf( "%s" , "<body>" );
printf( "%s", "<table border='0' cellpadding='3' cellspacing='0' width='100%'>" );
printf( "%s" , "<tr bgcolor='#6d88ad' class='textHeaderDark'>" );
printf( "%s", "<th align='left' colspan=\"2\">硬盘使用情况:</th>" );
printf( "%s", "</tr>" );
printf( "%s" , "<tr bgColor=\"f5f5f5\">" );
printf( "%s" , "<td>" );
if( strcmp( disk , "fail\n" ) == 0 ){
printf( "%s" , "<center><font color='red'>硬盘没有挂载!</font><center>");
}else{
strcpy( sum , strtok( disk , "," ) );
strcpy( used , strtok( NULL , "," ) );
strcpy( available , strtok( NULL , "," ) );
strcpy( percent , strtok( NULL , "," ) );
diskFloat = atoi( sum )/1024.0/1024;
printf( "硬盘总容量:%.2f G" , diskFloat );
diskFloat = atoi( used )/1024.0/1024;
printf( "; 已使用:%.2f G" , diskFloat );
diskFloat = atoi( available )/1024.0/1024;
printf( "; 未使用:%.2f G" , diskFloat );
printf( "; 已使用百分比:%s" , percent );
printf( "%s" , ";" );
}
printf( "%s" , "</td>" );
printf( "%s" , "</tr>" );
printf( "%s" , "</table>" );
printf( "%s" , "</body>" );
printf( "%s" , "</html>" );
pclose( stream );
return 0 ;
}
shell文件如下:
#!/bin/sh
disk=`df|grep disk`
if [ "$disk" = '' ] ; then
echo "fail
else
sum=`df|grep disk|awk '{print $2"," $3"," $4"," $5}'`
echo $sum
fi