int main (void) { struct book library[MAXBKS]; int count = 0; int index; printf ("Please enter the book title.\n"); printf ("Preee [enter] at the start of a line to stop.\n"); while (count < MAXBKS && mygets (library[count].title, MAXTITL) != NULL && library[count].title[0] != '\0') { printf ("Now enter the author.\n"); mygets (library[count].author, MAXAUTL); printf ("Now enter the value,\n"); scanf ("%f", &library[count++].value); while (getchar () != '\n') continue; if (count < MAXBKS) printf ("Enter the next title.\n"); } if (count > 0) { printf ("Here is the list of your books: \n"); for (index = 0; index < count; index ++) printf ("%s by %s: $ %.2f\n", library[index].title, library[index].author, library[index].value); } else printf ("No books? Too bad.\n"); return 0; }
void *read_thread(void *arg) { char buff[100]; myprints("Enter a line of text: "); mygets(buff, sizeof(buff)); myprints("Read: "); myprints(buff); myprints("Enter a line of text: "); mygets(buff, sizeof(buff)); myprints("Read: "); myprints(buff); return NULL; }
int choice(int sfd,packet* pk,int* fd) { switch(pk->type) { case 0: get(pk,fd); break; case 1: mycd(pk); break; case 2: myls(pk); break; case 3: myputs(sfd,pk); break; case 4: mygets(pk,fd); break; case 5: myremove(pk); break; case 6: mypwd(pk); break; case 7: getend(fd); break; default: break; } return 0; }
main() { char string[80]; FH *in,*out; #ifdef AMIGA out=(FH *)Output(); #else out = stdout; #endif #ifdef AMIGA if((in=(FH *)Open("test.dat",MODE_OLDFILE))==NULL) #else if((in=(FH *)fopen("test.dat","r"))==NULL) #endif { printf("Unable to open test.dat\n"); exit(1); } while(mygets(in,string,80)) myputs(out,string); #ifdef AMIGA Close(in); #else fclose(in); #endif }
int assemble(uint32_t *out_buf, char *asm_buf) { char asm_line[COL_MAX]; char term0[COL_MAX]; output_alias = out_buf; while (mygets(asm_line, asm_buf, COL_MAX) != NULL) { if (set_term0(asm_line, term0) == 1) { if (is_comment(asm_line, term0)) { // blank(comment) } else if (is_directive(asm_line, term0)) { exec_directive(asm_line, term0); } else if (is_label(asm_line, term0)) { register_label(asm_line, term0); } else { encode_and_output(asm_line, term0); } } else { // blank(empty line) } input_line_cnt++; } resolve_label(); return output_cnt; }
Config *config_load(const char *filename) { Config *config; FILE *f; char buffer[BUFFER_SIZE]; in_port_t port; int i; pmalloc(config, sizeof *config); if((f = fopen(filename, "r")) == NULL) { fprintf(stderr, "Unable to load %s\n", filename); exit(EXIT_FAILURE); } /* Nom. */ mygets(config->name, PLAYER_NAME_SIZE, f); printf("client: \'%s\'\n", config->name); /* Remplissage de '\0' */ for(i = strlen(config->name); i < PLAYER_NAME_SIZE; i++) config->name[i] = '\0'; /* Port. */ mygets(buffer, BUFFER_SIZE, f); port = (in_port_t)atoi(buffer); printf("port: \'%d\'\n", port); /* IP. */ mygets(buffer, BUFFER_SIZE, f); printf("ip: \'%s\'\n", buffer); /* Ip + Port au bon format. */ server_resolve_host(&config->ip, buffer, port); fclose(f); return config; }
void get_label() { char temp[MAX_LABEL_LENGTH+1]; do { myprintf("Volume label (11 characters, ENTER for none)? ",0); mygets(temp,MAX_LABEL_LENGTH+1); } /* end do. */ while (check_label(temp)); strcpy(Label, temp); } /* end get_label. */
void ParseUnicodeFile(FILE *in) { char buffer[600]; int ch, mask, base, lc, i; char *pt; while ( mygets(in,buffer)!=NULL ) { ch = strtol(buffer,NULL,16); if ( ch==0x1ec0 ) ch = 0x1ec0; pt = buffer+4; if ( strncmp(pt,";LATIN ",7)!=0 ) continue; pt += 7; if ( strncmp(pt,"CAPITAL ",8)==0 ) { lc = 0; pt += 8; } else if ( strncmp(pt,"SMALL ",6)==0 ) { lc = 1; pt += 6; } else continue; if ( strncmp(pt,"LETTER ",7)!=0 ) continue; pt += 7; base = *pt++; if ( lc ) base = tolower(base); if ( strncmp(pt," WITH ",6)!=0 ) continue; pt += 6; mask = 0; forever { for ( i=0; names2[i].name!=NULL; ++i ) { if ( strncmp(pt,names2[i].name,strlen(names2[i].name))==0 ) break; } if ( names2[i].name==NULL || names2[i].mask==0 ) goto continue_2_loop; mask |= names2[i].mask; pt += strlen(names2[i].name); while ( *pt!=';' && !(*pt==' ' && pt[1]=='A' && pt[2]=='N' && pt[3]=='D' && pt[4]==' ')) ++pt; if ( *pt==';' ) break; else pt += 5; } AddTransform(base,mask,0,ch); continue_2_loop:; } fclose(in); }
static void comm_comin( /* read string from command line */ char *buf, char *prompt ) { putc(COM_COMIN, devout); if (prompt == NULL) putc(0, devout); else { putc(1, devout); myputs(prompt, devout); } fflush(devout); if (getc(devin) != COM_COMIN) reply_error("comin"); mygets(buf, devin); getstate(); }
/* *选择:0<往链接写报文>/1<cd>/2<myls>/3<打开文件>/4<读取服务端文件内容并发给客户端> *******5<移除文件>/6<获取当前路径>/7<关闭链接>/8<退出循环> * */ int choice(node* n,packet* pk,int* running,char *filedir) { switch(pk->type) { case 0: put(pk,n->put_fd); break; case 1: mycd(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 2: myls(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 3: myputs(pk,&(n->put_fd),filedir); send_pk(n->accept_fd,pk); break; case 4: mygets(n->accept_fd,pk,n->curdir); break; case 5: myremove(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 6: mypwd(pk,n->curdir); send_pk(n->accept_fd,pk); break; case 7: putend(&(n->put_fd)); break; case 8: *running=0; break; default:break; } return 0; }
cDisplay() { disp_pos = 0; buffer1 = 0; j = 0; k = 0; cls(); printf(str1,0x0724,10); printf(str2,0x091e,23); putch('\r'); mygets(command); if (equal(command, "date") == 1) { cls(); printf("Today is ",0x0c14,9); time(); printf("Press any key to return...",0x0d14,26); getch(&ret); return; } else if (equal(command, "run") == 1) { cDo(); return; } else if (equal(command, "para") == 1) { parallelDo(); return; } else { cls(); printf("Error:Illegal instruction\n",0x0a14,26); printf("Press any key to return...",0x0b14,26); getch(&ret); return; } }
int main(int argc, char *argv[]) { char ans[2]; char *fileargs[64]; char *optargs[64]; int n_options; int index; int help_flag = 0; #ifdef __TURBOC__ setvect(0x23, ctrlc_hndlr); #else _dos_setvect(0x23, ctrlc_hndlr); #endif atexit(on_exit); n_options = classify_args(argc, argv, fileargs, optargs); for (index=0;index<n_options;index++) { if (optargs[index][0] == '?') help_flag=1; else { myprintf("Invalid parameter - /",0); myprintf(optargs[index],0); /* removed strupr */ myprintf("\r\n",0); exit(1); } /* end else. */ } /* end for. */ if (help_flag) { myprintf("\r\nLABEL Version " VERSION "\r\n", 0); myprintf("Creates, changes or deletes the volume label of a disk.\r\n",0); myprintf("\r\n",0); myprintf("Syntax: LABEL [drive:][label] [/?]\r\n",0); myprintf(" [drive:] Specifies which drive you want to label\r\n",0); myprintf(" [label] Specifies the new label you want to label the drive\r\n",0); myprintf(" /? Displays this help message\r\n",0); return 0; } /* end if. */ do_cmdline(argc, argv); if (*Drive == '?') /* If no drive specified, use current. */ GetDrive(); /* Save current directory and move to root. */ GetCurDir(curdir); if (curdir[0] != 0) { *rootdir = *Drive; SetCurDir(rootdir); } /* end if. */ /* If no label was specified, show current one first and then get new one. */ if (*Label == '\0') { disp_label(); get_label(); } /* end if. */ /* If they entered an empty label, then ask them if they want to */ /* delete the existing volume label. */ if ((*Label == '\0') && (!NoLabel)) { do { myprintf("\nDelete current volume label (Y/N)? ",0); mygets(ans,2); /* WHY not use getch? ??? */ } /* end do. */ while (((*ans=(char)toupper(*ans)) != 'Y') && (*ans != 'N')); if (toupper(*ans) == 'N') exit(1); } /* end if. */ /* Delete the old volume label. */ del_label(); /* Create the new one, if there is one to create. */ if (*Label != '\0') { if (make_label()) { exit(1); } /* end if. */ } /* end if. */ exit(0); return 0; } /* end main. */
/**************************************************** * 实现功能:添加学生信息 * * 输入参数:教师节点和学生节点 * * 返回值 :无 * ****************************************************/ void add_stu_message(TEA_PLINK tea_head, STU_PNODE stu_head) { system("clear"); if(strcmp(tea_head->tea_data.head_teacher, "no") == 0) //这里不能写成if(strcmp(...,"yes") != 0) 思考一下为什么 { printf("\n抱歉,您的权限不够,不能添加学生.\n"); return; } STU stu_data; printf("\n请输入学生id(6位数字):"); scanf("%d", &stu_data.id); while(getchar() != '\n'); if(stu_data.id < 100000 || stu_data.id > 999999) { system("clear"); printf("\n学生id输入错误,添加学生信息失败.\n"); return; } STU_PNODE find = find_stu_pre_node(stu_head, stu_data.id); if(find != NULL) { system("clear"); printf("id已存在.添加学生信息失败.\n"); return; } printf("请输入添加的学生姓名(不大于20个字符):"); char new_name[NAME_MAX]; mygets(new_name, NAME_MAX); strcpy(stu_data.name, new_name); printf("请输入添加的学生性别('m'或'f'):"); stu_data.sex = toupper(getchar()); if(stu_data.sex != 'M' && stu_data.sex != 'F') { system("clear"); printf("\n性别输入错误,添加学生信息失败.\n"); return; } printf("请输入添加的学生年龄(1-150):"); scanf("%d", &stu_data.age); while(getchar() != '\n'); if(stu_data.age > 150 || stu_data.age < 1) { system("clear"); printf("\n年龄输入小于1岁或超过150岁,添加学生信息失败.\n"); return; } printf("请输入该学生所在班级(1-20):"); scanf("%d", &stu_data.class_num); while(getchar() != '\n'); if(stu_data.class_num > 20 || stu_data.class_num < 1) { system("clear"); printf("\n班级输入错误,添加学生信息失败.\n"); return; } printf("请输入该学生的三门成绩:\n"); printf("数学成绩(0-100):"); scanf("%f", &stu_data.math_score); while(getchar() != '\n'); if(stu_data.math_score > 100 || stu_data.math_score < 0) { system("clear"); printf("\n数学成绩输入错误,添加学生信息失败.\n"); return; } printf("语文成绩(0-100):"); scanf("%f", &stu_data.chinese_score); while(getchar() != '\n'); if(stu_data.chinese_score > 100 || stu_data.chinese_score < 0) { system("clear"); printf("\n语文成绩输入错误,添加学生信息失败.\n"); return; } printf("C语言成绩(0-100):"); scanf("%f", &stu_data.c_program_score); while(getchar() != '\n'); if(stu_data.c_program_score > 100 || stu_data.c_program_score < 0) { system("clear"); printf("\nC语言成绩输入错误,添加学生信息失败.\n"); return; } //计算三科成绩总分 stu_data.total_score = stu_data.math_score + stu_data.chinese_score + stu_data.c_program_score; //密码初始化: strcpy(stu_data.passwd, "123456"); head_insert_stu_node(stu_head, stu_data); system("clear"); printf("\n学生信息增加完成!新添加学生密码初始化为123456.\n"); }
/**************************************************** * 实现功能:更改学生成绩 * * 输入参数:教师节点和学生节点 * * 返回值 :无 * ****************************************************/ void change_stu_score(TEA_PLINK tea_head, STU_PNODE stu_head) { system("clear"); if(stu_head->next == NULL) { printf("\n学生信息为空,没有数据可以更改.\n"); return; } output_stu_node(stu_head); printf("\n请输入要更改的学生id:"); int stu_id; scanf("%d",&stu_id); while(getchar() != '\n'); STU_PNODE find = find_stu_pre_node(stu_head, stu_id); if(NULL == find) { system("clear"); printf("该学生id不存在,成绩更改失败.\n"); return; } char change_subject[SUBJECT_MAX]; printf("请输入您要更改的科目(\"math\" or \"chinese\" or \"c_program\"):"); mygets(change_subject, SUBJECT_MAX); if((strcmp(change_subject, "math") != 0) && (strcmp(change_subject, "chinese") != 0) && (strcmp(change_subject, "c_program") != 0)) { system("clear"); printf("\n科目名称输入错误,成绩更改失败.\n"); return; } if(0 != strcmp(tea_head->tea_data.subject, change_subject) && strcmp(tea_head->tea_data.head_teacher, "no") == 0) { system("clear"); printf("\n抱歉,您没有权限更改%s科目成绩.\n",change_subject); return; } int new_score; if(strcmp(change_subject, "math") == 0) { printf("请输入新的分数:"); scanf("%d", &new_score); while(getchar() != '\n') if(new_score < 0 || new_score > 100) { system("clear"); printf("\n新的分数输入错误,成绩更改失败.\n"); return; } find->next->stu_data.math_score = new_score; find->next->stu_data.total_score = find->next->stu_data.chinese_score + find->next->stu_data.c_program_score + new_score; } if(strcmp(change_subject, "chinese") == 0) { printf("请输入新的分数(1-100):"); scanf("%d", &new_score); while(getchar() != '\n') if(new_score < 0 || new_score > 100) { system("clear"); printf("\n新的分数输入错误,成绩更改失败.\n"); return; } find->next->stu_data.chinese_score = new_score; find->next->stu_data.total_score = find->next->stu_data.math_score + find->next->stu_data.c_program_score + new_score; } if(strcmp(change_subject, "c_program") == 0) { printf("请输入新的分数:"); scanf("%d", &new_score); while(getchar() != '\n') if(new_score < 0 || new_score > 100) { system("clear"); printf("\n新的分数输入错误,成绩更改失败.\n"); return; } find->next->stu_data.c_program_score = new_score; find->next->stu_data.total_score = find->next->stu_data.math_score + find->next->stu_data.chinese_score + new_score; } system("clear"); printf("\n学生%s的%s科目成绩更改成功.\n" ,find->next->stu_data.name ,change_subject); }
int main (int argc, char *argv[]) { int i,j,nb_files,nb_files_orig; myFH *log_file[argc-1]; char *log_buffer[argc-1]; char *log_scan[argc-1]; char *log_month[argc-1]; char ref_date_buf[DATE_SIZE+1]; char *tmp_date_buf[argc-1]; char *log_date; int year,day,hour,minut,second; char month[3]; struct tm *date; time_t start=0; time_t start_new; char *trans_digits[60]; char *trans_year[200]; char months[24]="anebarprayunulugepctovec"; /* print usage if necessary */ if (argc == 1) { fprintf(stderr,"usage: %s logfile1 logfile2 ...\nmergelog %s Copyright (C) 2000-2001 Bertrand Demiddelaer\n",argv[0],VERSION); exit(1); } #ifdef USE_ZLIB /* check if there are enough gunzip buffers */ if(argc>MAX_FILES) { fputs("too many gzipped log files, aborting\n",stderr); exit(1); } #endif /* open log files */ for (i=1;i<argc;i++) { log_file[i-1]=myopen(argv[i],"r"); if (log_file[i-1] == NULL) { fprintf(stderr,"can't open %s, aborting\n",argv[i]); exit(1); } } /* feed arrays which will be used to translate dates */ for(i=0;i<60;i++) { trans_digits[i]=malloc(3); if (trans_digits[i] == NULL) { perror("malloc"); exit(1); } sprintf(trans_digits[i],"%.2d",i); } for (i=70;i<200;i++) { trans_year[i]=malloc(5); if (trans_year[i] == NULL) { perror("malloc"); exit(1); } sprintf(trans_year[i],"%.4d",1900+i); } /* malloc for the 'tm' structure */ date=malloc(sizeof(struct tm)); if (date == NULL) { perror("malloc"); exit(1); } /* init things for each log file and get the older date to start with */ nb_files=argc-1; for (i=0;i<argc-1;i++) { #ifdef USE_ZLIB /* init the gzip buffer */ f_buf[i]=malloc(GZBUFFER_SIZE); if (f_buf[i] == NULL) { perror("malloc"); exit(1); } f_cp[i]=f_buf[i]+GZBUFFER_SIZE; #endif /* init log_month buffers the first 2 digits will be used for the number of the month the last 2 digits will be used for the two letters of the month */ log_month[i]=calloc(4,1); if (log_month[i] == NULL) { perror("calloc"); exit(1); } /* get the first line of the log file and init log_scan */ log_buffer[i]=malloc(BUFFER_SIZE); log_scan[i]=log_buffer[i]+SCAN_OFFSET; if (log_buffer[i] == NULL) { perror("malloc"); exit(1); } /* init the tmp_date_buf */ tmp_date_buf[i]=malloc(DATE_SIZE+1); if (tmp_date_buf[i] == NULL) { perror("malloc"); exit(1); } memset(tmp_date_buf[i]+DATE_SIZE,'0',1); /* is it an empty file ? */ if (mygets(log_buffer[i],BUFFER_SIZE,log_file[i],i) != NULL ) { /* get the date pointers */ log_date=memchr(log_scan[i],'[',SCAN_SIZE); if (log_date == NULL) { fprintf(stderr,"abort due to a problem with %s:\n%s\n",argv[i+1],log_buffer[i]); exit(1); } /* put the date in the tmp_date_buf */ for (j=0;((j == 12)&&(memcmp(months+2*j,log_date+5,2) != 0));j++); if (j == 12) { fprintf(stderr,"abort due to a problem with %s:\n%s\n",argv[i+1],log_buffer[i]); exit(1); } memcpy(log_month[i],trans_digits[j],2); memcpy(log_month[i]+2,months+2*j,2); memcpy(tmp_date_buf[i],log_date+8,4); memcpy(tmp_date_buf[i]+4,trans_digits[j],2); memcpy(tmp_date_buf[i]+6,log_date+1,2); memcpy(tmp_date_buf[i]+8,log_date+13,2); memcpy(tmp_date_buf[i]+10,log_date+16,2); memcpy(tmp_date_buf[i]+12,log_date+19,2); /* extract the date of this first line */ if (sscanf(log_date+1,"%d/%3c/%d:%d:%d:%d",&day,month,&year,&hour,&minut,&second) < 6) { fprintf(stderr,"abort due to a problem with %s:\n%s\n",argv[i+1],log_buffer[i]); exit(1); } /* put this date in a 'tm' structure */ date->tm_sec=second; date->tm_min=minut; date->tm_hour=hour; date->tm_mday=day; date->tm_year=year-1900; date->tm_isdst=-1; for (j=0;((j<12)&&(memcmp(months+2*j,month+1,2) != 0));j++); if (j == 12) { fprintf(stderr,"abort due to a problem with %s:\n%s\n",argv[i+1],log_buffer[i]); exit(1); } date->tm_mon=j; memcpy(log_month[i],trans_digits[j],2); memcpy(log_month[i]+2,months+2*j,2); memcpy(tmp_date_buf[i]+4,trans_digits[j],2); /* convert it in the 'seconds since 00:00:00, Jan 1, 1970' format */ start_new=mktime(date); /* keep the older date */ if ((start_new < start)||(start == 0)) { start=start_new; } } else { /* this is an empty file */ nb_files--; *(tmp_date_buf[i])='9'; } } /* exit if we have only empty files */ if (nb_files == 0) { exit(0); } /* init 'start', 'date' and 'ref_date_buf' */ free(date); start--; date=localtime(&start); memcpy(ref_date_buf,trans_year[date->tm_year],4); memcpy(ref_date_buf+4,trans_digits[date->tm_mon],2); memcpy(ref_date_buf+6,trans_digits[date->tm_mday],2); memcpy(ref_date_buf+8,trans_digits[date->tm_hour],2); memcpy(ref_date_buf+10,trans_digits[date->tm_min],2); memcpy(ref_date_buf+12,trans_digits[date->tm_sec],2); memset(ref_date_buf+DATE_SIZE,'0',1); /* start to compute since this date */ nb_files_orig=argc-1; for(;;) { /* update 'start' 'date' and 'ref_date_buf' */ start++; if (date->tm_sec < 59) { date->tm_sec++; memcpy(ref_date_buf+12,trans_digits[date->tm_sec],2); } else { date->tm_sec=0; memset(ref_date_buf+12,'0',2); if (date->tm_min < 59) { date->tm_min++; memcpy(ref_date_buf+10,trans_digits[date->tm_min],2); } else { date->tm_min=0; memset(ref_date_buf+10,'0',2); if (date->tm_hour < 23) { date->tm_hour++; memcpy(ref_date_buf+8,trans_digits[date->tm_hour],2); } else { memset(ref_date_buf+8,'0',2); date=localtime(&start); memcpy(ref_date_buf,trans_year[date->tm_year],4); memcpy(ref_date_buf+4,trans_digits[date->tm_mon],2); memcpy(ref_date_buf+6,trans_digits[date->tm_mday],2); } } } /* scan this date for each log file */ for(i=0;i<nb_files_orig;i++) { /* write the log lines until the reference date is older than the log line */ for(;;) { /* if the reference date is older than the log line then go to next file we use here a faster implementation than something like: if (memcmp(ref_date_buf,tmp_date_buf[i],DATE_SIZE)<0) break; */ for(j=0;(j<DATE_SIZE)&&(*(ref_date_buf+j)==*(tmp_date_buf[i]+j));j++); if (*(ref_date_buf+j)<*(tmp_date_buf[i]+j)) break; /* write the log line faster than a puts and we are sure to find a '\0' in log_buffer[i] */ write(1,log_buffer[i],(size_t)((char *)memchr(log_buffer[i],0,BUFFER_SIZE)-log_buffer[i])); /* is it an end of file ? */ if (mygets(log_buffer[i],BUFFER_SIZE,log_file[i],i) == NULL) { /* close all log files and exit if all end of files are reached */ if (--nb_files == 0) { for (j=0;j<argc-1;j++) { myclose(log_file[j]); } exit(0); } /* we don't want anymore output from this file we put a '9' at the beginning of the year, to fail the date test it's dirty, but it's fast, and doesn't need an extra test */ *(tmp_date_buf[i])='9'; break; } else { /* prepare the new pointer for the date test */ log_date=memchr(log_scan[i],'[',SCAN_SIZE); if (log_date != NULL) { /* convert the log line month if necessary copy the new date in the buffer */ if ((*(log_month[i]+2)==*(log_date+5))&&(*(log_month[i]+3)==*(log_date+6))) { memcpy(tmp_date_buf[i]+4,log_month[i],2); memcpy(tmp_date_buf[i],log_date+8,4); memcpy(tmp_date_buf[i]+6,log_date+1,2); memcpy(tmp_date_buf[i]+8,log_date+13,2); memcpy(tmp_date_buf[i]+10,log_date+16,2); memcpy(tmp_date_buf[i]+12,log_date+19,2); } else { for (j=0;((j<12)&&(memcmp(months+2*j,log_date+5,2) != 0));j++); if (j == 12) { fprintf(stderr,"problem with %s:\n%s\ncontinuing...\n",argv[i+1],log_buffer[i]); } else { memcpy(log_month[i],trans_digits[j],2); memcpy(log_month[i]+2,months+2*j,2); memcpy(tmp_date_buf[i]+4,trans_digits[j],2); memcpy(tmp_date_buf[i],log_date+8,4); memcpy(tmp_date_buf[i]+6,log_date+1,2); memcpy(tmp_date_buf[i]+8,log_date+13,2); memcpy(tmp_date_buf[i]+10,log_date+16,2); memcpy(tmp_date_buf[i]+12,log_date+19,2); } } } else { fprintf(stderr,"problem with %s:\n%s\ncontinuing...\n",argv[i+1],log_buffer[i]); } } } } } /* never reached */ exit(1); }
int main (int argc, char **argv) { FILE *FInPtr, *FOutPtr; long int *record_locs, currLoc, endOfFile; char *buffer; int2 lines, namecount, i, j; nameEntry nameStruct; int c, errflag; char *db_path; /* initialize globals */ Vflag=0; errflag=0; #ifdef __STACK_CHECK__ _beginStackCheck(); #endif assert(sizeof(int2)==2); assert(sizeof(int4)==4); /* Get database path: If DESCDB is set, use it, otherwise use DATABASE */ if (getenv("DESCDB") == 0) { if ((db_path = strdup(DATABASE)) == 0) { fprintf(stderr,"couldn't allocate path variable\n"); exit (-1); } } else { if ((db_path = strdup(getenv("DESCDB"))) == 0) { fprintf(stderr,"couldn't allocate path variable\n"); exit (-1); } } /* parse command line */ while ((c = getopt(argc, argv, "hV")) != EOF) { switch (c) { case 'V': version(basename(argv[0])); break; case 'h': /*FALLTHROUGH*/ default: errflag++; break; } } if (errflag || (argc-optind != 1)) { free (db_path); usage(basename(argv[0])); } /* open input and output files */ if ((buffer = malloc (MAX_LINE_LENGTH)) == NULL) { fprintf(stderr,"couldn't allocate line buffer\n"); free (db_path); exit (-1); } if ((FInPtr = fopen(argv[argc-1],"r")) == NULL) { fprintf(stderr,"Error opening %s; exiting.\n",argv[argc-1]); free (db_path); free(buffer); exit(1); } if ((FOutPtr = fopen(db_path,"wb+")) == NULL) { fprintf(stderr,"Error opening database file %s; exiting.\n",db_path); free (db_path); free(buffer); exit(1); } /* Compile array of names */ lines = 0; namecount = 0; /* space for # of array entries */ fwrite((void *)&namecount,sizeof(namecount),1,FOutPtr); while(mygets(buffer,&lines,FInPtr) != -1) { if (!strncmp(buffer,NAME,FIELD_LEN)) { /* found a match */ strncpy(nameStruct.name,&buffer[FIELD_LEN],NAME_LEN-1); nameStruct.name[NAME_LEN-1] = '\0'; fwrite((void *)&nameStruct,sizeof(nameStruct),1,FOutPtr); namecount++; } } if ((record_locs = malloc (namecount*sizeof(long int)))==NULL) { fprintf(stderr,"malloc of record_locs failed (%ld bytes); exiting\n", (long) namecount*sizeof(long int)); exit(-1); } rewind(FInPtr); buffer[0] = '\0'; lines = 0; fputc('\t',FOutPtr); /* Increment to first field */ while (strncmp(buffer,NAME,FIELD_LEN)) /* found a match! */ mygets(buffer,&lines,FInPtr); { /* BUGBUG 22/10/95 Soenke Behrens */ /* ORCA/C does not advance the file position indicator */ /* correctly after above fputc(). This tries to remedy */ /* the situation. Take out once library has been fixed */ fprintf(FOutPtr,"Junk"); fseek(FOutPtr,-4,SEEK_CUR); } /* Write out records and keep track of their file offsets */ for (i = 0; i < namecount; i++) { record_locs[i] = ftell(FOutPtr); /* print out <Version>, <Shell>, <Author>, <Contact>, <Where>, <FTP> */ for (j = 0; j < FIELD_COUNT-1; j++) { buffer[FIELD_LEN] = '\0'; mygets(buffer,&lines,FInPtr); fprintf(FOutPtr,"%s\n",&buffer[FIELD_LEN]); } /* handle <description> field */ for (;;) { if (mygets(buffer,&lines,FInPtr) == -1) break; if (!strncmp(buffer,NAME,FIELD_LEN)) break; fprintf(FOutPtr,"%s ",buffer); } fputc('\n',FOutPtr); } endOfFile = ftell(FOutPtr); fflush(FOutPtr); /*gdr 1*/ rewind(FOutPtr); fwrite((void *)&namecount,sizeof(namecount),1,FOutPtr); fflush(FOutPtr); /*gdr 1*/ /* time to go through the record_locs array and backpatch in */ /* all the record locations. A little slower than necessary */ /* perhaps, but it gets the job done. */ for (i = 0; i < namecount; i++) { fread(&nameStruct,sizeof(nameStruct),1,FOutPtr); fseek(FOutPtr,-(sizeof(nameStruct)),SEEK_CUR); nameStruct.offset = record_locs[i]; fwrite((void *)&nameStruct,sizeof(nameStruct),(size_t) 1,FOutPtr); fflush(FOutPtr); } fseek(FOutPtr,endOfFile,SEEK_SET); fclose(FOutPtr); #ifdef __GNO__ /* change the filetype of the database to BIN */ changeToBin(db_path); #endif free(db_path); free(record_locs); free(buffer); #ifdef __STACK_CHECK__ fprintf(stderr,"stack usage: %d bytes\n", _endStackCheck()); #endif return 0; }
int main(int argc,char *argv[]) { extern char *optarg; extern int optind; int ch; struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"version", no_argument, 0, 'V'}, {"debug", no_argument, 0, 'd'}, {"quiet", no_argument, 0, 'q'}, {"log", required_argument, 0, 'l'}, {"command", required_argument, 0, 'c'}, #ifdef HAVE_LIBMICROHTTPD {"port", required_argument, 0, 'p'}, #endif {0, 0, 0, 0} }; int contest_offset; char *tmp; char buf[BUF_SIZE]; int ttylog,lu,i,q; struct winsize ws; #ifdef HAVE_LIBMICROHTTPD unsigned int port = 0; #endif regmatch_t pmatch[2]; char defcmd[] = "dnetc", logfile[128] = "stdout"; int pos_cpu[MAX_CPU]; char p[] = "a"; int log_fd; /* check arguments */ #ifdef HAVE_LIBMICROHTTPD while ((ch = getopt_long(argc, argv, "hdVql:c:p:", long_options, NULL)) != -1) #else while ((ch = getopt_long(argc, argv, "hdVql:c:", long_options, NULL)) != -1) #endif { switch(ch) { case 'd': dflag = 1; break; case 'V': printf("%s\n",GKRELLDNET_VERSION); exit(0); break; #ifdef HAVE_LIBMICROHTTPD case 'p': port = atoi(optarg); break; #endif case 'q': qflag = 1; break; case 'c': nargc = get_arg(&nargv,optarg); break; case 'l': strcpy(logfile,optarg); break; case 'h': default: usage(argv[0]); } } if(argc - optind != 0) usage(argv[0]); /* continue in background if in quiet mode */ if(qflag == 1 && ((new_pid = fork()) != 0)) { if(new_pid < 0) clean_and_exit("forking daemon",1); else exit(0); } /* default command line */ if(nargv == NULL) nargc = get_arg(&nargv,defcmd); /* change output to logfile */ if(strcmp(logfile,"stdout") != 0) { if((log_fd = open(logfile, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) clean_and_exit("opening logfile",1); if(dup2(log_fd,1) == -1) clean_and_exit("dup2",1); } ttylog = isatty(1); /* creat shared memory segment */ if((shmid = my_shmcreate(sizeof(struct dnetc_values),IPC_CREAT|IPC_EXCL|0644)) == -1) clean_and_exit("shmget",1); if((int) (shmem = shmat(shmid,0,0)) == -1) clean_and_exit("shmat",1); /* init shared memory content */ shmem->running = TRUE; strcpy(shmem->contest,"???"); shmem->cmode = CRUNCH_RELATIVE; shmem->wu_in = shmem->wu_out = 0; shmem->n_cpu = 1; for(i=0;i<MAX_CPU;i++) shmem->val_cpu[i] = 0; /* precompile regex */ if(regcomp(&preg_in,"[0-9]+.packets?.+remains?.in",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_out,"[0-9]+.packets?(.+in.buff-out|.\\(.+stats?.units?\\).(are|is).in)",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_contest,"[A-Z0-9-]{3,6}(.#[a-z])?:.Loaded",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_proxy,"((Retrieved|Sent).+(stat..unit|packet)|Attempting.to.resolve|Connect(ing|ed).to)",REG_EXTENDED) !=0) clean_and_exit(NULL,1); contest_offset = 0; if(regcomp(&preg_absolute,"(#[0-9]+: [A-Z0-9-]{3,6}|[A-Z0-9-]{3,6}(.#[a-z])?):.+\\[[,0-9]+\\]",REG_EXTENDED) != 0) clean_and_exit(NULL,1); if(regcomp(&preg_cruncher,"[0-9]+.cruncher.*started",REG_EXTENDED) != 0) clean_and_exit(NULL,1); regex_flag = 1; /* obtain a pseudo-terminal */ ws.ws_col = 132; ws.ws_row = 10; ws.ws_xpixel = ws.ws_ypixel = 0; if((openpty(&fd,&tty_fd,NULL,NULL,&ws)) == -1) clean_and_exit("openpty",1); /* start dnet client and start reading tty */ if((fils = fork()) == -1) clean_and_exit("fork",1); if(fils == 0) { /* change to dnetc directory */ change_dir(); /* start dnet client */ if(dup2(tty_fd,1) == -1) clean_and_exit("dup2",1); if(execvp(nargv[0],nargv) == -1) clean_and_exit("execvp",1); } #ifdef HAVE_LIBMICROHTTPD /* start http server */ if(port > 0) { struct sockaddr_in so; so.sin_family = AF_INET; so.sin_port = htons(port); so.sin_addr.s_addr = htonl(INADDR_LOOPBACK); http_daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, port, NULL, NULL, &answer_to_connection, shmem, MHD_OPTION_SOCK_ADDR, &so, MHD_OPTION_END); if(http_daemon == NULL) clean_and_exit("MHD_start_daemon",1); } #endif /* set signal handler */ if(signal(SIGHUP,got_signal) == SIG_ERR || signal(SIGINT,got_signal) == SIG_ERR || signal(SIGQUIT,got_signal) == SIG_ERR || signal(SIGCHLD,got_signal) == SIG_ERR || signal(SIGTERM,got_signal) == SIG_ERR) clean_and_exit("signal",1); /* some more init */ for(i=0;i<MAX_CPU;i++) shmem->val_cpu[i] = 0; /* main loop */ while((lu = mygets(fd,buf,BUF_SIZE)) > 0) { if(dflag) fprintf(stderr,"buf[0] = %x\n<--\n%s\n-->\n",buf[0],buf); if(buf[1] == '.' || buf[lu-1] != '\n') { if(dflag) fprintf(stderr,"lu: %02d, ",lu); /* fix line with two 0x0d char */ if((tmp = strchr(&buf[1],0x0d)) != NULL) lu = tmp-buf; /* skip line with proxy comm. */ q = regexec(&preg_proxy,buf,1,pmatch,0); if(q != 0) { /* check if line match absolute crunch-o-meter */ if(regexec(&preg_absolute,buf,1,pmatch,0) == 0) { /* set crunch-o-meter mode */ shmem->cmode = CRUNCH_ABSOLUTE; /* read CPU num */ tmp = strchr(&buf[pmatch[0].rm_so],'#'); if(tmp != NULL) { if(isdigit(tmp[1])) i = strtol(&buf[pmatch[0].rm_so+1],(char **) NULL,10) - 1; else if(islower(tmp[1])) i = tmp[1] - 'a'; /* avoid core dump */ i %= MAX_CPU; } else i = 0; /* read k(keys|nodes) */ shmem->val_cpu[i] = extract_val(&buf[pmatch[0].rm_so]); if(dflag) { fprintf(stderr,"\ncpu = %d, %llu nodes|keys\n",i,shmem->val_cpu[i]); fprintf(stderr,"found: %s\n",&buf[pmatch[0].rm_so]); } } else { /* set crunch-o-meter mode */ shmem->cmode = CRUNCH_RELATIVE; for(i=0;i<shmem->n_cpu;i++) { if(shmem->n_cpu != 1) { p[0] = 'a' + i; if((tmp = strstr(buf,p)) != NULL) pos_cpu[i] = tmp - buf; } else pos_cpu[i] = lu - 1; shmem->val_cpu[i] = (pos_cpu[i] - (pos_cpu[i]/8)*3) * 2; if(shmem->val_cpu[i] > 100) shmem->val_cpu[i] = 100; if(dflag) fprintf(stderr,"cpu%d: %llu,",i,shmem->val_cpu[i]); } if(dflag) fprintf(stderr,"\n"); } } if(!qflag && (ttylog || q == 0)) { printf("%s",buf); fflush(stdout); } } else { if(regexec(&preg_in,buf,1,pmatch,0) == 0) shmem->wu_in = strtol(&buf[pmatch[0].rm_so],NULL,10); if(regexec(&preg_out,buf,1,pmatch,0) == 0) shmem->wu_out = strtol(&buf[pmatch[0].rm_so],NULL,10); if(regexec(&preg_contest,buf,1,pmatch,0) == 0) strncpy(shmem->contest,&buf[pmatch[0].rm_so+contest_offset],3); if(regexec(&preg_cruncher,buf,1,pmatch,0) == 0) { shmem->n_cpu = strtol(&buf[pmatch[0].rm_so],NULL,10); /* too many crunchers */ if(shmem->n_cpu > MAX_CPU) { fprintf(stderr,"dnetw: too many crunchers\n"); clean_and_exit(NULL,1); } } if(dflag) fprintf(stderr,"contest = %s, in = %d, out = %d, n_cpu = %d\n",shmem->contest, shmem->wu_in, shmem->wu_out, shmem->n_cpu); if(!qflag) { printf("%s",buf); fflush(stdout); } } } clean_and_exit(NULL,0); return 0; }