Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. #define true 1
  7. #define false 0
  8.  
  9. typedef struct header {
  10. char name[100];
  11. char mode[8];
  12. char uid[8];
  13. char gid[8];
  14. char size[12];
  15. char mtime[12];
  16. char chksum[8];
  17. char typeflag;
  18. char linkname[100];
  19. char magic[6];
  20. char version[2];
  21. char uname[32];
  22. char gname[32];
  23. char devmajor[8];
  24. char devminor[8];
  25. char prefix[167];
  26. } header;
  27.  
  28. int read_command(int m, int n, char cmds[][n]) {
  29. char buf[512];
  30.  
  31. fgets(buf, 512, stdin);
  32.  
  33. int word_index = 0,
  34. char_index = 0;
  35.  
  36. for (int i = 0; buf[i] != 0 && buf[i] != '\n'; i++) {
  37. if (char_index) {
  38. if (buf[i] == ' ') {
  39. cmds[word_index - 1][char_index] = 0;
  40. char_index = 0;
  41.  
  42. continue;
  43. }
  44.  
  45. cmds[word_index - 1][char_index] = buf[i];
  46. char_index++;
  47. }
  48.  
  49. if (!char_index) {
  50. if (buf[i] != ' ') {
  51. word_index++;
  52. cmds[word_index - 1][char_index] = buf[i];
  53. char_index++;
  54. }
  55. }
  56.  
  57. if (word_index > m || char_index > n) {
  58. return -1;
  59. }
  60. }
  61.  
  62. cmds[word_index - 1][char_index] = 0;
  63.  
  64. return word_index;
  65. }
  66.  
  67. typedef struct user {
  68. char username[100];
  69. unsigned int uid,
  70. gid;
  71. } user;
  72.  
  73. int create_archive(char * name, char * dir) {
  74. FILE * files, * usermap, * archive;
  75.  
  76. files = fopen("files.txt", "r");
  77. usermap = fopen("usermap.txt", "r");
  78. archive = fopen(name, "wb");
  79.  
  80. if (files == NULL || usermap == NULL || archive == NULL) {
  81. return -1;
  82. }
  83.  
  84. char buf[1024];
  85.  
  86. int nfiles = 0;
  87.  
  88. /////////////////////////////////////
  89. /// ITERAM PRIN LINILE DIN usermap.txt
  90. /// SI UMPLEM STRUCTURILE DIN users[]
  91. user * users;
  92. users = malloc(sizeof(user)*1000);
  93.  
  94. unsigned int nusers = 0;
  95. {
  96. char delim[] = ":";
  97.  
  98. for (int i = 0; fgets(buf, 1024, usermap); i++) {
  99. //realloc(users, nusers + sizeof(user));
  100.  
  101. char * ptr;
  102.  
  103. ptr = strtok(buf, delim);
  104. strcpy(users[i].username, ptr);
  105.  
  106. ptr = strtok(NULL, delim);
  107. ptr = strtok(NULL, delim);
  108. sscanf(ptr, "%u", &users[i].uid);
  109.  
  110. ptr = strtok(NULL, delim);
  111. sscanf(ptr, "%u", &users[i].gid);
  112.  
  113. nusers++;
  114. }
  115. }
  116.  
  117. /////////////////////////////////////
  118. /// ITERAM PRIN LINILE DIN files.txt
  119. while (fgets(buf, 512, files)) {
  120. char uname[32],
  121. gname[32],
  122. file_name[100];
  123.  
  124. unsigned int file_size, uid, gid;
  125.  
  126. sscanf(buf, "%*s %*s %s %s %u %*s %*s %*s %s", uname, gname, &file_size, file_name);
  127.  
  128. /////////////////////////////////
  129. /// CAUTAM uid SI gid in users[]
  130. for (int i = 0; i < nusers; i++) {
  131. if (strcmp(users[i].username, uname) == 0) {
  132. uid = users[i].uid;
  133. gid = users[i].gid;
  134.  
  135. break;
  136. }
  137. }
  138.  
  139. unsigned int chksum = 0;
  140.  
  141. header file_header;
  142. memset(&file_header, 0, sizeof(header));
  143. strcpy(file_header.name, file_name);
  144. strcpy(file_header.magic, "ustar ");
  145.  
  146. sprintf(file_header.size, "%011o", file_size);
  147. file_size = (file_size / 512 + ((file_size % 512 == 0) ? 0 : 1)) * 512;
  148.  
  149. file_header.typeflag = '0';
  150. *(unsigned int *)file_header.devmajor = 0;
  151. *(unsigned int *)file_header.devminor = 0;
  152.  
  153. sprintf(file_header.mtime, "%011o", time(NULL));
  154.  
  155. strcpy(file_header.mode, "0000640");
  156. strcpy(file_header.uname, uname);
  157. strcpy(file_header.gname, gname);
  158.  
  159. sprintf(file_header.uid, "%07o", uid);
  160. sprintf(file_header.gid, "%07o", gid);
  161.  
  162. char *source = NULL;
  163. char full_path[200];
  164.  
  165. strcpy(full_path, dir);
  166. strcat(full_path, file_name);
  167.  
  168. //////////////////////////////////
  169. /// CITIRE FISIER PENTRU ARHIVARE
  170. FILE *fp = fopen(full_path, "r");
  171. if (fp == NULL) {
  172. return -1;
  173. }
  174.  
  175. source = malloc(file_size);
  176. memset(source, 0, file_size);
  177.  
  178. fread(source, sizeof(char), file_size, fp);
  179. fclose(fp);
  180.  
  181. for (int i = 0; i < 512; i++) {
  182. chksum += ((char *)&file_header)[i];
  183. }
  184.  
  185. sprintf(file_header.chksum, "%06o", chksum);
  186. file_header.chksum[6] = 0;
  187. file_header.chksum[7] = ' ';
  188.  
  189. fwrite(&file_header, 1, sizeof(header), archive);
  190. fwrite(source, 1, file_size, archive);
  191.  
  192. free(source);
  193.  
  194. nfiles++;
  195. }
  196.  
  197. for (int i = 0; i < 512 * 2; i++) {
  198. fputc(0, archive);
  199. }
  200.  
  201. free(users);
  202.  
  203. fclose(archive);
  204. fclose(files);
  205.  
  206. return nfiles;
  207. }
  208.  
  209. int list(char * name) {
  210. FILE * archive;
  211.  
  212. archive = fopen(name, "r");
  213. if (archive == NULL) {
  214. return -1;
  215. }
  216.  
  217. char buf[512];
  218. buf[0] = 1;
  219.  
  220. while(true) {
  221. int fsize;
  222.  
  223. fread(buf, 1, 512, archive);
  224. if (buf[0] == 0) break;
  225.  
  226. sscanf(((header *)buf)->size, "%o", &fsize);
  227. printf("> %s\n", ((header *)buf)->name);
  228.  
  229. fseek(archive, (fsize / 512 + ((fsize % 512 == 0) ? 0 : 1)) * 512, SEEK_CUR);
  230. }
  231.  
  232. return 0;
  233. }
  234.  
  235. int main() {
  236. char cmds[3][512];
  237.  
  238. while (true) {
  239. int nargs = read_command(3, 512, cmds);
  240.  
  241. if (nargs == 0) continue;
  242. if (nargs < 0) goto error;
  243.  
  244. if (strcmp(cmds[0], "create") == 0) {
  245. if (nargs != 3) goto error;
  246.  
  247. if (create_archive(cmds[1], cmds[2]) > 0) {
  248. printf("> Done!\n");
  249. } else {
  250. printf("> Failed!\n");
  251. }
  252.  
  253. continue;
  254. }
  255.  
  256. if (strcmp(cmds[0], "list") == 0) {
  257. if (nargs != 2) goto error;
  258.  
  259. list(cmds[1]);
  260.  
  261. continue;
  262. }
  263.  
  264. if (strcmp(cmds[0], "extract") == 0) {
  265. if (nargs != 3) goto error;
  266.  
  267. printf("> Ran extract.\n");
  268.  
  269. continue;
  270. }
  271.  
  272. if (strcmp(cmds[0], "exit") == 0) {
  273. break;
  274. }
  275.  
  276. error:
  277. printf("> Wrong command!\n");
  278. }
  279.  
  280. return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement