小板插入sd卡,U盘,udevd根据匹配规则自动把设备mount上后,终端执行df,得到如下
/myscript $ df
Filesystem 1k-blocks Used Available Use% Mounted on
none 30600 8 30592 0% /tmp
/dev/sda1 128116 101547 26569 79% /mnt/usb/dev/mmcblk0 985328 314944 670384 32% /mnt/sd
我想作的时,当设备mount好后,我通过执行df > /usr/mylog
把df的执行结果打到一个文件,读取文件即可。
执行
char *argv[] = {"df", ">", "df > /usr/mylog", (char *)0};
execv("/bin/df",argv);
不行,好像不识别>,执行前面类似shell的函数do_exec("df > /usr/mylog");也不行,错误一样。好像都是重定向那块出问题了。单独运行do_exec("df");是没问题的 。
查了一些资料,解决了。
void echo_devices_used_to_file(void)
{
pid_t pid;
pid = fork();
switch (pid) {
case 0:
{
char *argv[] = {"df",NULL};
char outfname[20]="/button/mylog";
FILE *outf;
outf=freopen(outfname,"w",stdout); //
将终端中的输出重定向到文件outfname里面 execv("/bin/df",argv);
break;
}
case -1:
printf("fork fail\n");
break;
default:
break;
}
}
再后来,发现没必要那么复杂,把上面代码改了,只需一个简单的系统调用就可以实现了,当然得包括相应的头文件了。如下
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/* for select */
#include
#include
#include
/* for shmget, shmat */
#include
#include
#include
/* for stat */
#include
#include
#include
void echo_devices_used_to_file(void)
{
const char *cmd_connect ="df > /button/mylog";
system (cmd_connect);
return 0;
}
以下2个为操作文件比较有用的函数
static inline char *skip_ws(const char *p) //跳过空格
{
while (isspace(*p)) p++;
return (char *)p;
}
static inline char *skip_token(const char *p) //跳过当前空格和空格紧接着的字符,如 128116
{
while (isspace(*p)) p++;
while (*p && !isspace(*p)) p++;
return (char *)p;
}
gboolean read_device_used (gchar * fname,char *device_name,unsigned int *total_size,unsigned int *used_size,unsigned int *available_size,unsigned int *used_percent)
{
GIOChannel * io = NULL;
GIOStatus status = G_IO_STATUS_NORMAL;
int i = 0;//used fot skip two lines of ahead
g_assert(fname);
io = g_io_channel_new_file(fname, "r", NULL);
if (io == NULL)
{
g_print("field_template_parse_from_file(): file NOT exists.[%s]\n", fname);
return FALSE;
}
do
{
gchar * buffer = NULL;
gchar ** strs = NULL;
gint i;
status = g_io_channel_read_line(io, &buffer, NULL, NULL, NULL);//读取文件的一行
if (buffer == NULL || strlen(buffer) == 0)
{
continue;
}
if( i<2 )
{
i++;
continue;
}
if( !strncmp(buffer,device_name,strlen(device_name)) )
{
unsigned int tmp = 0;
char *p = NULL;
p = skip_token(buffer); //如跳过/dev/sda1
tmp = strtoul(p, &p, 0);//128116 ,转成10进制并付给tmp
*total_size = tmp;
tmp = strtoul(p, &p, 0);//101547
*used_size = tmp;
tmp = strtoul(p, &p, 0);//26569
*available_size = tmp;
tmp = strtoul(p, &p, 0);//79
*used_percent = tmp;
}
g_strfreev(strs);
g_free(buffer);
} while (status == G_IO_STATUS_NORMAL);
g_io_channel_shutdown(io, TRUE, NULL);
g_io_channel_unref(io);
return TRUE;
}
void device_used(void)
{
unsigned int total_size = 0;
unsigned int used_size = 0;
unsigned int available_size = 0;
unsigned int used_percent = 0;
char size_total[100];
char has_used[100];
char free_size[100];
char per[100];
char device_name[30];
echo_devices_used_to_file();
if(device_index == 0)//usb
{
sprintf(device_name,"/dev/%s",usb_name);
}
else if(device_index == 4) //sd
{
sprintf(device_name,"/dev/%s",sd_name);
}
printf("device_name = %s.........\n",device_name);
read_device_used("/button/mylog",device_name,&total_size,&used_size,&available_size,&used_percent);
printf("..........total_size = %d ..............\n",total_size);
printf("........used_size = %d ..............\n",used_size);
}
阅读(1817) | 评论(0) | 转发(0) |