main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <sys/mount.h>
  9. #include "entry.h"
  10. #define MOUNT_POINT "/tmp/mountpoint/"
  11. #define CONFIG_FILE "wemlinux.cfg"
  12. const char* fstypes[]={"ext2", "ext3", "ext4", "btrfs", "fat", "vfat", "ntfs"};
  13. //#define STUB
  14. typedef struct
  15. {
  16. char device_name[16][16];
  17. char device_type[16][16];
  18. int n;
  19. } device_list_t;
  20. typedef struct
  21. {
  22. device_list_t installed;
  23. device_list_t not_installed;
  24. } config_device_t;
  25. int test_file(const char* base)
  26. {
  27. char tf[256];
  28. FILE* f = NULL;
  29. sprintf(tf,"%s/%s", base, CONFIG_FILE);
  30. f=fopen(tf, "r");
  31. if(f) fclose(f);
  32. return f!=NULL;
  33. }
  34. int testdevice(struct dirent *ent, config_device_t* cd)
  35. {
  36. char tmp[256], tmp2[256], mp[256];
  37. int i;
  38. if(!strncmp("sd", ent->d_name, 2) && strncmp("sda", ent->d_name, 3) && strlen(ent->d_name)==4 && ent->d_name[3]>='0' && ent->d_name[3]<='9')
  39. {
  40. sprintf(tmp, "/dev/%s",ent->d_name);
  41. sprintf(mp, MOUNT_POINT "%s", ent->d_name);
  42. mkdir(mp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  43. for(i=0; i<sizeof(fstypes)/sizeof(*fstypes); i++)
  44. {
  45. int res = mount(tmp, mp, fstypes[i], 0, NULL);
  46. perror("");
  47. if(!res)
  48. {
  49. int ok = test_file(mp);
  50. if(!strcmp(fstypes[i],"ntfs"))
  51. {
  52. char command[1024];
  53. sprintf(command, "ntfs-3g %s %s", tmp, mp);
  54. umount(mp);
  55. if(system(command))
  56. {
  57. printf("%s\n",command);
  58. perror("system()");
  59. }
  60. }
  61. if(ok)
  62. {
  63. strcpy(cd->installed.device_name[cd->installed.n], ent->d_name);
  64. strcpy(cd->installed.device_type[cd->installed.n], fstypes[i]);
  65. cd->installed.n++;
  66. }else
  67. {
  68. strcpy(cd->not_installed.device_name[cd->not_installed.n], ent->d_name);
  69. strcpy(cd->not_installed.device_type[cd->not_installed.n], fstypes[i]);
  70. cd->not_installed.n++;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. int filter(config_device_t *cd)
  77. {
  78. DIR *dir;
  79. struct dirent *ent;
  80. mkdir(MOUNT_POINT, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  81. if ((dir = opendir ("/dev/")) != NULL) {
  82. /* print all the files and directories within directory */
  83. while ((ent = readdir (dir)) != NULL) {
  84. if(testdevice(ent, cd))
  85. {
  86. }
  87. }
  88. closedir (dir);
  89. } else {
  90. /* could not open directory */
  91. perror ("");
  92. return EXIT_FAILURE;
  93. }
  94. }
  95. int main(int argc, char** argv)
  96. {
  97. int i;
  98. os_entries_t os;
  99. #ifndef STUB
  100. config_device_t cd;
  101. cd.installed.n=cd.not_installed.n=0;
  102. if(argc>1 && !strcmp(argv[1], "-l"))
  103. filter(&cd);
  104. printf("Installes (%d):\n", cd.installed.n);
  105. for(i=0; i<cd.installed.n; i++)
  106. printf("\t(%s,%s) : /tmp/mountpoint/%s/wemlinux.cfg\n", cd.installed.device_name[i], cd.installed.device_type[i], cd.installed.device_name[i]);
  107. printf("Non nstalles (%d):\n", cd.not_installed.n);
  108. for(i=0; i<cd.not_installed.n; i++)
  109. printf("(%s,%s)\n", cd.not_installed.device_name[i], cd.not_installed.device_type[i]);
  110. #endif
  111. os_entries_init(&os);
  112. if(cd.installed.n==1)
  113. {
  114. char file[256];
  115. sprintf(file, "/tmp/mountpoint/%s/wemlinux.cfg", cd.installed.device_name[0]);
  116. if(!os_entries_parse(&os,file))
  117. {
  118. int choice;
  119. printf("Select kernel\n");
  120. for(i=0; i<os.n; i++)
  121. {
  122. printf("%d : %s\n", i, os.entries[i].name);
  123. printf("\t%s\n", os.entries[i].location);
  124. printf("\t%s\n", os.entries[i].rootfs);
  125. printf("\t%s\n", os.entries[i].kernel);
  126. /*printf("\t%s\n", os.entries[i].initrd);
  127. printf("\t%s\n\n", os.entries[i].append);*/
  128. }
  129. printf("%d : exit\n", i);
  130. printf("Your choice ...\n");
  131. scanf("%d", &choice);
  132. if(choice>=0 && choice<os.n) os_exec(cd.installed.device_name[0], &os.entries[choice]);
  133. else return 0;
  134. }
  135. }
  136. }