- 论坛徽章:
- 0
|
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <pwd.h>
- #include <time.h>
- #include <grp.h>
- #include <mysql/mysql.h>
- #define N 7
- MYSQL my_sql;
- DIR * my_dir = NULL;
- char *args[N] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL};
- void read_dir(char *);
- void connect_mysql();
- void set_work();
- void get_file_infor(const char *,char *);
- void write_mysql();
- int main(int argc,char *argv[])
- {
- if(argc != 2)
- {
- fprintf(stderr,"arguments is not two!\n");
- return -1;
- }else
- {
- connect_mysql();
- set_work();
- read_dir(argv[1]);
- }
- mysql_close(&my_sql);
- return EXIT_SUCCESS;
- }
-
- void read_dir(char *dirname)
- {
- struct dirent * dir;
- int n = 0;
- my_dir = opendir(dirname);
- if(!my_dir)
- {
- fprintf(stderr,"open dir fail %s\n",strerror(errno));
- }else
- {
-
- while((dir = readdir(my_dir)))
- {
- if(strcmp(".",dir->d_name)&&strcmp("..",dir->d_name))
- {
- for(;n < N;n++)
- {
- args[n] = (char *)malloc(256);
- if(!args[n])
- {
- fprintf(stderr,"malloc fail\n");
- closedir(my_dir);
- exit(-1);
- }
- }
- args[0] = dir->d_name;
- get_file_infor(dir->d_name,dirname);
- printf("\n");
- }
-
- }
-
-
- }
- }
-
- void connect_mysql()
- {
- mysql_init(&my_sql);
- if(mysql_real_connect(&my_sql,"localhost","ma","123456ma",
- "student",0,0,0))
- {
- printf("connect mysql success\n");
- }else{
- fprintf(stderr,"connect mysql fail %s",mysql_error(&my_sql));
- exit(-1);
- }
- }
-
- void set_work()
- {
- int res1,res2;
- res1 =mysql_query(&my_sql,"set names utf8");
- res2 = mysql_query(&my_sql,"use student");
- if(res1)
- {
- fprintf(stderr,"set character fail! %s\n",mysql_error(&my_sql));
- }else
- printf("set character success !\n");
- if(res2)
- {
- fprintf(stderr,"use student database fail ! %s\n",mysql_error(&my_sql));
- }else
- printf("use database success !\n");
-
- }
-
- void write_mysql()
- {
- printf("\t#######start write into mysql#######\n");
- int res ;
- char * infor = (char *)malloc(512);
- memset(infor,0,512);
- sprintf(infor,"INSERT INTO File(filename,filemode,filelink,fileown,filegroup,filetime,filesize) VALUES ('%s','%s',%s,'%s','%s','%s',%s)",args[0],args[1],args[2],args[3],args[4],args[5],args[6]);
- infor[strlen(infor)] = '\0';
- // printf("%s\n",infor);
- res = mysql_query(&my_sql,infor);
- if(!res)
- printf("Insert %lu rows\n",(unsigned long)mysql_affected_rows(&my_sql));
- else
- fprintf(stderr,"Insert data fail %s\n",mysql_error(&my_sql));
-
- }
-
- void get_file_infor(const char *path1,char *arg)
- {
- struct stat filestat;
- int n = 0;
- char *path = (char *)malloc(256);
- memset(path,0,256);
- sprintf(path,"%s/%s",arg,path1);
- if(lstat(path,&filestat))
- {
- fprintf(stderr,"get inode information fail %s\n",strerror(errno));
- return;
- }else
- {
- switch(filestat.st_mode & S_IFMT)
- {
- case S_IFBLK: args[1] = "block";break;
- case S_IFCHR: args[1] = "character";break;
- case S_IFDIR: args[1] = "directory";break;
- case S_IFIFO: args[1] = "FIFO/pipe";break;
- case S_IFLNK: args[1] = "symlink";break;
- case S_IFREG: args[1] = "regularfile";break;
- case S_IFSOCK: args[1] = "socket";break;
- default: args[1] = "unknown";
-
- }
- sprintf(args[2],"%ld ",(unsigned long)filestat.st_nlink);
- struct passwd * passwd = getpwuid(filestat.st_uid);
- sprintf(args[3],"%s ",passwd->pw_name);
- struct group * group = getgrgid(filestat.st_gid);
- sprintf(args[4],"%s ",group->gr_name);
- struct tm *filetime = gmtime(&(filestat.st_mtime));
- sprintf(args[5],"%d/%d/%d",filetime->tm_year+1900,filetime->tm_mon,filetime->tm_mday);
- sprintf(args[6],"%lld",filestat.st_size);
- write_mysql();
- }
-
- }
复制代码 |
|