maintenance.c
/* use the functions above to make the table in our database
items.db match exactly those items of clothing with an image in the 
directory /images. Called by main.c if flag -s is received */
use the functions above to make the table in our database
items.db match exactly those items of clothing with an image in the
directory /images. Called by main.c if flag -s is received
use the functions above to make the table in our database items . db match exactly those items of clothing with an image in the directory / images . Called by main . c if flag - s is received
int sync_folder()
{
    struct dirent *ent;
    DIR *dptr;
    dptr = opendir("images");
    if (dptr == NULL)
    {
        printf("Cannot open directory");
        return 1;
    }
    int f_count = 0;
    while ((ent = readdir(dptr)) != NULL)
        if (ent->d_type != DT_DIR)
            f_count++;
    char files[f_count][MAX_CHARS];
    rewinddir(dptr);
    int i = 0;
    while ((ent = readdir(dptr)) != NULL)
    {
        if (ent->d_type != DT_DIR)
        {
            strcpy(files[i], ent->d_name);
            i++;
        }
    }
    closedir(dptr);
    sort_names(files, f_count);
    if ( confirm_tables() ) return 1;
    sqlite3 *db;
    int rc = sqlite3_open_v2("items.db", &db, SQLITE_OPEN_READONLY, NULL);
    if (rc)
    {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
        return 1;
    }
    char *query_number = "SELECT COUNT(*) FROM clothing;";
    sqlite3_stmt *stmt;
    rc = sqlite3_prepare_v2(db, query_number, -1, &stmt, NULL);
    if (rc != SQLITE_OK)
    {
        printf("error: %s\n", sqlite3_errmsg(db));
        return 1;
    }
    sqlite3_reset(stmt); 
    int e_count = (rc = sqlite3_step(stmt) == SQLITE_ROW) ? sqlite3_column_int(stmt, 0) : 0;
    char entries[e_count][MAX_CHARS];
    char *query_names = "SELECT name FROM clothing ORDER BY name ASC;"; 
    rc = sqlite3_prepare_v2(db, query_names, -1, &stmt, NULL);
    if (rc != SQLITE_OK)
    {
        printf("error: %s\n", sqlite3_errmsg(db));
        return 1;
    }
    i = 0;
    while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
    {
        strcpy(entries[i], sqlite3_column_text(stmt, 0));
        i++;
    }
    if (rc != SQLITE_DONE)
    {
        printf("error: %s", sqlite3_errmsg(db));
    }
    sqlite3_finalize(stmt);
    sqlite3_close(db);
    if (compare(files, f_count, entries, e_count))
    {
        return 1;
    }
    return 0;
}
int sync_folder ( ) { struct dirent * ent ; DIR * dptr ; dptr = opendir ( " " ) ; if ( dptr == NULL ) { printf ( " " ) ; return 1 ; } int f_count = 0 ; while ( ( ent = readdir ( dptr ) ) != NULL ) if ( ent -> d_type != DT_DIR ) f_count ++ ; char files [ f_count ] [ MAX_CHARS ] ; rewinddir ( dptr ) ; int i = 0 ; while ( ( ent = readdir ( dptr ) ) != NULL ) { if ( ent -> d_type != DT_DIR ) { strcpy ( files [ i ] , ent -> d_name ) ; i ++ ; } } closedir ( dptr ) ; sort_names ( files , f_count ) ; if ( confirm_tables ( ) ) return 1 ; sqlite3 * db ; int rc = sqlite3_open_v2 ( " " , & db , SQLITE_OPEN_READONLY , NULL ) ; if ( rc ) { fprintf ( stderr , " \n " , sqlite3_errmsg ( db ) ) ; return 1 ; } char * query_number = " " ; sqlite3_stmt * stmt ; rc = sqlite3_prepare_v2 ( db , query_number , -1 , & stmt , NULL ) ; if ( rc != SQLITE_OK ) { printf ( " \n " , sqlite3_errmsg ( db ) ) ; return 1 ; } sqlite3_reset ( stmt ) ; int e_count = ( rc = sqlite3_step ( stmt ) == SQLITE_ROW ) ? sqlite3_column_int ( stmt , 0 ) : 0 ; char entries [ e_count ] [ MAX_CHARS ] ; char * query_names = " " ; rc = sqlite3_prepare_v2 ( db , query_names , -1 , & stmt , NULL ) ; if ( rc != SQLITE_OK ) { printf ( " \n " , sqlite3_errmsg ( db ) ) ; return 1 ; } i = 0 ; while ( ( rc = sqlite3_step ( stmt ) ) == SQLITE_ROW ) { strcpy ( entries [ i ] , sqlite3_column_text ( stmt , 0 ) ) ; i ++ ; } if ( rc != SQLITE_DONE ) { printf ( " " , sqlite3_errmsg ( db ) ) ; } sqlite3_finalize ( stmt ) ; sqlite3_close ( db ) ; if ( compare ( files , f_count , entries , e_count ) ) { return 1 ; } return 0 ; }
use the functions above to make the table in our database
items.db match exactly those items of clothing with an image in the
directory /images.
use the functions above to make the table in our database items . db match exactly those items of clothing with an image in the directory / images .
/* first create a sorted list of strings from the names of
    files in /images */ /* second, create an order list of strings from the names of entries 
    in the database */ //" COLLATE NOCASE ASC;"; /* copy rows into array entries */ // print_names(entries, e_count); /* finally, compare the two lists to correct discrepancies */