Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <string.h>
- #define true 1
- #define false 0
- typedef struct header {
- char name[100];
- char mode[8];
- char uid[8];
- char gid[8];
- char size[12];
- char mtime[12];
- char chksum[8];
- char typeflag;
- char linkname[100];
- char magic[6];
- char version[2];
- char uname[32];
- char gname[32];
- char devmajor[8];
- char devminor[8];
- char prefix[167];
- } header;
- int read_command(int m, int n, char cmds[][n]) {
- char buf[512];
- fgets(buf, 512, stdin);
- int word_index = 0,
- char_index = 0;
- for (int i = 0; buf[i] != 0 && buf[i] != '\n'; i++) {
- if (char_index) {
- if (buf[i] == ' ') {
- cmds[word_index - 1][char_index] = 0;
- char_index = 0;
- continue;
- }
- cmds[word_index - 1][char_index] = buf[i];
- char_index++;
- }
- if (!char_index) {
- if (buf[i] != ' ') {
- word_index++;
- cmds[word_index - 1][char_index] = buf[i];
- char_index++;
- }
- }
- if (word_index > m || char_index > n) {
- return -1;
- }
- }
- cmds[word_index - 1][char_index] = 0;
- return word_index;
- }
- typedef struct user {
- char username[100];
- unsigned int uid,
- gid;
- } user;
- int create_archive(char * name, char * dir) {
- FILE * files, * usermap, * archive;
- files = fopen("files.txt", "r");
- usermap = fopen("usermap.txt", "r");
- archive = fopen(name, "wb");
- if (files == NULL || usermap == NULL || archive == NULL) {
- return -1;
- }
- char buf[1024];
- int nfiles = 0;
- /////////////////////////////////////
- /// ITERAM PRIN LINILE DIN usermap.txt
- /// SI UMPLEM STRUCTURILE DIN users[]
- user * users;
- users = malloc(sizeof(user)*1000);
- unsigned int nusers = 0;
- {
- char delim[] = ":";
- for (int i = 0; fgets(buf, 1024, usermap); i++) {
- //realloc(users, nusers + sizeof(user));
- char * ptr;
- ptr = strtok(buf, delim);
- strcpy(users[i].username, ptr);
- ptr = strtok(NULL, delim);
- ptr = strtok(NULL, delim);
- sscanf(ptr, "%u", &users[i].uid);
- ptr = strtok(NULL, delim);
- sscanf(ptr, "%u", &users[i].gid);
- nusers++;
- }
- }
- /////////////////////////////////////
- /// ITERAM PRIN LINILE DIN files.txt
- while (fgets(buf, 512, files)) {
- char uname[32],
- gname[32],
- file_name[100];
- unsigned int file_size, uid, gid;
- sscanf(buf, "%*s %*s %s %s %u %*s %*s %*s %s", uname, gname, &file_size, file_name);
- /////////////////////////////////
- /// CAUTAM uid SI gid in users[]
- for (int i = 0; i < nusers; i++) {
- if (strcmp(users[i].username, uname) == 0) {
- uid = users[i].uid;
- gid = users[i].gid;
- break;
- }
- }
- unsigned int chksum = 0;
- header file_header;
- memset(&file_header, 0, sizeof(header));
- strcpy(file_header.name, file_name);
- strcpy(file_header.magic, "ustar ");
- sprintf(file_header.size, "%011o", file_size);
- file_size = (file_size / 512 + ((file_size % 512 == 0) ? 0 : 1)) * 512;
- file_header.typeflag = '0';
- *(unsigned int *)file_header.devmajor = 0;
- *(unsigned int *)file_header.devminor = 0;
- sprintf(file_header.mtime, "%011o", time(NULL));
- strcpy(file_header.mode, "0000640");
- strcpy(file_header.uname, uname);
- strcpy(file_header.gname, gname);
- sprintf(file_header.uid, "%07o", uid);
- sprintf(file_header.gid, "%07o", gid);
- char *source = NULL;
- char full_path[200];
- strcpy(full_path, dir);
- strcat(full_path, file_name);
- //////////////////////////////////
- /// CITIRE FISIER PENTRU ARHIVARE
- FILE *fp = fopen(full_path, "r");
- if (fp == NULL) {
- return -1;
- }
- source = malloc(file_size);
- memset(source, 0, file_size);
- fread(source, sizeof(char), file_size, fp);
- fclose(fp);
- for (int i = 0; i < 512; i++) {
- chksum += ((char *)&file_header)[i];
- }
- sprintf(file_header.chksum, "%06o", chksum);
- file_header.chksum[6] = 0;
- file_header.chksum[7] = ' ';
- fwrite(&file_header, 1, sizeof(header), archive);
- fwrite(source, 1, file_size, archive);
- free(source);
- nfiles++;
- }
- for (int i = 0; i < 512 * 2; i++) {
- fputc(0, archive);
- }
- free(users);
- fclose(archive);
- fclose(files);
- return nfiles;
- }
- int list(char * name) {
- FILE * archive;
- archive = fopen(name, "r");
- if (archive == NULL) {
- return -1;
- }
- char buf[512];
- buf[0] = 1;
- while(true) {
- int fsize;
- fread(buf, 1, 512, archive);
- if (buf[0] == 0) break;
- sscanf(((header *)buf)->size, "%o", &fsize);
- printf("> %s\n", ((header *)buf)->name);
- fseek(archive, (fsize / 512 + ((fsize % 512 == 0) ? 0 : 1)) * 512, SEEK_CUR);
- }
- return 0;
- }
- int main() {
- char cmds[3][512];
- while (true) {
- int nargs = read_command(3, 512, cmds);
- if (nargs == 0) continue;
- if (nargs < 0) goto error;
- if (strcmp(cmds[0], "create") == 0) {
- if (nargs != 3) goto error;
- if (create_archive(cmds[1], cmds[2]) > 0) {
- printf("> Done!\n");
- } else {
- printf("> Failed!\n");
- }
- continue;
- }
- if (strcmp(cmds[0], "list") == 0) {
- if (nargs != 2) goto error;
- list(cmds[1]);
- continue;
- }
- if (strcmp(cmds[0], "extract") == 0) {
- if (nargs != 3) goto error;
- printf("> Ran extract.\n");
- continue;
- }
- if (strcmp(cmds[0], "exit") == 0) {
- break;
- }
- error:
- printf("> Wrong command!\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement