Database/db.c
void interpret_command(char *command, char *response, int len) {
	// printf("command: %s, response: %s\n", command, response);
    char value[MAXLEN];
    char ibuf[MAXLEN];
    char name[MAXLEN];
    int sscanf_ret;

    if (strlen(command) <= 1) {
        snprintf(response, len, "ill-formed command");
        return;
    }

    // which command is it?
    switch (command[0]) {
        case 'q':
            // Query
            sscanf_ret = sscanf(&command[1], "%255s", name);
            if (sscanf_ret < 1) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            db_query(name, response, len);
            if (strlen(response) == 0) {
                snprintf(response, len, "not found");
            }

            return;

        case 'a':
            // Add to the database
            sscanf_ret = sscanf(&command[1], "%255s %255s", name, value);
            if (sscanf_ret < 2) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            if (db_add(name, value)) {
                snprintf(response, len, "added");
            } else {
                snprintf(response, len, "already in database");
            }

            return;

        case 'd':
            // Delete from the database
            sscanf_ret = sscanf(&command[1], "%255s", name);
            if (sscanf_ret < 1) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            if (db_remove(name)) {
                snprintf(response, len, "removed");
            } else {
                snprintf(response, len, "not in database");
            }

            return;

        case 'f':
            // process the commands in a file (silently)
            sscanf_ret = sscanf(&command[1], "%255s", name);
            if (sscanf_ret < 1) {
                snprintf(response, len, "ill-formed command");
                return;
            }

            FILE *finput = fopen(name, "r");
            if (!finput) {
                snprintf(response, len, "bad file name");
                return;
            }
            while (fgets(ibuf, sizeof(ibuf), finput) != 0) {
                pthread_testcancel();  // fgets is not a cancellation point
                interpret_command(ibuf, response, len);
            }
            fclose(finput);
            snprintf(response, len, "file processed");
            return;

        default:
            snprintf(response, len, "ill-formed command");
            return;
    }
}
/* Interprets the given command string and calls the appropriate database
 * function. Writes up to len-1 bytes of the response message string produced
 * by the database to the response buffer. */
Interprets the given command string and calls the appropriate database
function. Writes up to len-1 bytes of the response message string produced
by the database to the response buffer.
Interprets the given command string and calls the appropriate database function . Writes up to len - 1 bytes of the response message string produced by the database to the response buffer .
void interpret_command(char *command, char *response, int len) {
    char value[MAXLEN];
    char ibuf[MAXLEN];
    char name[MAXLEN];
    int sscanf_ret;
    if (strlen(command) <= 1) {
        snprintf(response, len, "ill-formed command");
        return;
    }
    switch (command[0]) {
        case 'q':
            sscanf_ret = sscanf(&command[1], "%255s", name);
            if (sscanf_ret < 1) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            db_query(name, response, len);
            if (strlen(response) == 0) {
                snprintf(response, len, "not found");
            }
            return;
        case 'a':
            sscanf_ret = sscanf(&command[1], "%255s %255s", name, value);
            if (sscanf_ret < 2) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            if (db_add(name, value)) {
                snprintf(response, len, "added");
            } else {
                snprintf(response, len, "already in database");
            }
            return;
        case 'd':
            sscanf_ret = sscanf(&command[1], "%255s", name);
            if (sscanf_ret < 1) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            if (db_remove(name)) {
                snprintf(response, len, "removed");
            } else {
                snprintf(response, len, "not in database");
            }
            return;
        case 'f':
            sscanf_ret = sscanf(&command[1], "%255s", name);
            if (sscanf_ret < 1) {
                snprintf(response, len, "ill-formed command");
                return;
            }
            FILE *finput = fopen(name, "r");
            if (!finput) {
                snprintf(response, len, "bad file name");
                return;
            }
            while (fgets(ibuf, sizeof(ibuf), finput) != 0) {
                pthread_testcancel();  
                interpret_command(ibuf, response, len);
            }
            fclose(finput);
            snprintf(response, len, "file processed");
            return;
        default:
            snprintf(response, len, "ill-formed command");
            return;
    }
}
void interpret_command ( char * command , char * response , int len ) { char value [ MAXLEN ] ; char ibuf [ MAXLEN ] ; char name [ MAXLEN ] ; int sscanf_ret ; if ( strlen ( command ) <= 1 ) { snprintf ( response , len , " " ) ; return ; } switch ( command [ 0 ] ) { case ' ' : sscanf_ret = sscanf ( & command [ 1 ] , " " , name ) ; if ( sscanf_ret < 1 ) { snprintf ( response , len , " " ) ; return ; } db_query ( name , response , len ) ; if ( strlen ( response ) == 0 ) { snprintf ( response , len , " " ) ; } return ; case ' ' : sscanf_ret = sscanf ( & command [ 1 ] , " " , name , value ) ; if ( sscanf_ret < 2 ) { snprintf ( response , len , " " ) ; return ; } if ( db_add ( name , value ) ) { snprintf ( response , len , " " ) ; } else { snprintf ( response , len , " " ) ; } return ; case ' ' : sscanf_ret = sscanf ( & command [ 1 ] , " " , name ) ; if ( sscanf_ret < 1 ) { snprintf ( response , len , " " ) ; return ; } if ( db_remove ( name ) ) { snprintf ( response , len , " " ) ; } else { snprintf ( response , len , " " ) ; } return ; case ' ' : sscanf_ret = sscanf ( & command [ 1 ] , " " , name ) ; if ( sscanf_ret < 1 ) { snprintf ( response , len , " " ) ; return ; } FILE * finput = fopen ( name , " " ) ; if ( ! finput ) { snprintf ( response , len , " " ) ; return ; } while ( fgets ( ibuf , sizeof ( ibuf ) , finput ) != 0 ) { pthread_testcancel ( ) ; interpret_command ( ibuf , response , len ) ; } fclose ( finput ) ; snprintf ( response , len , " " ) ; return ; default : snprintf ( response , len , " " ) ; return ; } }
Interprets the given command string and calls the appropriate database
 function.
Interprets the given command string and calls the appropriate database function .
// printf("command: %s, response: %s\n", command, response); // which command is it? // Query // Add to the database // Delete from the database // process the commands in a file (silently) // fgets is not a cancellation point