int main(int argc, char **argv) { int i, c; char *optvalue = NULL; char *p, *line, *cfg_file; db_list_t *list; struct passwd *pwdent; pwdent = getpwuid(getuid()); cfg_file = (char *) malloc(strlen(pwdent->pw_dir) + 5); snprintf(cfg_file, strlen(pwdent->pw_dir) + 9, "%s/.locker", pwdent->pw_dir); if (cfg_init(cfg_file, &cfg) < 0) { fprintf(stderr, "could not open config file\n"); return EXIT_FAILURE; } if (!db_init(cfg.db_file)) { fprintf(stderr, "cound not init db\n"); return EXIT_FAILURE; } while ((c = getopt(argc, argv, "a:s:p:c:")) != -1) { switch (c) { case 'a': optvalue = optarg; db_list(optvalue, db_list_callback); break; case 's': optvalue = optarg; printf("password:"******""); db_set(optvalue, encrypt_text(line, cfg.gpg_key)); break; case 'p': optvalue = optarg; db_get(optvalue, db_get_callback); break; default: fprintf(stderr, "Usage:\n\t-s <name>\tSet password\n\t-p <name>\tPrint decrypted password\n\t-a <prefix>\tList all names\n"); } } db_close(); return EXIT_SUCCESS; }
/* * Search the database for an activity that matches the ID informed and return a pointer to the found one. * If no activity has been found, a null pointer will be returned. */ Atividade *ativPegar( int codAtividade ) { Atividade **ativs, *ativ; char sqlTemplate[] = " SELECT a.codatividade, a.nome, a.descricao, strftime('%%s', a.data), a.pontos, a.codtipoatividade, a.coddisc, h.codhorario " " FROM atividade a " " LEFT JOIN horario h " " ON h.coddisc = a.coddisc " " AND strftime('%%w', a.data) = CAST((round((h.codhorario-1.5)/2)+1) as INTEGER) " " WHERE codatividade = %d " " LIMIT 1; "; char query[strlen(sqlTemplate)+40]; sprintf(query, sqlTemplate, codAtividade); ativs = db_list(sizeof(Atividade), ativExtrair, query); ativ = *ativs; free(ativs); return ativ; }
int main(int argc, const char const *argv[]) { apr_pool_t *p = NULL; apr_pool_initialize(); apr_pool_create(&p, NULL); apr_getopt_t *opt; apr_status_t rv; char ch = '\0'; const char *optarg = NULL; const char *config_opts = NULL; const char *install_opts = NULL; const char *make_opts = NULL; const char *url = NULL; enum CommandType request = COMMAND_NONE; rv = apr_getopt_init(&opt, p, argc, argv); while(apr_getopt(opt, "I:Lc:m:i:d:SF:B:", &ch, &optarg) == APR_SUCCESS) { switch (ch) { case 'I': request = COMMAND_INSTALL; url = optarg; break; case 'L': request = COMMAND_LIST; break; case 'c': config_opts = optarg; break; case 'm': make_opts = optarg; break; case 'i': install_opts = optarg; break; case 'S': request = COMMAND_INIT; break; case 'F': request = COMMAND_FETCH; url = optarg; break; case 'B': request = COMMAND_BUILD; url = optarg; break; } } switch(request) { case COMMAND_INSTALL: check(url, "You must at least give a URL."); Command_install(p, url, config_opts, make_opts, install_opts); break; case COMMAND_LIST: db_list(); break; case COMMAND_FETCH: check(url != NULL, "You must give a URL."); Command_fetch(p, url, 1); log_inf("Downloaded to %s and in /tmp/", BUILD_DIR); break; case COMMAND_BUILD: check(url, "You must at least give a URL."); Command_build(p, url, config_opts, make_opts, install_opts); break; case COMMAND_INIT: rv = db_init(); check(rv == 0, "Failed to make the database."); break; default: sentinel("Invalid command given."); } return 0; error: return 1; }
int main(int argc, char** argv) { char* args; /* no option, print usage */ if (argc < 2) { usage(); return 0; } config_load(); /* connect to the db or error */ if (db_connect()) { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); return 0; } /* compile the arguments */ args = get_args(argc,argv); /* if listing notes */ if (strcmp(argv[1],"list") == 0) { db_list(args); }else{ /* if null argument print usage */ if (args == NULL || argc <3) { usage(); /* if creating a new note */ }else if (strcmp(argv[1],"new") == 0 || strcmp(argv[1],"create") == 0) { db_new(args); /* if opening/editing an existing note */ }else if (strcmp(argv[1],"open") == 0 || strcmp(argv[1],"edit") == 0) { db_edit(args); /* append to a note if data is on stdin */ }else if (strcmp(argv[1],"append") == 0) { if (isatty(STDIN_FILENO)) { db_edit(args); }else{ db_append(args); } /* encrypt a new or existing note */ }else if (strcmp(argv[1],"encrypt") == 0) { db_encrypt(args); /* decrypt an existing note */ }else if (strcmp(argv[1],"decrypt") == 0) { db_decrypt(args); /* display an existing note */ }else if (strcmp(argv[1],"show") == 0) { db_show(args); /* if deleting note/s */ }else if (strcmp(argv[1],"del") == 0) { db_del(args); /* unknown option, print usage */ }else{ usage(); } } /* free args if we can */ if (args != NULL) free(args); /* close the database */ sqlite3_close(db); /* save config */ config_save(); return 0; }