/*
** FILE: uptime.c
** NOTE: 2015-09-23 created by Jack Liu
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <utmp.h>
#include "uptime.h"
int
get_uptime( double *dpUT )
{
FILE *fp = NULL;
double dIdleTime = 0;
if( ( fp = fopen( UPTIME_FILE, "r" ) ) == NULL )
{
fprintf( stderr, "fopen error\n" );
return -1;
}
if( fscanf( fp, "%lf %lf", dpUT, &dIdleTime ) != 2 )
{
fprintf( stderr, "fscanf error\n" );
return -1;
}
fclose( fp );
return 0;
}
int
get_avgload( double *dpOneAL, double *dpFivAL, double *dpFifAL )
{
FILE *fp = NULL;
if( ( fp = fopen( AVGLOAD_FILE, "r" ) ) == NULL )
{
fprintf( stderr, "fopen error\n" );
return -1;
}
if( fscanf( fp, "%lf %lf %lf", dpOneAL, dpFivAL, dpFifAL ) != 3 )
{
fprintf( stderr, "fscanf error\n" );
return -1;
}
fclose( fp );
return 0;
}
int
get_userno( int *iUsers )
{
int fd = -1;
struct utmp stEntry;
int iEntSize = sizeof( struct utmp );
int iRdNum = 0;
*iUsers = 0;
if( ( fd = open( UTMP_FILE, O_RDONLY ) ) < 0 )
{
fprintf( stderr, "open error\n" );
return -1;
}
while( ( iRdNum = read( fd, &stEntry, iEntSize ) ) == iEntSize )
{
if( stEntry.ut_type == USER_PROCESS )
++( *iUsers );
}
if( iRdNum < 0 )
{
close( fd );
fprintf( stderr, "read error\n" );
return -1;
}
close( fd );
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
uptime指令的简单实现
共7个文件
h:2个
c:2个
o:1个
需积分: 36 30 下载量 176 浏览量
2015-09-24
17:09:14
上传
评论
收藏 17KB RAR 举报
温馨提示
linux下模拟C语言uptime指令的简单实现,主要是通过读取系统文件信息实现
资源推荐
资源详情
资源评论
收起资源包目录
UPTIME.rar (7个子文件)
UPTIME
uptime1
myuptime 17KB
src
uptime.c 1KB
myuptime.c 1006B
Makefile 430B
obj
uptime.o 9KB
include
uptime.h 715B
mydef.h 408B
共 7 条
- 1
资源评论
qq1223386926
- 粉丝: 9
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功