linux在u盘创建文件,当一个U盘插入linux系统后,自动在其上面创建music document文件...

1 /*

2 *-----------------------------------------3 * Name : auto_mkdir_on_u.c4 * Author : jason5 * Email : boyisgood86@gmail.com6 *7 * Function : When the U disk into the linux system,automatically to8 * creat music and document directory on it.If them existing,do it not.9 *10 *-----------------------------------------11 */

12 #include

13 #include

14 #include

15 #include

16 #include

17 #include

18 #include

19 #include

20 #include

21 #include

22 #include

23 #include

24

25 #define UEVENT_BUFFER_SIZE 2048

26 #define mtab_file (access("/proc/mounts",F_OK)?"/proc/mounts":"/etc/mtab")

27

28 char * compale(const char *buf);29 char * search_path(const char *com_rec);30 int Mkdir(const char *sea_rec);31

32 int main(void)33 {34 structsockaddr_nl client;35 structtimeval tv;36 char * com_rec =NULL;37 char * sea_rec =NULL;38 intnetLink_fd, rcvlen, ret;39 fd_set fds;40 int buffersize = 1024;41

42 netLink_fd =socket(AF_NETLINK, SOCK_RAW, NETLINK_KOBJECT_UEVENT);43

44 memset(&client, 0, sizeof(client));45 client.nl_family =AF_NETLINK;46 client.nl_pid =getpid();47 client.nl_groups = 1; /*receive broadcast message*/

48

49 //set options on sockets

50 setsockopt(netLink_fd, SOL_SOCKET, SO_RCVBUF, &buffersize, sizeof(buffersize));51

52 //bind

53 bind(netLink_fd, (struct sockaddr*)&client, sizeof(client));54

55 while (1) {56 char buf[UEVENT_BUFFER_SIZE] = { 0};57

58 FD_ZERO(&fds); //clean

59 FD_SET(netLink_fd, &fds); //write 1 on bit netLink_fd

60

61 tv.tv_sec = 0;62 tv.tv_usec = 100 * 1000;63

64 ret = select(netLink_fd + 1, &fds, NULL, NULL, &tv);65 if(ret < 0)66 continue;67 if(!(ret > 0 && FD_ISSET(netLink_fd, &fds)))68 continue;69 /*

70 * receive data71 * There is a buf space,many important info in it.If you want to knonw more, * you can use code like this:72 * printf("buf = %s\n",buf);73 * and do it that you like.74 * detail please man recv or man 2 recv.75 *76 */

77 rcvlen = recv(netLink_fd, &buf, sizeof(buf), 0);78 if (rcvlen > 0) {79 if((com_rec = compale(buf)) !=NULL){80 sleep(2);81 printf("com_rec = %s\n",com_rec);82

83 sea_rec =search_path(com_rec);84 if(sea_rec !=NULL){85 printf("sea_rec = %s\n",sea_rec);86 if(Mkdir(sea_rec) < 0){87 printf("create Failed!\n");88 }else{89 printf("create OK !\n");90 }91 }92 }93 }94 else{95 printf("error:%s\n",strerror(errno));96 }97 }98 close(netLink_fd);99 return 0;100 }101

102 /*find /dev/sdx*/

103 char * compale(const char *buf)104 {105 const char *s1 = "add@";106 const char *s2 = "/block/sd";107 char *p =NULL;108

109 if(strstr(buf,s1) !=NULL){110 if((p = strstr(buf,s2)) !=NULL){111 returnp;112 }113 }114 returnNULL;115 }116

117 /*find systemMount point*/

118 char * search_path(const char *com_rec)119 {120 /*com_rec = /block/sdb or /block/sdb1*/

121

122 struct mntent *mnt;123 FILE *file =NULL;124 int i = 0;125 const char * p =com_rec;126 //char * retu_value = NULL;

127 for(i = 0;i < 6;i++){128 p++;129 }130 printf("search_path p = %s\n",p);131

132 file = setmntent(mtab_file,"r");133 if(!file){134 printf("error:%s\n",strerror(errno));135 //return NULL;

136 }137 while((mnt =getmntent(file))){138 //printf("Driver %s,name %s,type %s,opt %s\n",mnt->mnt_dir,mnt->mnt_fsname,mnt->mnt_type,mnt->mnt_opts);139 //printf("mnt->mnt_fsname = %s\n",mnt->mnt_fsname);

140 if(strstr(mnt->mnt_fsname,p) !=NULL){141 printf("here!\n");142 endmntent(file);143 return (char *)mnt->mnt_dir;144 }145

146 //retu_value = strstr(mnt->mnt_fsname,p);147 //printf("retu_value = %s\n",retu_value);

148 }149 endmntent(file);150 returnNULL;151 }152

153 /*find music and document dirctory existing or not*/

154 int Mkdir(const char *sea_rec)155 {156 const char * const music_s = "/music";157 const char * const document_s = "/document";158 //char * p_rec = sea_rec;

159

160 char * p_music = (char*)malloc(sizeof(char)*128);161 if(NULL ==p_music){162 perror("p_music");163 exit(1);164 }165 memset(p_music,0,sizeof(char)*128);166 p_music =strcpy(p_music,sea_rec);167

168 char * p_document = (char*)malloc(sizeof(char)*128);169 if(NULL ==p_document){170 perror("p_document");171 exit(1);172 }173 memset(p_document,0,sizeof(char)*128);174 p_document =strcpy(p_document,sea_rec);175

176 printf("p_music = %s\n",p_music);177 printf("p_document = %s\n",p_document);178 //auto create music dirctory on disk

179 p_music =strcat(p_music,music_s);180 if(access(p_music,F_OK) < 0){181 printf("No music\n");182 perror("access!");183 if(mkdir(p_music,S_IRWXU) < 0){184 perror("mkdir");185 //return -1;

186 }187 }else{188 printf("music existing!\n");189 }190

191 printf("p_music = %s\n",p_music);192 printf("p_document = %s\n",p_document);193

194 //auto create document dirctory on disk

195 p_document =strcat(p_document,document_s);196 if(access(p_document,F_OK) < 0){197 printf("No document\n");198 perror("access");199 if(mkdir(p_document,S_IRWXU) < 0){200 perror("mkdir");201 //return -1;

202 }203 }else{204 printf("document existing !\n");205 }206

207 free(p_music);208 free(p_document);209 return 0;210 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值