Advertisement
tkamiten

pset6 load changed and lookup, indexes

Jun 4th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. const char* lookup(const char* path)
  2. {
  3. // char pointer to find dot
  4. char* dot_ptr;
  5.  
  6. // array to store file type extension
  7. char ext[6];
  8.  
  9. int i = 0;
  10.  
  11. dot_ptr = strrchr(path,'.');
  12.  
  13. do
  14. {
  15. ext[i] = *dot_ptr;
  16. i++;
  17. dot_ptr++;
  18. }while(*dot_ptr != '\0');
  19.  
  20. ext[i] = '\0';
  21.  
  22. if(strcasecmp(".css", ext) == 0)
  23. return "text/css";
  24. else if(strcasecmp(".html", ext) == 0)
  25. return "text/html";
  26. else if(strcasecmp(".gif", ext) == 0)
  27. return "image/gif";
  28. else if(strcasecmp(".ico", ext) == 0)
  29. return "image/x-icon";
  30. else if(strcasecmp(".jpg", ext) == 0)
  31. return "image/jpeg";
  32. else if(strcasecmp(".js", ext) == 0)
  33. return "text/javascript";
  34. else if(strcasecmp(".php", ext) == 0)
  35. return "text/x-php";
  36. else if(strcasecmp(".png", ext) == 0)
  37. return "image/png";
  38. else
  39. return NULL;
  40. }
  41.  
  42. bool load(FILE* file, BYTE** content, size_t* length)
  43. {
  44.  
  45. int counter = 0;
  46.  
  47. BYTE* ptr;
  48.  
  49. ptr = (BYTE *)malloc(sizeof(BYTE));
  50.  
  51. while(fread(ptr, sizeof(BYTE), 1, file) == 1)
  52. {
  53. counter++;
  54. ptr = (BYTE *)realloc(ptr,sizeof(BYTE)*(counter + 1));
  55.  
  56. }
  57.  
  58. *content = ptr;
  59. *length = counter;
  60. return true;
  61. }
  62.  
  63. char* indexes(const char* path)
  64. {
  65. int res_p; // store access return integer
  66. int res_h;
  67.  
  68. char* php = NULL;
  69. char* html= NULL;
  70.  
  71. strcpy(php, path);
  72. strcat(php, "index.php");
  73. const char* php_c =(const char*)php;
  74.  
  75. strcpy(html, path);
  76. strcat(html, "index.html");
  77. const char* html_c =(const char*)html;
  78.  
  79. char* path_p = malloc(strlen(path) + 1 + 9);
  80. strcpy(path_p, path);
  81. strcat(path_p, "index.php");
  82.  
  83. char* path_h = malloc(strlen(path) + 1 + 10);
  84. strcpy(path_h, path);
  85. strcat(path_h, "index.html");
  86.  
  87. res_p = access(php_c, F_OK);
  88. res_h = access(html_c, F_OK);
  89.  
  90. if (res_p == 0)
  91. return path_p;
  92. else
  93. {
  94. if (res_h == 0)
  95. return path_h;
  96. else
  97. return NULL;
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement