Mercurial > trustbridge
diff cinst/main.c @ 163:8cfcd38a9bb3
Change coding style for cinst main / windowsstore to GNU
The kdelibs / qt coding style is not really suited for plain C
and looks irritiating when combined with standard C functions.
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Tue, 25 Mar 2014 10:29:12 +0000 |
parents | a4b1c77f3e6a |
children | 4a20bab3f806 |
line wrap: on
line diff
--- a/cinst/main.c Tue Mar 25 10:08:56 2014 +0000 +++ b/cinst/main.c Tue Mar 25 10:29:12 2014 +0000 @@ -59,71 +59,84 @@ * * @returns: 0 on success. An error code otherwise. */ -int readInput(char **certificate_list, char ***to_install, - char ***to_remove, char ***all_certs) +int +readInput (char **certificate_list, char ***to_install, + char ***to_remove, char ***all_certs) { - int lines_read = 0; - int readingList = 0; - size_t list_size = 0; - char buf[MAX_LINE_LENGTH + 2]; + int lines_read = 0; + int readingList = 0; + size_t list_size = 0; + char buf[MAX_LINE_LENGTH + 2]; - if (*certificate_list || *to_install || *to_remove) { - printf("Error invalid parameters\n"); - return -1; + if (*certificate_list || *to_install || *to_remove) + { + printf ("Error invalid parameters\n"); + return -1; } - while (fgets(buf, MAX_LINE_LENGTH + 1, stdin)) { - size_t len = strlen(buf); /* fgets ensures buf is terminated */ - if (len <= 3) { - printf("Line too short.\n"); - return ERR_INVALID_INPUT; + while (fgets (buf, MAX_LINE_LENGTH + 1, stdin) ) + { + size_t len = strlen (buf); /* fgets ensures buf is terminated */ + if (len <= 3) + { + printf ("Line too short.\n"); + return ERR_INVALID_INPUT; } - if (lines_read ++ > MAX_LINES) { - printf("Too many lines\n"); - return ERR_TOO_MUCH_INPUT; + if (lines_read++ > MAX_LINES) + { + printf ("Too many lines\n"); + return ERR_TOO_MUCH_INPUT; } - if (buf[len-2] != '\r') { - if (buf[len-1] != '\n') { - printf("Line too long.\n"); - return ERR_INVALID_INPUT; + if (buf[len - 2] != '\r') + { + if (buf[len - 1] != '\n') + { + printf ("Line too long.\n"); + return ERR_INVALID_INPUT; } - buf[len-1] = '\r'; - buf[len] = '\n'; - buf[len+1] = '\0'; - len++; + buf[len - 1] = '\r'; + buf[len] = '\n'; + buf[len + 1] = '\0'; + len++; } - if (strcmp("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0){ - readingList = 1; - continue; - } - if (strcmp("-----END CERTIFICATE LIST-----\r\n", buf) == 0){ - readingList = 0; - continue; + if (strcmp ("-----BEGIN CERTIFICATE LIST-----\r\n", buf) == 0) + { + readingList = 1; + continue; } - if (readingList) { - str_append_str(certificate_list, &list_size, buf, len); - } else if (strcmp("UNINSTALL\r\n", buf) == 0) { - /* Remove trailing \r\n */ - strv_append(to_remove, buf, len - 2); - continue; + if (strcmp ("-----END CERTIFICATE LIST-----\r\n", buf) == 0) + { + readingList = 0; + continue; } - if (*buf == 'I') { - /* Remove leading I: and trailing \r\n */ - strv_append(readingList ? all_certs : to_install, - buf+2, len - 4); - continue; + if (readingList) + { + str_append_str (certificate_list, &list_size, buf, len); } - if (*buf == 'R') { - /* Remove leading R: and trailing \r\n */ - strv_append(readingList ? all_certs : to_remove, - buf+2, len - 4); - continue; + else if (strcmp ("UNINSTALL\r\n", buf) == 0) + { + /* Remove trailing \r\n */ + strv_append (to_remove, buf, len - 2); + continue; + } + if (*buf == 'I') + { + /* Remove leading I: and trailing \r\n */ + strv_append (readingList ? all_certs : to_install, + buf + 2, len - 4); + continue; + } + if (*buf == 'R') + { + /* Remove leading R: and trailing \r\n */ + strv_append (readingList ? all_certs : to_remove, buf + 2, len - 4); + continue; } } - return 0; + return 0; } /** @brief Check that the insturctions match to the list @@ -136,110 +149,133 @@ * * @returns 0 on success, an error otherwise */ -int validate_instructions(char **all_certs, - char **to_validate) +int +validate_instructions (char **all_certs, char **to_validate) { - int i = 0, - j = 0; + int i = 0, j = 0; - if (!all_certs || strv_length(all_certs) < 1) { - /* Invalid parameters */ - return -1; + if (!all_certs || strv_length (all_certs) < 1) + { + /* Invalid parameters */ + return -1; } - if (to_validate == NULL) { - /* Nothing is valid */ - return 0; + if (to_validate == NULL) + { + /* Nothing is valid */ + return 0; } - for (i=0; to_validate[i]; i++) { - bool found = false; - for (j=0; all_certs[j]; j++) { - if (strncmp(to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) == 0) { - found = true; - break; + for (i = 0; to_validate[i]; i++) + { + bool found = false; + for (j = 0; all_certs[j]; j++) + { + if (strncmp (to_validate[i], all_certs[j], MAX_LINE_LENGTH - 2) == + 0) + { + found = true; + break; } } - if (!found) { - printf("Install instruction with invalid certificate\n."); - return ERR_INVALID_INSTRUCTIONS; + if (!found) + { + printf ("Install instruction with invalid certificate\n."); + return ERR_INVALID_INSTRUCTIONS; } } - return 0; + return 0; } -int main() { - char **to_install = NULL; - char **to_remove = NULL; - char **all_certs = NULL; - char *certificate_list = NULL; - size_t list_len = 0; - int ret = -1; - bool uninstall = false; +int +main () +{ + char **to_install = NULL; + char **to_remove = NULL; + char **all_certs = NULL; + char *certificate_list = NULL; + size_t list_len = 0; + int ret = -1; + bool uninstall = false; - ret = readInput(&certificate_list, &to_install, &to_remove, &all_certs); + ret = readInput (&certificate_list, &to_install, &to_remove, &all_certs); - if (ret) { - return ret; + if (ret) + { + return ret; } - if (!certificate_list) { - return ERR_INVALID_INPUT_NO_LIST; + if (!certificate_list) + { + return ERR_INVALID_INPUT_NO_LIST; } - list_len = strnlen(certificate_list, MAX_INPUT_SIZE); + list_len = strnlen (certificate_list, MAX_INPUT_SIZE); - ret = verify_list(certificate_list, list_len); + ret = verify_list (certificate_list, list_len); - if (ret) { - return ERR_INVALID_SIGNATURE; + if (ret) + { + return ERR_INVALID_SIGNATURE; } - if (!strv_length(to_install) && !strv_length(to_remove)) { - return ERR_NO_INSTRUCTIONS; + if (!strv_length (to_install) && !strv_length (to_remove) ) + { + return ERR_NO_INSTRUCTIONS; } - /* Check that the instructions are ok to execute */ - if (to_install) { - ret = validate_instructions(all_certs, to_install); - if (ret) { - return ret; + /* Check that the instructions are ok to execute */ + if (to_install) + { + ret = validate_instructions (all_certs, to_install); + if (ret) + { + return ret; } } - if (to_remove) { - if (to_remove[0] && strncmp("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0) { - uninstall = true; - strv_free(to_remove); - to_remove = NULL; - } else { - ret = validate_instructions(all_certs, to_remove); - if (ret) { - return ret; + if (to_remove) + { + if (to_remove[0] + && strncmp ("UNINSTALL", to_remove[0], MAX_LINE_LENGTH) == 0) + { + uninstall = true; + strv_free (to_remove); + to_remove = NULL; + } + else + { + ret = validate_instructions (all_certs, to_remove); + if (ret) + { + return ret; } } } - if (uninstall) { - /* To uninstall does not have to be verified as it part of the - * signed list.*/ - to_remove = all_certs; - } else { - strv_free(all_certs); - all_certs = NULL; + if (uninstall) + { + /* To uninstall does not have to be verified as it part of the + * signed list.*/ + to_remove = all_certs; + } + else + { + strv_free (all_certs); + all_certs = NULL; } #ifdef WIN32 - return write_stores_win (to_install, to_remove, true); + return write_stores_win (to_install, to_remove, true); #endif - /* Make valgrind happy */ - strv_free(to_install); - strv_free(to_remove); - free(certificate_list); + /* Make valgrind happy */ + strv_free (to_install); + strv_free (to_remove); + free (certificate_list); - return 0; + return 0; }