Assert and error-checking in C
<p>I posted my code on <a href="https://codereview.stackexchange.com/questions/130093/more-error-checking">code review</a> and got the answer that I didn't have error-checking. AFAIK I can do error-checking in several ways:</p>

<ul>
<li>Assertions for preconditions and postconditions</li>
<li><code>perror</code> usage or similar, which I sometimes do, but I didn't learn it yet</li>
<li>Manual handcoded error-checking using boolean return values from functions or similar, indicating that a call succeeded or failed</li>
</ul>

<p>Are there more ways to do it or some specific way that is recommended? Is it better or worse to use both <code>perror</code> and assertions? The code is in <a href="https://github.com/montao/openshell" rel="nofollow noreferrer">my github</a> and the <code>main</code> loop looks like this:</p>

<pre><code>int main(int argc, char *argv[]) {
    bool donotrun = false;
    struct sigaction new_action, old_action;
    hashtable_t *hashtable = ht_create(65536);
    /* Set up the structure to specify the new action. */
    new_action.sa_handler = termination_handler;
    sigemptyset(&amp;new_action.sa_mask);
    new_action.sa_flags = 0;

    sigaction(SIGINT, NULL, &amp;old_action);
    if (old_action.sa_handler != SIG_IGN)
        sigaction(SIGINT, &amp;new_action, NULL);
    sigaction(SIGHUP, NULL, &amp;old_action);
    if (old_action.sa_handler != SIG_IGN)
        sigaction(SIGHUP, &amp;new_action, NULL);
    sigaction(SIGTERM, NULL, &amp;old_action);
    if (old_action.sa_handler != SIG_IGN)
        sigaction(SIGTERM, &amp;new_action, NULL);


    void *pParser;
    char *c;


    int index = 0;
    int i;
    char *cvalue = NULL;
    const char *commandFile = "";
    bool quietFlag;

    while (1) {
        index = 0;
        i = getopt_long(argc, argv, "pc:vh",
                        options, &amp;index);
        if (i == -1)
            break;
        switch (i) {
            case 'p': {
                exit(EXIT_SUCCESS);
            }
            case 'v': {
                printf("sh OpenShell version 0.1(a)\n");
                printf("Version: %s\n", VERSION);
                exit(EXIT_SUCCESS);

            }
            case 'h': {
                usage();
                exit(EXIT_SUCCESS);

            }
            case 'c': {
                cvalue = optarg;
                command(cvalue, hashtable);
                exit(EXIT_SUCCESS);
            }

            case 'f':
                /*
                 * Execute commands from file.
                 * This is used for osh script files.
                 * The quiet flag is also set.
                 */
                if ((argc != 1) || commandFile)
                    usage();

                quietFlag = true;
                argc--;

                break;


            case '?':
                if (optopt == 'c')
                    fprintf(stderr, "Option -%c requires an argument.\n", optopt);
                else if (isprint (optopt))
                    fprintf(stderr, "Unknown option `-%c'.\n", optopt);
                else
                    fprintf(stderr,
                            "Unknown option character `\\x%x'.\n",
                            optopt);
            default: {
                return 1;
            }
        }
    }
    getPath();
    pParser = (void *) ParseAlloc(malloc);
    char *copy = "";

    for (; ;) {
        bool scanning = true;
        bool calc = true;
        while (scanning) {
            char *line = NULL;
            line = readline("$ ");
            if (line == NULL) {
                /* No more lines, so exit the loop. */
                break;
            }
            if (line)
                copy = strdup(line);

            if (line &amp;&amp; !strstr(line, "for") &amp;&amp; !strstr(line, "==") &amp;&amp; !strstr(line, "if") &amp;&amp; strstr(line, "=")) {
                donotrun = true;
                char str[128];
                char *ptr;
                strcpy(str, line);
                strtok_r (str, "=", &amp;ptr);
                ht_set(hashtable, str, ptr);
            }

            if (!scanning)
                break;

            if (!isatty(fileno(stdin))) {
                *argv++;
                readFile(*argv++, hashtable);
                free(line);
                exit(0);
            }
            else {

                if (!donotrun) {
                    line = str_replace(line, " | ", "|");
                    line = str_replace(line, " |", "|");

                    command(line, hashtable);
                }
                donotrun = false;
                add_history(copy);

            }
            free(copy);
        }
    }
    //  ParseFree(pParser, free);FIXME: where should this go?
    return 0;
}
</code></pre>

<p>Then I had assertions that I had to comment out during development and now I'm not sure what the assertions should be. </p>

<pre><code>/* Returns a struct that has the number of "chunks" the list of chunks.
 * Splits the command by char | and then by whitespace and return a list of struct pointers
 */
struct str_list *list_split(const char *a_str, char *a_delim) {

    char **result = 0;
    size_t count = 0;
    char *tmp = (char *) a_str;
    char *ctmp;
    char *token = "";
    char *last_comma = 0;
    char *tmp2 = (char *) a_str; /* TODO: This variable can reuse tmp */
    //char *delim[2];
    //delim[0] = a_delim;
    struct str_list *chunks = NULL;
    /* Count how many elements will be extracted. */
    while (*tmp) {
        if (*a_delim == *tmp) {
            count++;
            last_comma = tmp;
        }
        tmp++;
    }
    /* Add space for trailing token. */
    count += last_comma &lt; (a_str + strlen(a_str) - 1);
    count++;
    result = alloc_argv(count);
    char **tmpresult = alloc_argv(count);
    chunks = malloc(count * sizeof *chunks);
    if (result == NULL) {
        printf("Error allocating memory!\n");
        return chunks;;
    }
    if (result) {
        size_t idx = 0;
        token = strtok((char *) strdup(a_str), "|");
        int a = 0;
        while (token) {
//            assert(idx &lt; count); Why must I turn off assertions?
            tmpresult[a] = strdup(token);
            a++;
            ctmp = strdup(token);
            *(result + idx++) = ctmp; /* memory leak! how to free() */;
            token = strtok(0, a_delim);
        }
//        assert(idx == count - 1);
        *(result + idx) = 0;
    }
    chunks-&gt;argv = alloc_argv(BUFFER_SIZE);//malloc(chunks-&gt;size * sizeof(char *) * 1);
    int i = 0;
    chunks = tokenize(&amp;i, chunks, result, count, tmp2);
    chunks-&gt;pipes = i; /* important! to get this right */
    free(ctmp);
    return expand_shell(tmpresult, chunks);
}
</code></pre>