int main(){ int choice; // populate the database /* read file create insert statements execute sql statements close file */ populate_sql(); // menu // /* Options ** 1. Update record ** 2. Delete all records ** 3. Quit */ while (choice = menu()){ if (choice == 4){ return 0; } if (choice == 3){ report(); } else if (choice == 2){ del_records(); } else if (choice == 1){ update_records(); } } return 0; }
int process_args(int argc, char *argv[], char batch_mode) { int c; char mode = 0; char time_ok = 0; char room = 0; char *filename; record *rec = calloc(1, sizeof(record)); record *first = NULL; record *last = NULL; EVP_CIPHER_CTX en_ctx, de_ctx; optind = 0; opterr = 0; while ((c = getopt(argc, argv, ":T:K:E:G:ALR:B:")) != -1) switch (c) { case 'T': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (strtoi(optarg, &(rec->timestamp)) || !rec->timestamp) return print_error(ARG_ERROR); time_ok = 1; break; case 'K': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (!is_token_valid(optarg)) return print_error(ARG_ERROR); rec->token = optarg; break; case 'E': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->ptype == GUEST) return print_error(ARG_ERROR); rec->ptype = EMPLOYEE; if (!is_name_valid(optarg)) return print_error(ARG_ERROR); rec->name = optarg; break; case 'G': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->ptype == EMPLOYEE) return print_error(ARG_ERROR); rec->ptype = GUEST; if (!is_name_valid(optarg)) return print_error(ARG_ERROR); rec->name = optarg; break; case 'A': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->etype == DEPARTURE) return print_error(ARG_ERROR); rec->etype = ARRIVAL; break; case 'L': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (rec->etype == ARRIVAL) return print_error(ARG_ERROR); rec->etype = DEPARTURE; break; case 'R': if (mode == BATCH_MODE) return print_error(ARG_ERROR); mode = DEFAULT_MODE; if (strtoi(optarg, &(rec->room_id))) return print_error(ARG_ERROR); room = 1; break; case 'B': if (mode == DEFAULT_MODE || batch_mode) return print_error(ARG_ERROR); mode = BATCH_MODE; filename = optarg; break; default: return print_error(ARG_ERROR); } if (mode == BATCH_MODE) { free(rec); if (batch(filename)) return print_error(ARG_ERROR); return NO_ERROR; } //Checa opcoes mandatorias if (mode != DEFAULT_MODE || !time_ok || rec->token == NULL || rec->name == NULL || rec->etype == UNDEF_E || optind >= argc) return print_error(ARG_ERROR); //Ajusta o evento de acordo com a presenca de sala if (!room) { if (rec->etype == ARRIVAL) rec->etype = G_ARRIVAL; else rec->etype = G_DEPARTURE; } //Inicia contextos de seguranca if ((aes_init((unsigned char *) rec->token, strlen(rec->token), &en_ctx, &de_ctx))) return print_error(ARG_ERROR); //validacoes de consistencia c = read_records(&first, argv[optind], &last, &de_ctx); if (c) { EVP_CIPHER_CTX_cleanup(&en_ctx); EVP_CIPHER_CTX_cleanup(&de_ctx); return c; } if ((c = check_record(first, rec))) { del_records(&first); free(rec); EVP_CIPHER_CTX_cleanup(&en_ctx); EVP_CIPHER_CTX_cleanup(&de_ctx); return print_error(ARG_ERROR); } if (!first) first = rec; else { if (!last) last = first; last->next = rec; } write_record(first, argv[optind], &en_ctx); //TODO em batch, esses registros poderiam ser mantidos para evitar reler o arquivo. if (first != rec) { last->next = NULL; del_records(&first); } free(rec); EVP_CIPHER_CTX_cleanup(&en_ctx); EVP_CIPHER_CTX_cleanup(&de_ctx); return NO_ERROR; }