daemon.c
static int method_variants(sd_bus_message *m, void *userdata,
                           sd_bus_error *ret_error) {
  (void)ret_error;
  (void)userdata;
  int r;
  char *file;

  r = sd_bus_message_read(m, "s", &file);
  if (r < 0) {
    fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r));
    return r;
  }

  char *format = strrchr(
      file, '.'); /* Detachment of necessary extension from full file name */
  sqlite3 *db;
  sqlite3_stmt *res;

  int rc = sqlite3_open("extensions.db", &db);

  if (rc != SQLITE_OK) {
    fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);

    return 1;
  }

  char *sql = "SELECT program_path FROM Programs WHERE program_extension=?";

  rc =
      sqlite3_prepare_v2(db, sql, -1, &res, 0); /* Precompilation sql commamd */

  if (rc != SQLITE_OK) {
    printf("error: %s!\n", sqlite3_errmsg(db));
    return -1;
  }

  sqlite3_bind_text(res, 1, format, -1,
                    0); /* Adding @format@ paramater to search request */
  char **final = malloc(0);
  int size = 0;
  for (;;) {
    rc = sqlite3_step(res); /* Iterating over results */
    if (rc == SQLITE_DONE) break;
    if (rc != SQLITE_ROW) {
      printf("error: %s!\n", sqlite3_errmsg(db));
      break;
    }
    /* Adding new search result into array, works like push_back in vector*/
    final = realloc(final, (size + 1) * sizeof *final);
    final[size] = strdup((const char *)sqlite3_column_text(res, 0));
    size++;
  }

  final = realloc(final, (size + 1) * sizeof *final);
  final[size] = 0;

  sqlite3_finalize(res);
  sqlite3_close(db);
  /* Creating new message to return where our array will be handeled */
  sd_bus_message *n = 0;
  sd_bus_message_new_method_return(m, &n);
  sd_bus_message_append_strv(n, final);

  /* Clear memory */
  char **ptr = final;
  while (*ptr) {
    free(*ptr++);
  }
  free(final);
  return sd_bus_send(NULL, n, NULL);
}
/* Returns array of string if success, empty array if no extension in database
 */
Returns array of string if success, empty array if no extension in database
Returns array of string if success empty array if no extension in database
static int method_variants(sd_bus_message *m, void *userdata,
                           sd_bus_error *ret_error) {
  (void)ret_error;
  (void)userdata;
  int r;
  char *file;
  r = sd_bus_message_read(m, "s", &file);
  if (r < 0) {
    fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r));
    return r;
  }
  char *format = strrchr(
      file, '.'); 
  sqlite3 *db;
  sqlite3_stmt *res;
  int rc = sqlite3_open("extensions.db", &db);
  if (rc != SQLITE_OK) {
    fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    return 1;
  }
  char *sql = "SELECT program_path FROM Programs WHERE program_extension=?";
  rc =
      sqlite3_prepare_v2(db, sql, -1, &res, 0); 
  if (rc != SQLITE_OK) {
    printf("error: %s!\n", sqlite3_errmsg(db));
    return -1;
  }
  sqlite3_bind_text(res, 1, format, -1,
                    0); 
  char **final = malloc(0);
  int size = 0;
  for (;;) {
    rc = sqlite3_step(res); 
    if (rc == SQLITE_DONE) break;
    if (rc != SQLITE_ROW) {
      printf("error: %s!\n", sqlite3_errmsg(db));
      break;
    }
    final = realloc(final, (size + 1) * sizeof *final);
    final[size] = strdup((const char *)sqlite3_column_text(res, 0));
    size++;
  }
  final = realloc(final, (size + 1) * sizeof *final);
  final[size] = 0;
  sqlite3_finalize(res);
  sqlite3_close(db);
  sd_bus_message *n = 0;
  sd_bus_message_new_method_return(m, &n);
  sd_bus_message_append_strv(n, final);
  char **ptr = final;
  while (*ptr) {
    free(*ptr++);
  }
  free(final);
  return sd_bus_send(NULL, n, NULL);
}
static int method_variants ( sd_bus_message * m , void * userdata , sd_bus_error * ret_error ) { ( void ) ret_error ; ( void ) userdata ; int r ; char * file ; r = sd_bus_message_read ( m , " " , & file ) ; if ( r < 0 ) { fprintf ( stderr , " \n " , strerror ( - r ) ) ; return r ; } char * format = strrchr ( file , ' ' ) ; sqlite3 * db ; sqlite3_stmt * res ; int rc = sqlite3_open ( " " , & db ) ; if ( rc != SQLITE_OK ) { fprintf ( stderr , " \n " , sqlite3_errmsg ( db ) ) ; sqlite3_close ( db ) ; return 1 ; } char * sql = " " ; rc = sqlite3_prepare_v2 ( db , sql , -1 , & res , 0 ) ; if ( rc != SQLITE_OK ) { printf ( " \n " , sqlite3_errmsg ( db ) ) ; return -1 ; } sqlite3_bind_text ( res , 1 , format , -1 , 0 ) ; char * * final = malloc ( 0 ) ; int size = 0 ; for ( ; ; ) { rc = sqlite3_step ( res ) ; if ( rc == SQLITE_DONE ) break ; if ( rc != SQLITE_ROW ) { printf ( " \n " , sqlite3_errmsg ( db ) ) ; break ; } final = realloc ( final , ( size + 1 ) * sizeof * final ) ; final [ size ] = strdup ( ( const char * ) sqlite3_column_text ( res , 0 ) ) ; size ++ ; } final = realloc ( final , ( size + 1 ) * sizeof * final ) ; final [ size ] = 0 ; sqlite3_finalize ( res ) ; sqlite3_close ( db ) ; sd_bus_message * n = 0 ; sd_bus_message_new_method_return ( m , & n ) ; sd_bus_message_append_strv ( n , final ) ; char * * ptr = final ; while ( * ptr ) { free ( * ptr ++ ) ; } free ( final ) ; return sd_bus_send ( NULL , n , NULL ) ; }
Returns array of string if success, empty array if no extension in database
Returns array of string if success empty array if no extension in database
/* Detachment of necessary extension from full file name */ /* Precompilation sql commamd */ /* Adding @format@ paramater to search request */ /* Iterating over results */ /* Adding new search result into array, works like push_back in vector*/ /* Creating new message to return where our array will be handeled */ /* Clear memory */