当前位置:多学网学习教育电脑学习电脑基础教程电脑专业知识疯狂的代码,你想试试?

疯狂的代码,你想试试?

[08-23 20:51:46]   来源:http://www.duoxue8.com  电脑专业知识   阅读:217
疯狂的代码,你想试试?,标签:电脑基础知识,电脑入门,电脑学习,http://www.duoxue8.com
cpp 代码复制内容到剪贴板
  1. Code example for dir scanning   
  2. /*   
  3. * Includes   
  4. */    
  5. #include    
  6. #include    
  7. #include    
  8. #include    
  9. #include    
  10. #include    
  11. #include    
  12. #include    
  13. #include    
  14. #include    
  15. #include    
  16. typedef struct dirstruct{    
  17. char fname[256];    
  18. char localtime[256];    
  19. time_t time;    
  20. }DirStruct_t;    
  21. static int dirlist(const gchar *path, GList **list);    
  22. static int file_time_comparefunc(DirStruct_t *fileA, DirStruct_t *fileB);    
  23. #define FILE_PATH "/tmp/DirectoryList.txt"    
  24. /*   
  25. * First argument can be the directory that needs to be listed   
  26. * For example if we want to list all the files in Backups folder,   
  27. * ./dirlisting /mnt/pools/B/B0/Backups   
  28. * This will list all the files in sorted order in /tmp/DirectoryList.txt   
  29. */    
  30. int    
  31. main(int argc, char **argv)    
  32. {    
  33. printf("Starting the directory list");    
  34. gchar path[256];    
  35. GList *list = NULL;    
  36. GList *next = NULL;    
  37. FILE *fp = NULL;    
  38. if(argc > 1){    
  39. strncpy(path, argv[1], 256);    
  40. }else{    
  41. strncpy(path, "/usr/local/amazon", 256);    
  42. }    
  43. dirlist(path, &list);    
  44. list = g_list_sort(list, (GCompareFunc)file_time_comparefunc);    
  45. next = list;    
  46. if(next != NULL){    
  47. fp = fopen(FILE_PATH, "w");    
  48. while(next != NULL){    
  49. DirStruct_t *data = (DirStruct_t *)(next->data);    
  50. if(data != NULL){    
  51. fprintf(fp, "n %ld %s %s ", data->time, data->localtime, data->fname);    
  52. g_free(data);    
  53. }else{    
  54. printf("n Data is NULL");    
  55. }    
  56. next = g_list_next(next);    
  57. }    
  58. g_list_free(list);    
  59. fclose(fp);    
  60. }else{    
  61. printf("n List is NULL");    
  62. }    
  63. return (0);    
  64. }    
  65. static int    
  66. file_time_comparefunc(DirStruct_t *fileA, DirStruct_t *fileB)    
  67. {    
  68. if(fileA->time > fileB->time){    
  69. return 1;    
  70. }else if(fileA->time == fileB->time){    
  71. return 0;    
  72. }else {    
  73. return -1;    
  74. }    
  75. }    
  76. static int    
  77. dirlist(const gchar *path, GList **list)    
  78. {    
  79. GError *error = NULL;    
  80. struct stat statbuf;    
  81. struct tm *tm;    
  82. char datestring[256];    
  83. const gchar *fname = NULL;    
  84. GDir *dir = g_dir_open(path, 0, &error);    
  85. while((fname = g_dir_read_name(dir)) != NULL){    
  86. char *fullpath = g_strconcat(path,"/", fname, NULL);    
  87. if(stat(fullpath, &statbuf) != -1){    
  88. tm = localtime(&statbuf.st_ctime);    
  89. strftime(datestring, sizeof(datestring), nl_langinfo(D_T_FMT), tm);    
  90. gboolean isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR)? TRUE : FALSE;    
  91. time_t t = mktime(tm);    
  92. if(isdir == FALSE){    
  93. DirStruct_t *fstruct = g_try_new0(DirStruct_t, 1);    
  94. fstruct->time = t;    
  95. strncpy(fstruct->localtime, datestring, 256);    
  96. strncpy(fstruct->fname, fullpath, 256);    
  97. *list = g_list_append(*list, fstruct);    
  98. //printf("n %s %s %ld", fullpath, datestring, t);    
  99. }else{    
  100. //printf("n DIR %s %s %ld", fullpath, datestring, t);    
  101. dirlist(fullpath, list);    
  102. }    
  103. }else{    
  104. printf("n Stat error on file %s ", fname);    
  105. }    
  106. g_free(fullpath);    
  107. }    
  108. g_dir_close(dir);    
  109. return (0);    
  110. }   



疯狂的代码,你想试试? 结束。
Tag:电脑专业知识电脑基础知识,电脑入门,电脑学习电脑学习 - 电脑基础教程 - 电脑专业知识
疯狂的代码,你想试试?相关文章