Guest User

Untitled

a guest
Mar 9th, 2017
37
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #include<sys/mount.h>
  5. typedef struct partition {
  6. char *file_system;
  7. char *mount_point;
  8. char *type;
  9. char *options;
  10. char *dump;
  11. char *pass;
  12. }partition;
  13.  
  14.  
  15. int N=10;
  16.  
  17. partition p[10];
  18.  
  19. int printFstab()
  20. {
  21. for(int i=0;i<N;i++)
  22. {
  23.  
  24. printf("\n%d: %s",i, p[i].file_system);
  25. printf(" %d: %s",i,p[i].mount_point);
  26. printf(" %d: %s",i,p[i].type);
  27. printf(" %d: %s",i,p[i].options);
  28. printf(" %d: %s",i,p[i].dump);
  29. printf(" %d: %s",i,p[i].pass);
  30.  
  31. }
  32.  
  33.  
  34. }
  35.  
  36. int decodeFstab()
  37. {
  38.  
  39. int i = 0;
  40. int boole=0;
  41. char tmp = 0;
  42. FILE *f;
  43.  
  44. if (!(f = fopen("fstab","r"))) {
  45. fprintf(stderr,"Hiba, a fajl megnyitasanal\n");
  46. exit(1);
  47. }
  48.  
  49. char *fstab;
  50. size_t currSize;
  51. currSize = sizeof(char);
  52. fstab = (char *)malloc(currSize);
  53.  
  54. int k=0;
  55.  
  56. char * pch;
  57.  
  58.  
  59. while (!feof(f) )
  60. {
  61. fscanf(f,"%c",&tmp);
  62.  
  63. if(tmp == '#') boole=1;
  64. if(boole==1 && tmp!='\n') continue;
  65. if(boole==1 && tmp=='\n') boole=0;
  66. if(tmp=='\n' && fstab[i-1]=='\n') continue;
  67. if(tmp==' ' && fstab[i-1]==' ') continue;
  68.  
  69.  
  70. if(tmp == '\n')
  71. {
  72. pch = strtok (fstab," ");
  73. int j=0;
  74. while (pch != NULL)
  75. {
  76. switch (j){
  77. case 0:
  78. p[i].file_system=(char *)malloc(sizeof(&pch));
  79. strcpy(p[i].file_system, pch);
  80. //itt kiírja jól az értékeket. de a printFstab csak null értékeket ír ki.
  81. printf("\nP: %s,%p\nPCH: %s,%p",p[i].file_system,p[i].file_system,pch,pch);
  82. break;
  83. case 1:
  84. p[i].mount_point=(char *)malloc(sizeof(pch));
  85. strcpy(p[i].mount_point, pch);
  86. break;
  87. case 2:
  88. p[i].type=(char *)malloc(sizeof(pch));
  89. strcpy(p[i].type, pch);
  90. break;
  91. case 3:
  92. p[i].options=(char *)malloc(sizeof(pch));
  93. strcpy(p[i].options, pch);
  94. break;
  95. case 4:
  96. p[i].dump=(char *)malloc(sizeof(pch));
  97. strcpy(p[i].dump, pch);
  98. break;
  99. case 5:
  100. p[i].pass=(char *)malloc(sizeof(pch));
  101. strcpy(p[i].pass, pch);
  102.  
  103. free(fstab);
  104. currSize = sizeof(char);
  105. fstab = (char *)malloc(currSize);
  106. j=0;
  107. i=0;
  108.  
  109. break;
  110.  
  111. }
  112.  
  113. pch = strtok (NULL, " ");
  114. j++;
  115.  
  116. }
  117.  
  118.  
  119. }
  120.  
  121.  
  122. fstab[i] = tmp;
  123. currSize += sizeof(char);
  124. fstab = (char *)realloc(fstab,currSize);
  125.  
  126.  
  127.  
  128. i++;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135. return 0;
  136.  
  137. }
  138. int main(int argc, char *argv[]) {
  139.  
  140. decodeFstab();
  141. printFstab();
  142.  
  143. return 0;}
RAW Paste Data