// Close statement void statement::close() { if (is_open()) { // Free parameters param_it it; for(it = m_params.begin();it != m_params.end();it++) delete it->second; m_params.clear(); // Free result if any free_results(); // Free handle SQLFreeHandle(SQL_HANDLE_STMT, stmt_h); stmt_h = NULL; b_open = false; } }
/** * 結果を保存する関数 * @param char *first * @param char *second * @param double overlap */ void insert_result(char *first, char *second, double indicator) { RESULTS **tmp; // 結果の格納配列の大きさが足りないとき if (n_results >= n_results_max) { n_results_max *= 2; // 倍の大きさにする // メモリを再度割り当てる if ((tmp = (RESULTS **)realloc(results, sizeof(RESULTS *) * n_results_max)) == NULL) { fprintf(stderr, "Memory Reallocation Error\n"); free_results(); exit(1); } else { results = tmp; // 新しい領域を指す } } // 結果の文字列のペアを確保 results[n_results] = (RESULTS *)malloc(sizeof(RESULTS)); results[n_results]->first = (char *)malloc(sizeof(char) * (strlen(first) + 1)); results[n_results]->second = (char *)malloc(sizeof(char) * (strlen(second) + 1)); if (results[n_results]->first == NULL || results[n_results]->second == NULL) { fprintf(stderr, "Memory Allocation Error\n"); exit(1); } // 文字列のコピー memcpy(results[n_results]->first, first, sizeof(char) * (strlen(first) + 1)); memcpy(results[n_results]->second, second, sizeof(char) * (strlen(second) + 1)); results[n_results]->indicator = indicator; // 強度 n_results++; // 結果の数の更新 }
int main(int argc, char *argv[]) { char *file_name = " Usage: -f filename"; CALC_MODE mode = CM_OVERLAP; // 計算する方式 int is_chaged = 1; // 計算方法に変更があったか ACTION act; // 操作を選択 int threshold = 2; // 計算に用いる出現頻度の最小値 int i; init(); // 要素の確保 // コマンドライン引数の読み込み for (i = 0; i < argc; i++) { if (strcmp(argv[i], "-f") == 0 && i + 1 < argc) // 次の文字列があれば file_name = argv[i + 1]; // 開くファイルを読み込む } read_file(file_name); // ファイル読み込み printf("\nFinished Reading File\n"); printf("Total number of documents read = %d\n", n_docs); printf("Total number of words in the index = %d\n", n_words); fflush(stdout); printf("\nCurrent Indicator Type: %s, Threshold: %d\n", mode_string[mode], threshold); while (1) { if (is_chaged) // 変更があった場合 { free_results(); // 前回の結果の消去 // 次回の計算用 results = (RESULTS **)malloc(sizeof(RESULTS *) * n_results_max); if (results == NULL) { fprintf(stderr, "Memory Allocation Error\n"); exit(1); } calc_indicator(mode, threshold); // 強度の計算 printf("\nCalculation Finished\n"); fflush(stdout); sort_results(); // 結果のソート printf("\nSort Finished\n"); fflush(stdout); } printf("\nCurrent Indicator Type: %s, Threshold: %d\n", mode_string[mode], threshold); printf("Choose Action (-1:Quit)\n"); printf("0:Show Results 1:Change Indicator \n"); printf("2:Change Precision 3:Write to Csv \n> "); act = read_input_as_int(); // 操作を選択 switch (act) { case A_QUIT: // 終了 free_all(); printf("Quit\n"); return 0; case A_SHOW_RESULTS: show_results_in_range(); // 結果の表示 is_chaged = 0; break; case A_CHANGE_INDICATOR: // 計算方法の変更 is_chaged = change_mode(&mode); break; case A_CHANGE_PRECISION: is_chaged = change_threshold(&threshold); // 計算に用いるときの閾値(頻度)の変更 break; case A_WRITE_CSV: write_to_csv_in_range(); // CSV形式で書き出し is_chaged = 0; break; default: break; } } return 0; }
/** * 要素の解放を行う関数 */ void free_all(void) { free_document(); // ドキュメントの解放 free_array(); // 配列の解放 free_results(); // 結果の解放 }
int main(int argc, char*argv[]) { int fh_wtmp; int fh_lastlog; struct lastlog lastlog_ent; struct utmp utmp_ent; long userid[MAX_ID]; long i, slot; int status = 0; long wtmp_bytes_read; struct stat wtmp_stat; struct s_localpwd *localpwd; uid_t *uid; char wtmpfile[128], lastlogfile[128]; memcpy(wtmpfile, WTMP_FILENAME, 127); memcpy(lastlogfile, LASTLOG_FILENAME, 127); while (--argc && ++argv) /* poor man getopt */ { if (!memcmp("-f", *argv, 2)) { if (!--argc) break; ++argv; memcpy(wtmpfile, *argv, 127); } else if (!memcmp("-l", *argv, 2)) { if (!--argc) break; ++argv; memcpy(lastlogfile, *argv, 127); } } signal(SIGALRM, read_status); alarm(5); for (i=0; i<MAX_ID; i++) userid[i]=FALSE; if ((fh_lastlog=open(lastlogfile,O_RDONLY)) < 0) { fprintf(stderr, "unable to open lastlog-file %s\n", lastlogfile); return(1); } if ((fh_wtmp=open(wtmpfile,O_RDONLY)) < 0) { fprintf(stderr, "unable to open wtmp-file %s\n", wtmpfile); close(fh_lastlog); return(2); } if (fstat(fh_wtmp,&wtmp_stat)) { perror("chklastlog::main: "); close(fh_lastlog); close(fh_wtmp); return(3); } wtmp_file_size = wtmp_stat.st_size; localpwd = read_pwd(); while ((wtmp_bytes_read = read (fh_wtmp, &utmp_ent, sizeof (struct utmp))) >0) { if (wtmp_bytes_read < sizeof(struct utmp)) { fprintf(stderr, "wtmp entry may be corrupted"); break; } total_wtmp_bytes_read+=wtmp_bytes_read; if ( !nonuser(utmp_ent) && strncmp(utmp_ent.ut_line, "ftp", 3) && (uid=localgetpwnam(localpwd,utmp_ent.ut_name)) != NULL ) { if (*uid > MAX_ID) { fprintf(stderr, "MAX_ID is %ld and current uid is %ld, please check\n\r", MAX_ID, *uid ); exit (1); } if (!userid[*uid]) { lseek(fh_lastlog, (long)*uid * sizeof (struct lastlog), 0); if ((wtmp_bytes_read = read(fh_lastlog, &lastlog_ent, sizeof (struct lastlog))) > 0) { if (wtmp_bytes_read < sizeof(struct lastlog)) { fprintf(stderr, "lastlog entry may be corrupted"); break; } if (lastlog_ent.ll_time == 0) { if (-1 != (slot = getslot(localpwd, *uid))) printf("user %s deleted or never logged from lastlog!\n", NULL != localpwd->uname[slot] ? (char*)localpwd->uname[slot] : "(null)"); else printf("deleted user uid(%d) not in passwd\n", *uid); ++status; } userid[*uid]=TRUE; } } } } #if 0 printf("\n"); #endif free_results(localpwd); close(fh_wtmp); close(fh_lastlog); return(status); }