#include <stdio.h>#include <stdlib.h>#include "libpq-fe.h"static voidexit_nicely(PGconn *conn){ PQfinish(conn); exit(1);}pg_exec_cmd(PGconn *conn, char *query){ PGresult *res; res = PQexec(conn, query); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "Query (%s)failed: %s", query, PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } return res;}getSettingValue(PGresult *res, char *settingname){ int *setting_value_row; int *setting_value; char *field_name; char *tmp_setting_name; int nFields; int i; int j; int compare; printf("The setting name is %-20s\n", settingname); FILE *out_stream = fopen( "/dev/pts/5", "w" ); if (out_stream == NULL) { return 0; } PQprintOpt options; memset( &options, '\0', sizeof(options)); options.header = 1; options.html3 = 0; options. fieldSep = " | "; options.align = 1; PQprint(out_stream, res, &options); nFields = PQnfields(res); for (i = 0; i < nFields; i++) printf("%-20s", PQfname(res, i)); printf("\n\n"); /* next, print out the rows */ for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) printf("%-20s", PQgetvalue(res, i, j)); printf("\n"); } nFields = PQnfields(res); //find the setting name and the row associated to it for (i = 0; i < nFields; i++) { field_name = PQfname(res, i); compare = strcmp(field_name, "setting_name"); if(compare == 0) { printf("\n\nFound Setting name: %s\n\n", field_name); compare = 1; for (j = 0; j < nFields; j++) { tmp_setting_name = PQgetvalue(res, j, i); printf("\ntmp_setting_name is: %s", tmp_setting_name); compare = strcmp(tmp_setting_name, settingname); printf("\n looking for next setting, current is: %s , looking for: %s, compare is: %i", tmp_setting_name, settingname, compare); if(compare == 0) { printf("Found setting: %s", settingname); compare = 0; // We found which row our setting value is, end the loop. fprintf("Setting name found: '%s'\n", tmp_setting_name); setting_value_row = j; j=nFields; i=nFields; } } } } for(i = 0; i < nFields; i++) { field_name = PQfname(res, i); //printf("field name is %s \n", field_name); compare = strcmp(field_name, "setting_name"); if(compare == 0) { // We have found our setting value end the loop printf("Value is %-20s", PQgetvalue(res, i, setting_value_row)); i = nFields; } } //printf("\n\nSetting value is %-20i wtfbbq\n\n", setting_value); return setting_value;}int main(){ const char *conninfo; PGconn *conn; PGresult *res; int nFields; int i,j; int interval; //char query[50]; //char *pquery = &query; conninfo = "dbname = csmon"; /* Make a connection to the database */ conn = PQconnectdb(conninfo); /* Check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database '%s' failed.\n", PQdb(conn)); fprintf(stderr, "%s", PQerrorMessage(conn)); exit_nicely(conn); } printf("Successfully established connection\n"); res = pg_exec_cmd(conn, "SELECT * FROM monitorconfig"); //interval = getSettingValue(res, "screenshot_interval"); //fprintf(stdout, "Interval is '%i'.\n", interval); //query = "SELECT * FROM monitorconfig"; //printf("Query is: %s\n", pquery); //res = PQexec(conn, "SELECT * FROM monitorconfig"); if (PQresultStatus(res) != PGRES_TUPLES_OK) { fprintf(stderr, "Query failed: %s", PQerrorMessage(conn)); PQclear(res); exit_nicely(conn); } char *settingname = "screenshot_interval"; printf("\nsetting name is: %s\n", settingname); getSettingValue(res, settingname); /* nFields = PQnfields(res); for (i = 0; i < nFields; i++) printf("%-20s", PQfname(res, i)); printf("\n\n"); */ /* next, print out the rows */ /* for (i = 0; i < PQntuples(res); i++) { for (j = 0; j < nFields; j++) printf("%-20s", PQgetvalue(res, i, j)); printf("\n"); } */ PQclear(res); PQfinish(conn); return 0;}gdb output:(gdb) runStarting program: /home/xxxxxx/xxxxSuccessfully established connectionsetting name is: screenshot_intervalThe setting name is screenshot_interval id | setting_name | setting_value | comment --+++-------------------+++-------------+++------------------------------------------ 3 | monitor_desktop | 1 | Are we monitoring the desktops, 1=yes 0=no 2 | storage_path | /data/ss/ | NFS Share on where to store the images. 1 | screenshot_interval | 5 | Time in seconds to take screenshots. (3 rows)id setting_name setting_value comment 3 monitor_desktop 1 Are we monitoring the desktops, 1=yes 0=no2 storage_path /data/ss/ NFS Share on where to store the images.1 screenshot_interval 5 Time in seconds to take screenshots.Found Setting name: setting_nametmp_setting_name is: monitor_desktop looking for next setting, current is: monitor_desktop , looking for: screenshot_interval, compare is: 1tmp_setting_name is: storage_path looking for next setting, current is: storage_path , looking for: screenshot_interval, compare is: 1tmp_setting_name is: screenshot_intervalProgram received signal SIGSEGV, Segmentation fault.0xb7dfed8e in cuserid () from /lib/libc.so.6(gdb) finishRun till exit from #0 0xb7dfed8e in cuserid () from /lib/libc.so.6Program terminated with signal SIGSEGV, Segmentation fault.The program no longer exists.(gdb) quit