void eliminate(){ int count=0; int i ,j; for(i=PANELX-1;i>=0;){ bool elim = true; for(j=0;j<PANELY;j++){ if(prev_panel[i][j]==0) elim = false; } if(elim){ count++; for(j=0;j<PANELY;j++) prev_panel[i][j]=0; removeline(i); } else{ i--; } } if(count != 0) score=score + (1<<count); if(level!=score/50+1){ level = score/50+1; sleeptime= sleeptime- level*50000; } attrset(COLOR_PAIR(7)); mvprintw(15,37," Your Score "); attrset(COLOR_PAIR(0)); mvprintw(16,40,"%d",score); attrset(COLOR_PAIR(7)); mvprintw(18,37," Your Level "); attrset(COLOR_PAIR(0)); mvprintw(19,40,"%d",level); }
/* only check "around" y */ static void checkboard(int y) { int a, b, c, r = 0, ey = y + 4; if (ey>=24) ey=24; for (b=y; b<ey; b++) { c = 0; for (a=4; a<WIDTH+4; a++) { if (board[a][b]) c++; } if (c == WIDTH) { setbg(white); horizontal(40-WIDTH, 40+WIDTH-1, b-3); moveto(1,24); flush(); usleep(100000); setbg(black); horizontal(40-WIDTH, 40+WIDTH-1, b-3); moveto(1,24); flush(); usleep(100000); removeline(b); r++; } } if (r==4) { /* slower if you make four rows at once! */ speed--; if (speed < minspeed) speed = minspeed; printlevel(); } }
void *sendCommand(void *arg) { char msg[INPUT]; int sfd = *(int *)arg; printf("\n>"); fflush(stdout); while(1) { //Sir Just fixed a size here so user can only enter this much memset(msg,'\0',INPUT); fgets (msg,INPUT, stdin); //Sir Using fgets instead of scanf becasue scanf breaks on whitespace so then message breaks 2/3 parts //fgets is far more approripate for string handling and to get input correctly - morevoer it also avoids buffer overflow as it //stops reading in after the number we give to it removeline(msg); if(strcmp(msg,"disconnect")==0) { shouldclose = true; } write(sfd,msg,strlen(msg)); if(shouldclose) { disconnect(0); } } }
void removeline(int i){ int j; if(i == 0){ for(j=0;j<PANELY;j++) prev_panel[i][j]=0; return ; } else{ for(j=0;j<PANELY;j++) prev_panel[i][j]=prev_panel[i-1][j]; removeline(i-1); } }
int historycmd(int argc, char *argv[]) { int fd=0; int res=0; int i=0; char *line = NULL; FILE *fp; // mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; if ( cbsd_enable_history == 0 ) return 0; // do not logging history command in history if (!strcmp(argv[1],"history")) return 0; if (!strcmp(argv[1],"sudo")) return 0; if ((fd = open(cbsd_history_file, O_RDWR | O_APPEND | O_CREAT, (mode_t)0600 )) == -1) { return 0; } if (NULL == (fp = fdopen(fd, "a"))) { close(fd); return 1; } if (setlock(fd)!=0) return 1; //locked //rotate first line if necessary if (check_history_size(cbsd_history_file)!=0) removeline(fd,1); for (i = 1; i < argc; i++) res += strlen(argv[i])+1; res = res + 5; //+5: "cbsd " before and + "\n" at the end line=calloc(res,sizeof(char)); strcpy(line,"cbsd "); for (i = 1; i < argc; i++) { strcat(line, argv[i]); strcat(line," " ); } line[res-1] = '\n'; fputs(line,fp); free(line); fflush(fp); fclose(fp); return 0; }
int sbbs_t::text_sec() { char str[256],usemenu ,*file[MAX_TXTFILES],addpath[83],addstr[83],*buf,ch; char tmp[512]; long i,j,usrsec[MAX_TXTSECS],usrsecs,cursec; long l,length; FILE *stream; for(i=j=0;i<cfg.total_txtsecs;i++) { if(!chk_ar(cfg.txtsec[i]->ar,&useron,&client)) continue; usrsec[j++]=i; } usrsecs=j; if(!usrsecs) { bputs(text[NoTextSections]); return(1); } action=NODE_RTXT; while(online) { sprintf(str,"%smenu/text_sec.*",cfg.text_dir); if(fexist(str)) menu("text_sec"); else { bputs(text[TextSectionLstHdr]); for(i=0;i<usrsecs && !msgabort();i++) { sprintf(str,text[TextSectionLstFmt],i+1,cfg.txtsec[usrsec[i]]->name); if(i<9) outchar(' '); bputs(str); } } ASYNC; mnemonics(text[WhichTextSection]); if((cursec=getnum(usrsecs))<1) break; cursec--; while(online) { sprintf(str,"%smenu/text%lu.*",cfg.text_dir,cursec+1); if(fexist(str)) { sprintf(str,"text%lu",cursec+1); menu(str); usemenu=1; } else { bprintf(text[TextFilesLstHdr],cfg.txtsec[usrsec[cursec]]->name); usemenu=0; } sprintf(str,"%stext/%s.ixt",cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code); j=0; if(fexist(str)) { if((stream=fnopen((int *)&i,str,O_RDONLY))==NULL) { errormsg(WHERE,ERR_OPEN,str,O_RDONLY); return(0); } while(!ferror(stream) && !msgabort()) { /* file open too long */ if(!fgets(str,81,stream)) break; str[strlen(str)-2]=0; /* chop off CRLF */ if((file[j]=(char *)malloc(strlen(str)+1))==NULL) { errormsg(WHERE,ERR_ALLOC,nulstr,strlen(str)+1); continue; } strcpy(file[j],str); fgets(str,81,stream); if(!usemenu) bprintf(text[TextFilesLstFmt],j+1,str); j++; } fclose(stream); } ASYNC; if(SYSOP) { strcpy(str,"QARE?"); mnemonics(text[WhichTextFileSysop]); } else { strcpy(str,"Q?"); mnemonics(text[WhichTextFile]); } i=getkeys(str,j); if(!(i&0x80000000L)) { /* no file number */ for(l=0;l<j;l++) free(file[l]); if((i=='E' || i=='R') && !j) continue; } if(i=='Q' || !i) break; if(i==-1) { /* ctrl-c */ for(i=0;i<j;i++) free(file[i]); return(0); } if(i=='?') /* ? means re-list */ continue; if(i=='A') { /* Add text file */ if(j) { bputs(text[AddTextFileBeforeWhich]); i=getnum(j+1); if(i<1) continue; i--; /* number of file entries to skip */ } else i=0; bprintf(text[AddTextFilePath] ,cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code); if(!getstr(addpath,80,0)) continue; strcat(addpath,crlf); bputs(text[AddTextFileDesc]); if(!getstr(addstr,74,0)) continue; strcat(addstr,crlf); sprintf(str,"%stext/%s.ixt" ,cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code); if(i==j) { /* just add to end */ if((i=nopen(str,O_WRONLY|O_APPEND|O_CREAT))==-1) { errormsg(WHERE,ERR_OPEN,str,O_WRONLY|O_APPEND|O_CREAT); return(0); } write(i,addpath,strlen(addpath)); write(i,addstr,strlen(addstr)); close(i); continue; } j=i; /* inserting in middle of file */ if((stream=fnopen((int *)&i,str,O_RDWR))==NULL) { errormsg(WHERE,ERR_OPEN,str,O_RDWR); return(0); } length=(long)filelength(i); for(i=0;i<j;i++) { /* skip two lines for each entry */ fgets(tmp,81,stream); fgets(tmp,81,stream); } l=(long)ftell(stream); if((buf=(char *)malloc(length-l))==NULL) { fclose(stream); errormsg(WHERE,ERR_ALLOC,str,length-l); return(0); } fread(buf,1,length-l,stream); fseek(stream,l,SEEK_SET); /* go back to where we need to insert */ fputs(addpath,stream); fputs(addstr,stream); fwrite(buf,1,length-l,stream); fclose(stream); free(buf); continue; } if(i=='R' || i=='E') { /* Remove or Edit text file */ ch=(char)i; if(ch=='R') bputs(text[RemoveWhichTextFile]); else bputs(text[EditWhichTextFile]); i=getnum(j); if(i<1) continue; sprintf(str,"%stext/%s.ixt" ,cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code); j=i-1; if((stream=fnopen(NULL,str,O_RDONLY))==NULL) { errormsg(WHERE,ERR_OPEN,str,O_RDONLY); return(0); } for(i=0;i<j;i++) { /* skip two lines for each entry */ fgets(tmp,81,stream); fgets(tmp,81,stream); } fgets(addpath,81,stream); truncsp(addpath); fclose(stream); if(!strchr(addpath,'\\') && !strchr(addpath,'/')) sprintf(tmp,"%stext/%s/%s" ,cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code,addpath); else strcpy(tmp,addpath); if(ch=='R') { /* Remove */ if(fexist(tmp)) { sprintf(str,text[DeleteTextFileQ],tmp); if(!noyes(str)) if(remove(tmp)) errormsg(WHERE,ERR_REMOVE,tmp,0); } sprintf(str,"%stext/%s.ixt" ,cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code); removeline(str,addpath,2,0); } else { /* Edit */ strcpy(str,tmp); editfile(str); } continue; } i=(i&~0x80000000L)-1; if(!strchr(file[i],'\\') && !strchr(file[i],'/')) sprintf(str,"%stext/%s/%s" ,cfg.data_dir,cfg.txtsec[usrsec[cursec]]->code,file[i]); else strcpy(str,file[i]); fexistcase(str); attr(LIGHTGRAY); printfile(str,0); sprintf(str,"%s read text file: %s" ,useron.alias,file[i]); logline("T-",str); pause(); sys_status&=~SS_ABORT; for(i=0;i<j;i++) free(file[i]); } } return(0); }
/* editor event loop */ int editor_loop() { int ch; file_t *file; file = files[current]; ch = getch(); switch(ch) { case KEY_RESIZE: getmaxyx(screen, h, w); drawmenu(); drawscreen(); break; case KEY_F(5): /* display help */ drawhelp(); refresh(); while(!help_loop()); drawscreen(); break; case KEY_F(6): /* save */ if(file->filename[0]) { writefile(file, file->filename); file->saved = 1; } break; case KEY_F(8): /* force quit */ return 1; case KEY_F(9): /* toggle linenumbers */ show_linenumbers = !show_linenumbers; drawscreen(); break; case KEY_F(10): /* tab size */ tab_size = !tab_size; drawscreen(); break; case KEY_F(11): /* C highlight */ c_highlight = !c_highlight; drawscreen(); break; case 3: /* control-C */ if(file->selected) { /* TODO: copy */ } break; case 11: /* control-K */ if(file->line_count > 1) removeline(file, file->cursor_y); break; case 22: /* control-V */ /* TODO: paste */ break; case 24: /* control-X */ /* quit */ /* TODO: check all open files */ if(file->saved) return 1; break; case 19: /* control-S */ if(file->selected) { int y; /* end selection */ file->selected = 0; for(y = file->sel_begin_y; y <= file->sel_end_y; y++) drawline(file, y); } else { /* begin selection */ file->selected = 1; file->sel_begin_x = file->cursor_x; file->sel_begin_y = file->cursor_y; file->sel_end_x = file->cursor_x; file->sel_end_y = file->cursor_y; } break; case 21: /* control-U */ if(file->selected) { int y; /* un-indent lines */ for(y = file->sel_begin_y; y <= file->sel_end_y; y++) { if(file->lines[y].len && file->lines[y].buf[0] == '\t') removetext(file, 0, y, 1); } } break; case KEY_PPAGE: /* move half screen up */ if(file->cursor_y) { file->cursor_y -= h/2; if(file->cursor_y < 0) file->cursor_y = 0; /* don't go past end of line */ if(file->cursor_x > file->lines[file->cursor_y].len) file->cursor_x = file->lines[file->cursor_y].len; } break; case KEY_NPAGE: /* move half screen down */ if(file->cursor_y < file->line_count-1) { file->cursor_y += h/2; if(file->cursor_y > file->line_count-1) file->cursor_y = file->line_count-1; /* don't go past end of line */ if(file->cursor_x > file->lines[file->cursor_y].len) file->cursor_x = file->lines[file->cursor_y].len; } break; case KEY_UP: if(file->cursor_y) { file->cursor_y--; /* don't go past end of line */ if(file->cursor_x > file->lines[file->cursor_y].len) file->cursor_x = file->lines[file->cursor_y].len; } break; case KEY_DOWN: if(file->cursor_y < file->line_count-1) { file->cursor_y++; /* move cursor to line end */ if(file->cursor_x > file->lines[file->cursor_y].len) file->cursor_x = file->lines[file->cursor_y].len; } break; case KEY_HOME: /* go to begin of line */ if(file->cursor_x) file->cursor_x = 0; break; case KEY_END: /* go to end of line */ if(file->cursor_x < file->lines[file->cursor_y].len) file->cursor_x = file->lines[file->cursor_y].len; break; case KEY_LEFT: /* go to left one character */ if(file->cursor_x) file->cursor_x--; else if(file->cursor_y) { /* jump to end of previous line */ file->cursor_y--; file->cursor_x = file->lines[file->cursor_y].len; } break; case KEY_RIGHT: /* go right one character */ if(file->cursor_x < file->lines[file->cursor_y].len) file->cursor_x++; else if(file->cursor_y < file->line_count-1) { /* jump to begin of next line */ file->cursor_y++; file->cursor_x = 0; } break; case KEY_DC: if(file->selected) { file->selected = 0; /* delete selection */ if(file->sel_begin_y == file->sel_end_y) { /* remove characters from current line */ removetext(file, file->sel_begin_x, file->sel_begin_y, file->sel_end_x - file->sel_begin_x); } else { int y; line_t *line; /* remove ending from the first line */ removetext(file, file->sel_begin_x, file->sel_begin_y, file->lines[file->sel_begin_y].len - file->sel_begin_x); /* copy ending from the last line to the end of first line */ line = &file->lines[file->sel_end_y]; if(file->sel_end_x < line->len) { inserttext(file, file->sel_begin_x, file->sel_begin_y, &line->buf[file->sel_end_x], line->len - file->sel_end_x); } /* remove all lines between the first and last line */ for(y=0; y < file->sel_end_y-file->sel_begin_y; y++) removeline(file, file->sel_begin_y+1); file->cursor_x = file->sel_begin_x; file->cursor_y = file->sel_begin_y; } } else if(file->cursor_x < file->lines[file->cursor_y].len) { /* remove character from cursor position */ removetext(file, file->cursor_x, file->cursor_y, 1); } break; case KEY_BACKSPACE: case '\b': case 127: if(file->cursor_x) { /* remove character from cursor position */ file->cursor_x--; removetext(file, file->cursor_x, file->cursor_y, 1); } else if(file->cursor_y) { line_t *line; /* move to the end of the previous line */ file->cursor_y--; file->cursor_x = file->lines[file->cursor_y].len; /* copy text from the next line to the end of current line */ line = &file->lines[file->cursor_y+1]; if(line->len) { inserttext(file, file->cursor_x, file->cursor_y, line->buf, line->len); } /* and remove the next line */ removeline(file, file->cursor_y+1); } break; case '\r': { line_t *line; /* add new line */ insertline(file, file->cursor_y+1); line = &file->lines[file->cursor_y]; if(file->cursor_x < line->len) { /* copy ending from the current line to the next line */ inserttext(file, 0, file->cursor_y+1, &line->buf[file->cursor_x], line->len - file->cursor_x); /* and remove ending from the current line */ removetext(file, file->cursor_x, file->cursor_y, line->len - file->cursor_x); } /* move cursor to the begin of the next line */ file->cursor_y++; file->cursor_x = 0; } break; case '\t': if(file->selected) { int y; /* indent lines */ for(y = file->sel_begin_y; y <= file->sel_end_y; y++) inserttext(file, 0, y, "\t", 1); break; } /* fall trought */ default: if((ch >= ' ' && ch < 256) || ch == '\t') { unsigned char buf = ch; /* insert character at cursor position */ inserttext(file, file->cursor_x, file->cursor_y, (const char *)&buf, 1); file->cursor_x++; } break; } cursormoved(file); setcursor(); refresh(); return 0; }