int main(){ int fileSize=0, i=0; /** initialise whole memory as free **/ FreeMap.totalFreeCount=MAX_BLOCK_COUNT; for(i=0; i<MAX_BLOCK_COUNT; i++){ FreeMap.isFree[i]=FREE; } /** create file of mentione size **/ fileSize=createFile("newFile.txt"); if(fileSize==0){ return 1; } /** read file and store in memory back store **/ struct IndexNode *fileIndex = loadFileInMemory("newFile.txt", fileSize); if(fileIndex==NULL){ return 1; } /** read file from memory and print **/ readAndPrint(fileIndex); /** initialise root directory **/ initialiseDir(&root, ROOT); /** initialise inode-table **/ for(i=0; i<MAX_FILE_SUPPORTED; i++) { inodeTable[MAX_FILE_SUPPORTED].inode=NULL; inodeTable[MAX_FILE_SUPPORTED].inode_no=-1; } /** map file to directory and inode **/ if(mapFileToInodeTable(fileIndex, ROOT)==1) return 1; return 0; }
int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct addrinfo hints, *servinfo, *p; int rv; char s[INET6_ADDRSTRLEN]; if (argc != 2) { fprintf(stderr,"usage: client hostname\n"); exit(1); } memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; if ((rv = getaddrinfo(argv[1], PORT, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } //printf("got address info\n"); // loop through all the results and connect to the first we can for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("client: socket"); continue; } if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("client: connect"); continue; } break; } if (p == NULL) { fprintf(stderr, "client: failed to connect\n"); return 2; } //printf("estabilishing connection...\n"); inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s); //printf("client: connecting to %s\n", s); freeaddrinfo(servinfo); // all done with this structure if ((numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) { perror("recv"); exit(1); } buf[numbytes] = '\0'; // printf("%s\n\n",buf); int numfilmes = strToInt(&(buf[45])); char mywritebuf[MAXDATASIZE]; char option; int id, num_exemplares; do { // show_menu(); scanf(" %c", &option); switch(option) { case '1': { start(); mywrite(sockfd, "1\0"); readAndPrint(sockfd); stop(1); break; } case '2': { mywritebuf[0] = '2'; mywritebuf[1] = ' '; // show_genres(); while(1) { scanf(" %c", &(mywritebuf[2])); if(mywritebuf[2] < '1' || mywritebuf[2] > '7') { // printf("Opcao invalida.\n"); // show_genres(); } else break; } mywritebuf[3] = '\0'; start(); mywrite(sockfd, mywritebuf); readAndPrint(sockfd); stop(2); break; } case '3': { mywritebuf[0] = '3'; mywritebuf[1] = ' '; // printf("Insira o id do filme: "); while(1) { scanf(" %d", &id); if(id < 1 || id > numfilmes) { // printf("Id invalido (Possuimos %d filmes).\n", numfilmes); // printf("Insira o id do filme: "); } else break; } sprintf(&(mywritebuf[2]), "%d", id); start(); mywrite(sockfd, mywritebuf); readAndPrint(sockfd); stop(3); break; } case '4': { mywritebuf[0] = '4'; mywritebuf[1] = ' '; // printf("Insira o id do filme: "); while(1) { scanf(" %d", &id); if(id < 1 || id > numfilmes) { // printf("Id invalido (Possuimos %d filmes).\n", numfilmes); // printf("Insira o id do filme: "); } else break; } sprintf(&(mywritebuf[2]), "%d", id); start(); mywrite(sockfd, mywritebuf); readAndPrint(sockfd); stop(4); break; } case '5': { start(); mywrite(sockfd, "5\0"); readAndPrint(sockfd); stop(5); break; } case '6': { mywritebuf[0] = '6'; mywritebuf[1] = ' '; // printf("Insira o id do filme: "); while(1) { scanf(" %d", &id); if(id < 1 || id > numfilmes) { // printf("Id invalido (Possuimos %d filmes).\n", numfilmes); // printf("Insira o id do filme: "); } else break; } // printf("Insira o novo numero de exemplares: "); scanf(" %d", &num_exemplares); sprintf(&(mywritebuf[2]), "%d %d", id, num_exemplares); start(); mywrite(sockfd, mywritebuf); readAndPrint(sockfd); stop(6); break; } case '7': { mywritebuf[0] = '7'; mywritebuf[1] = ' '; // printf("Insira o id do filme: "); while(1) { scanf(" %d", &id); if(id < 1 || id > numfilmes) { // printf("Id invalido (Possuimos %d filmes).\n", numfilmes); // printf("Insira o id do filme: "); } else break; } sprintf(&(mywritebuf[2]), "%d", id); start(); mywrite(sockfd, mywritebuf); readAndPrint(sockfd); stop(7); break; } case '8': { mywrite(sockfd, "8\0"); break; } default: { printf("Isso não é uma opcao valida.\n"); break; } }; // printf("\n"); }while(option != '8'); close(sockfd); return 0; }