要求:
1)插入u盘后,能自动识别并挂载
2)自动遍历u盘内容并分类处理
3)拷贝到本地目录(/var/jpeg 和 /var/mp3)拓展:
1)循环播放.mp3充当背景音乐
2)循环播放.jpeg图片
一、u盘自动识别挂载
二、自动遍历u盘内容并拷贝到本地目录
//usetest.h
#ifndef __USBTEST_H
#define __USBTEST_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include "link.h"
#define MAX_PATH 512
void copy_file(char *pathname,char *path);
bool path_exit(char *pathname,char last[],jpeg_linklist *jpg);
void print_file_info(char *pathname,jpeg_linklist *jpg);
void dir_order(char *pathname,jpeg_linklist *jpg);
#endif
在usbtest.c中
1)判断后缀名
bool path_exit(char *pathname,char last[],jpeg_linklist *jpg)
{
char *ptr,*fileExit;
ptr = strrchr(pathname,'.');
if(strcmp(ptr,last) == 0)
{
printf("%s exit:%s\n",last,pathname);
copy_file(pathname,last+1);
insert_list(jpg,pathname);
return true;
}
else
return false;
}
2)拷贝.mp3/.jpeg文件到/var/mp3 和 /var/jpeg中
void copy_file(char *pathname,char *path)
{
char buf[1024] = {0};
char *name,newname[MAX_PATH];
FILE *fp,*fp1;
int cnt;
fp = fopen(pathname,"a+");
if(fp == NULL)
{
printf("open test fail\n");
return;
}
sprintf(newname,"%s/%s","/vim",path); //自己定义要把文件复制到哪个路径,这里是/vim/jpeg
name = strrchr(pathname,'/')+1; //原来的文件名(不包括路径)
sprintf(newname,"%s/%s",newname,name); //复制过去的文件路径
printf("newname:%s\n",newname);
fp1 = fopen(newname,"w+");
if(fp1 == NULL)
{
printf("open test1 fail\n");
return;
}
while((cnt = fread(buf,sizeof(char),sizeof(buf),fp)) > 0)
{
fwrite(buf,sizeof(char),cnt,fp1);
printf("cnt:%d\n",cnt);
}
fclose(fp);
fclose(fp1);
}
3)遍历u盘内容
void print_file_info(char *pathname,jpeg_linklist *jpg) //获取节点信息
{
struct stat filestat;
if(stat(pathname,&filestat) == -1)
{
printf("cannot access file %s\n",pathname);
return;
}
printf("%s %8ld\n",pathname,filestat.st_size);
path_exit(pathname,".jpeg",jpg); //调用path_exit(),在遍历时找出符合.jpeg后缀的文件路径
path_exit(pathname,".mp3");
if((filestat.st_mode & S_IFMT) == S_IFDIR) //如果是目录,递归调用
dir_order(pathname,jpg);
}
void dir_order(char *pathname,jpeg_linklist *jpg)
{
DIR *dfd;
char name[MAX_PATH];
struct dirent *dp;
if((dfd = opendir(pathname)) == NULL) //打开目录
{
printf("dir_order:cannot open %s\n %s",pathname,strerror(errno));
return;
}
while((dp = readdir(dfd)) != NULL) //读取目录
{
if(strncmp(dp->d_name,".",1) == 0)
continue;
if(strlen(pathname)+strlen(dp->d_name)+2 > sizeof(name))
printf("dir_order:name %s %s too long\n",pathname,dp->d_name);
else
{
memset(name,0,sizeof(name));
sprintf(name,"%s/%s",pathname,dp->d_name);
print_file_info(name,jpg);
}
}
closedir(dfd);
}
三、循环播放.jpeg图片
创建一个单向循环链表,用于存放.jpeg的路径名
//link.h中
ifndef __LINK_H
#define __LINK_H
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct Jpeg_linklist
{
char namepath[256];
struct Jpeg_linklist *next;
}jpeg_linklist;
jpeg_linklist *create_list(char *namepath);
bool insert_list(jpeg_linklist *head,char *namepath);
void show_list(jpeg_linklist *head);
#endif
jpeg_linklist *create_list(char *namepath)
{
jpeg_linklist *head = (jpeg_linklist*)malloc(sizeof(jpeg_linklist));
if(head == NULL)
{
printf("create fail\n");
return NULL;
}
strcpy(head->namepath,namepath);
head->next = head;
return head;
}
bool insert_list(jpeg_linklist *head,char *namepath)
{
jpeg_linklist *new = create_list(namepath);
if(new == NULL)
{
printf("insert jpeg fail\n");
return false;
}
new->next = head->next;
head->next = new;
return true;
}
void show_list(jpeg_linklist *head)
{
char newname[256];
if(head == NULL)
return;
jpeg_linklist *p = head->next;
while(1) //循环播放
{
printf("list:%s\t",p->namepath);
sprintf(newname,"%s %s","xdg-open",p->namepath);
system(newname);
sleep(2);
p = p->next;
}
printf("\n");
}
四、主函数main.c
#include "usbtest.h"
#include "link.h"
int main(int argc,char *argv[])
{
jpeg_linklist *jpg = NULL;
jpg = create_list("NULL");
if(jpg == NULL)
{
printf("create jpeg list fail\n");
return -1;
}
if(argc == 1)
dir_order(".",jpg);
else
dir_order(argv[1],jpg);
show_list(jpg);
return 0;
}

该系统实现u盘插入后的自动识别挂载,遍历u盘内容并将.jpeg图片和.mp3文件分类拷贝到本地目录。同时,系统还能循环播放.jpeg图片和.mp3背景音乐。主要功能包括u盘挂载、文件拷贝和多媒体播放。
8224

被折叠的 条评论
为什么被折叠?



