void close_file(file_t * file) { clean_term(file->term); clean_renderer(file->rend); clean_buff(file->buff); clean_buff(file->modbuff); clean_keys(); }
int init_download(client_ftp_t *client_ftp, char *cmd) { char buff[1024]; int i; bzero(buff, 1024); xsend(client_ftp->s, "TYPE I\n", 7, 0); i = xrecv(client_ftp->s, buff, 1024, 0); clean_buff(buff, i); printf("%s\n", buff); bzero(buff, 1024); open_new_data_connection(client_ftp); open_data_connection(client_ftp); bzero(buff, 1024); for (i = 4; cmd[i] == ' ' && cmd[i] != '\0'; i++) ; sprintf(buff, "RETR %s\n", cmd + i); xsend(client_ftp->s, buff, strlen(buff), 0); i = xrecv(client_ftp->s, buff, 1024, 0); clean_buff(buff, i); printf("%s\n", buff); if (strncmp(buff, "550", 3) == 0) { close(client_ftp->s_data); return (1); } return (0); }
/*Function to compare absolute path of file in file catalog. Input:char out[],int fd_cat Output:int */ int comparepath(char out[]) { struct stat st; int size = 0; size_t length = 0; int ret = -1; int flag = 1; char* buffer = NULL; fstat(fd_cat, &st); size = st.st_size; // rewind the stream pointer to the start of catalog file if (size> 0) { if (-1 == lseek(fd_cat,0,SEEK_SET)) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } } if (size== 0) { ret=1; goto out; } while (size> 0) { ret=read(fd_cat,&length,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer=(char*)calloc(1,length+1); ret = read(fd_cat,buffer,length); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer[length]='\0'; if (strcmp(out,buffer)== 0) { ret=0; clean_buff(&buffer); break; } size-=(length+int_size); memset(buffer,0,sizeof(buffer)); clean_buff(&buffer); ret=1; } out: return ret; }
void retr_opt(client_ftp_t *client_ftp, char *cmd) { FILE *fs; char buff[1024]; char put[1024]; int nb; int i; int j; if ((i = init_download(client_ftp, cmd)) == 1) return; if ((fs = fopen(cmd + 4, "w+")) == NULL) { printf("Failed to open file\n"); close(client_ftp->s_data); return; } bzero(buff, 1024); while ((nb = xread(client_ftp->s_data, buff, 1024))) { bzero(put, 1024); for (i = 0, j = 0; i != nb; i++, j++) put[j] = buff[i]; fwrite(put, 1, j, fs); bzero(buff, 1024); } fclose(fs); i = xrecv(client_ftp->s, buff, 1024, 0); clean_buff(buff, i); printf("%s\n", buff); close(client_ftp->s_data); }
void init_pwd(client_ftp_t *client_ftp) { char buffer[1024]; int i; bzero(buffer, 1024); xsend(client_ftp->s, "PWD\n", 4, 0); i = xrecv(client_ftp->s, buffer, 1024, 0); clean_buff(buffer, i); client_ftp->pwd = strdup(buffer); }
/*Function to read all deduped files from a catalog file. Input:void Output:int */ int readfilecatalog() { struct stat st; int ret = -1; char *buffer = NULL; int size = 0; int length = 0; fstat(fd_cat, &st); size = st.st_size; if (size> 0) { if (-1 == lseek(fd_cat,0,SEEK_SET)) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } } else { goto out; } printf("\nAbsolute path of deduped files are:\n"); while (size> 0) { ret=read(fd_cat,&length,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer=(char*)calloc(1,length+1); ret = read(fd_cat,buffer,length); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer[length]='\0'; printf("%s\n",buffer); size-=(length+int_size); memset(buffer,0,sizeof(buffer)); clean_buff(&buffer); ret=1; } out: return ret; }
/*Function to delete file from a directory. Input:void Output:int */ int delete_file() { int fd_cat = -1; int ret = -1; int status = -1; char *filename = NULL; filename=(char*)calloc(1,FILE_SIZE); fd_cat =open("filecatalog.txt",O_RDONLY); if (fd_cat< 1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } printf("\n deduped files are\n"); ret=readfilecatalog(); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } printf("\nSelect the file you want to delete\n"); while (scanf("%s",filename) <= 0); printf("\nFILE%s",filename); status = remove(filename); if( status== 0 ) printf("\nFile deleted successfully.\n"); else { printf("\nUnable to delete the file\n"); perror("\nError\n"); goto out; } ret=0; out: clean_buff(&filename); return ret; }
/*Function to enter a filename that has to be restored. Input:void Output:int */ int restore_file() { int fd_cat = -1; int ret = -1; char* path = NULL; printf("\ndeduped files\n"); ret=readfilecatalog(); if (ret== -1) { goto out; } path=(char*)calloc(1,FILE_SIZE); out4:printf("\nEnter the exact and full path of dedup file to be restored\n"); scanf("%s",path); ret=comparepath(path); if (ret== -1) { goto out; } if (ret== 1) { printf("\nPlease enter valid full path of file"); goto out4; } ret=restorefile(path); if (ret== -1) { goto out; } ret=0; out: clean_buff(&path); return ret; }
/*Function to check whether a hash of specfic range is present in stub or not. Input:int fd_stub,int b_offset,int e_offset Output:int */ int searchstubhash(int fd_stub,int b_offset,int e_offset) { struct stat st; int size = 0; size_t length = 0; int ret = -1; int flag = 1; int eset = 0; int bset = 0; char* buffer = NULL; fstat(fd_stub, &st); size = st.st_size; // rewind the stream pointer to the start of stub file if (size> 0) { if(-1 == lseek(fd_stub,0,SEEK_SET)) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } } if (size== 0) { ret = 1; goto out; } while (size> 0) { ret=read(fd_stub,&length,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer=(char*)calloc(1,length+1); ret = read(fd_stub,buffer,length); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } ret=read(fd_stub,&bset,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } ret=read(fd_stub,&eset,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer[length]='\0'; if (bset== b_offset && eset== e_offset) { ret=0; clean_buff(&buffer); break; } size-=(length+int_size+int_size+int_size); clean_buff(&buffer); ret=1; } out: return ret; }
/*Function to delete file and restore it with original contents. Input:char* path Output:int */ int restorefile(char* path) { int status = 0; int ret = -1; char temp_name[NAME_SIZE] = ""; int l = 0; char *ssc = NULL; int size = 0; int size1 = 0; int pos = 0; char* buffer = NULL; char* buffer2 = NULL; char* ptr = NULL; int length = 0; int sd1 = -1; int fd_block = -1; struct stat st; int bset = 0; int eset = 0; int fd2 = -1; char actualpath [PATH_MAX+1]; char* ts1 = NULL; char* ts2 = NULL; char* dir = NULL; char* filename1 = NULL; ts1 = strdup(path); ts2 = strdup(path); dir = dirname(ts1); filename1 = basename(ts2); sprintf(dir,"%s/",dir); sprintf(temp_name,"%sDedup_%s",dir,filename1); printf("%s\n",dir); printf("\npath%s",path); printf("%s\n",filename1); printf("\n%s\n",temp_name); sd1 = open(temp_name,O_RDONLY); if (sd1< 1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } else { printf("\nStub file opened\n"); } fstat(sd1, &st); size = st.st_size; fd2 = open(path,O_CREAT|O_RDWR); if (fd2< 1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } else { printf("\nRestore file created\n"); } if (size> 0) { if (-1 == lseek(sd1,0,SEEK_SET)) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } } if(size==0) { printf("\nNo contents\n"); ret=-1; goto out; } while(size>0) { ret=read(sd1,&length,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer=(char*)calloc(1,length+1); ret=read(sd1,buffer,length); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } ret=read(sd1,&bset,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } ret=read(sd1,&eset,int_size); if (ret== -1) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } buffer[length]='\0'; pos=getposition(buffer); if (pos== -1) goto out; printf("\nPosition is %d\n",pos); buffer2=get_block(pos); if (strcmp(buffer2,"")== 0) { goto out; } printf("block is %s\n",buffer2); ret= write(fd2,buffer2,strlen(buffer2)); if (ret< 0) { fprintf(stderr,"%s\n",strerror(errno)); goto out; } size1-=(length+int_size+int_size+int_size); clean_buff(&buffer); clean_buff(&buffer2); } ret=0; out: return ret; }
/* 这个函数会按照一定的策略释放物品和技能 */ int axe_policy(LIFE_S *self) { int ret = DOTA_INVALID; int tmp = DOTA_INVALID; OPERATE_S *self_ops; LIFE_S *target; BUFF_NODE_S *node; self_ops = self->ops; target = self->target; /* 目标的血量是否低于400,若低于则释放淘汰之刃,瞬间斩杀 */ if (target->cur_hmaa.health <= AXE_C_DAMAGE_UPPER_LIMIT) { ret = self_ops->skill(self, AXE_C); DOTA_RETURN_IF_SUCCESS(ret); } /* * 是否处于物理攻击下,若是则会触发反击螺旋,攻击斧王的人越多, * 触发的概率越大,physical attack的buff是不会主动清除的,这里 * 根据该buff的数量得知某一时刻攻击斧王的人数。 */ while (NULL != (node = find_buff_byname(self, NAME_PHY_ATK))) { (void)clean_buff(self, node); tmp = self_ops->skill(self, AXE_X); if (DOTA_SUCCESS == tmp) ret = DOTA_SUCCESS; } DOTA_RETURN_IF_SUCCESS(ret); /* * 当目标没有羊刀效果才释放邪恶蛋蛋,不然放了白放,这才是聪明的 * AI该干的事 */ if ((NULL == find_buff_byname(target, NAME_GSV)) && (NULL == find_buff_byname(target, "AXE_E"))) { ret = self_ops->equip(self, EQUIPMENT_1); DOTA_RETURN_IF_SUCCESS(ret); } /* 释放羊刀策略,没有被吼住且没有邪恶蛋蛋效果时,释放羊刀 */ if ((NULL == find_buff_byname(target, "AXE_E")) && (NULL == find_buff_byname(target, NAME_XMS))) { ret = self_ops->equip(self, EQUIPMENT_0); DOTA_RETURN_IF_SUCCESS(ret); } /* * 目标没有被羊住,且没有被释放邪恶蛋蛋时,释放狂战士怒吼 * 收益最大 */ if ((NULL == find_buff_byname(target, NAME_GSV)) && (NULL == find_buff_byname(target, NAME_XMS))) { ret = self_ops->skill(self, AXE_E); DOTA_RETURN_IF_SUCCESS(ret); } /* 这个战斗饥渴的BUFF还是挺管用的,能烧掉不少血 */ ret = self_ops->skill(self, AXE_R); DOTA_RETURN_IF_SUCCESS(ret); /* 物理攻击 */ ret = self_ops->attack(self); DOTA_RETURN_IF_SUCCESS(ret); return ret; }
/*Function to get the block from blockstore. Input:int pos Output:char* */ char *ydl_block::get_block(int pos,int *l) { struct stat st; int size = 0; int length = 0; int ret = -1; char *buffer = NULL; int position = 1; fstat(block_fd, &st); size = st.st_size; /*rewind the stream pointer to the start of block file*/ if (size > 0) { if (-1 == lseek(block_fd, 0, SEEK_SET)) { cout << "\nLseek failed with error:" << strerror(errno) << endl; goto out; } } while (size > 0) { ret = read(block_fd, &length, INT_SIZE); *l = length; if (ret == -1) { cout << "\nError while reading " << strerror(errno) << endl; goto out; } position = position+ret; if (length <= 0) { goto out; } buffer = (char *)calloc(1, length+1); if (position == pos) { ret = read(block_fd, buffer, length); if (ret == -1) { cout << "\nRead failed with error " << strerror(errno) << endl; goto out; } ret = 0; buffer[length] = '\0'; break; } ret = read(block_fd, buffer, length); if (ret == -1) { cout << "\nRead failed with error " << strerror(errno) << endl; goto out; } position = position + length; buffer[length] = '\0'; size -= (length+INT_SIZE); clean_buff(&buffer); } ret = 0; out: if (ret == -1) { memset(buffer, 0, length+1); } return buffer; }
/*Function to get the block from blockstore. Input:int pos Output:char* */ char* get_block(int pos) { struct stat st; int size = 0; size_t length = 0; int ret = -1; char* buffer = NULL; int position= 1; fstat(fd.fd_block, &st); size = st.st_size; // rewind the stream pointer to the start of block file if (size> 0) { if (-1 == lseek(fd.fd_block,0,SEEK_SET)) { printf("\nLseek failed with error: [%s]\n",strerror(errno)); goto out; } } while (size> 0) { ret=read(fd.fd_block,&length,INT_SIZE); if (ret== -1) { printf("\nError while reading %s",strerror(errno)); goto out; } position=position+ret; if (length<= 0) { goto out; } buffer=(char*)calloc(1,length+1); if (position== pos) { ret = read(fd.fd_block,buffer,length); if (ret== -1) { printf("\nRead failed with error %s\n",strerror(errno)); goto out; } ret=0; buffer[length]='\0'; break; } ret = read(fd.fd_block,buffer,length); if (ret== -1) { printf("\nRead failed with error %s\n",strerror(errno)); goto out; } position=position+length; buffer[length]='\0'; size-=(length+INT_SIZE); clean_buff(&buffer); } ret=0; out: if (ret== -1) { memset(buffer,0,sizeof(buffer)); } return buffer; }
int read_seq (FILE *IN, seq_t *seq_holder, char *alphabet) { char *BUFF, *stock, *p, *q; int i, state, l; size_t buffsize, bufflen, seq_len; /* check if there is something to read */ if ((i = fgetc(IN)) == EOF && feof(IN) != 0) { return NO_SEQ; } ungetc(i, IN); state = SEQ_NONE; seq_len = bufflen = 0; buffsize = BUFFSIZE; seq_holder->seq = NULL; if ((BUFF = (char *) malloc(sizeof(char) * buffsize+1 )) == NULL) error_fatal ("memory1" , NULL); if ((stock = (char *) malloc(sizeof(char) * buffsize+1 )) == NULL) error_fatal ("memory2" , NULL); *stock = '\0'; q = stock; while ((i = fgetc(IN)) != EOF) { /* skip empty lines */ if (isspace(i)) {continue;} if ( ungetc(i, IN) == EOF ) error_fatal ("ungetc", NULL); /* end entry or start entry */ if ( i == '>' ) { if (state == SEQ_NONE ) state = HEADER; if (state == SEQ) break ; } if (fgets(BUFF, BUFFSIZE + 1, IN) == NULL) break; /* header processing */ if ( state == HEADER ) { /* memcopy with '/0' inclusion */ memcpy(q, BUFF, BUFFSIZE+1); /* is header line completly read */ if (strrchr(BUFF, '\n') == NULL) { if((stock = realloc(stock, buffsize + BUFFSIZE + 1)) == NULL) error_fatal("realloc", NULL); q = stock + buffsize ; buffsize += BUFFSIZE ; continue; } process_header(seq_holder, stock); state = SEQ; buffsize = BUFFSIZE; p = stock; *p ='\0'; continue; } if ( state == SEQ || state == DUMP ){ /* we clean the buffer before any further use */ if ((l = clean_buff(&BUFF, alphabet)) == -1) error_fatal(seq_holder->id, "sequence contain spurious characters"); } if (state == SEQ ) { bufflen = l; /* is stock buffer long enough */ if ( seq_len + bufflen >= buffsize ) { buffsize += BUFFSIZE; if ((stock = realloc(stock, sizeof(char) * buffsize+1)) == NULL) error_fatal("Memory3", "Reallocating seq"); } q = stock + seq_len; strncpy (q, BUFF, bufflen); seq_len += bufflen; } if ( state == SEQ_NONE ) { error_fatal("Sequence", "is NOT fasta formated"); } } free(BUFF); if ( state == SEQ ) { *(stock+seq_len) = '\0'; seq_holder->seq = stock; } seq_holder->size = seq_len; return state; }