· 8 years ago · Jan 21, 2018, 03:04 PM
1#include <assert.h>
2//#include <cstring>
3#include <time.h>
4#include <dirent.h>
5#include <gtk/gtk.h>
6#include <pcre.h>
7//#include <search.h>
8#include <sqlite3.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <sys/param.h>
13#include <sys/stat.h>
14#include <sys/types.h>
15#include <unistd.h>
16#include "uthash.h"
17
18GtkWidget *g_lbl_hello;
19GtkWidget *g_lbl_count;
20
21
22//function to combine char *
23void concatenate_string(char *original, char *add)
24{
25 while(*original)
26 original++;
27
28 while(*add)
29 {
30 *original = *add;
31 add++;
32 original++;
33 }
34 *original = '\0';
35}
36//
37
38int strcat2(char *s,char *t){
39 for(;*s!='\0';s++){
40 }
41 while((*s++ = *t++)!='\0'){
42 }
43 t='\0';
44 return 0;
45}
46
47// FUNCTION sqlite3 callback
48int callback(void *NotUsed, int argc, char **argv, char **azColName) {
49 NotUsed = 0;
50 for (int i = 0; i < argc; i++) {
51 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
52 }
53 printf("\n");
54 return 0;
55}
56//
57
58// FUNCTION is_file
59int is_file(const char *path)
60{
61 struct stat path_stat;
62 stat(path, &path_stat);
63 return S_ISREG(path_stat.st_mode); //is regular file
64}
65//
66
67
68// FUNCTION is_dir
69int is_dir(const char *path)
70{
71 struct stat path_stat;
72 stat(path, &path_stat);
73 return S_ISDIR(path_stat.st_mode); //is directory
74}
75//
76
77struct nameidstruct {
78 const char *name; /* key (string is WITHIN the structure) */
79 int id;
80 UT_hash_handle hh; /* makes this structure hashable */
81};
82struct nameidstruct *s, *tmp,*tmp2, *folderpathtmp = NULL;const char *name2;
83//struct nameidstruct *s2;
84int idir =1;
85unsigned int num_dirs;//char *tmphashname;
86// FUNCTION listdir
87
88void listdir(const char *name, int indent)
89{
90
91 DIR *dir;//, *tstdp
92 struct dirent *entry;
93 //struct stat stDirInfo;
94 struct stat stFileInfo;
95 char path[MAXPATHLEN];
96 //char szFullName[MAXPATHLEN];
97 if (!(dir = opendir(name)))
98 return;
99
100 while ((entry = readdir(dir)) != NULL) {
101 if (entry->d_type == DT_DIR) {
102 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
103 continue;
104 snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
105 //printf("%*s[%s] %s\n", indent, "", entry->d_name,path);
106 if (lstat(name, &stFileInfo) < 0)
107 perror ( name );
108
109 /* is the file a directory? */
110 if (S_ISDIR(stFileInfo.st_mode)){
111 //printf( "Directory: %s\n", path );
112
113
114 //HASH_FIND_STR( folderpathtmp, name, s);
115 //HASH_FIND_STR(head, key_ptr, item_ptr)
116
117 num_dirs = HASH_COUNT(folderpathtmp);
118 //printf("there are %u dirs and %u i's\n", num_dirs,idir);
119
120
121 //HASH_FIND_STR(folderpathtmp,path,s);
122 HASH_FIND(hh, folderpathtmp, path, strlen(path), s);
123 //printf("%*s PATHER:%s::\n", indent, "", path);
124 if (s){ printf("::::::::::::::::::betty's id is %d\n", s->id); }
125 else{
126 s = (struct nameidstruct*)malloc(sizeof(struct nameidstruct));
127
128 //strncpy(s->name, *n,50);
129 s->name=path;
130 s->id = idir++;
131 //HASH_ADD_KEYPTR( hh, folderpathtmp, s->name, strlen(s->name), s );
132 HASH_ADD_STR( folderpathtmp, name, s );
133 if(strcmp("/usr/portage/sys-boot/plymouth-openrc-plugin",path) == 0){
134 printf("hey\n");
135 }
136
137 //HASH_ADD_STR(head, keyfield_name, item_ptr)
138 }
139 //printf("%*s-:%s:-\n", indent, "", path);
140 //HASH_FIND_STR(folderpathtmp,"/usr/portage/sys-boot/mb", s);
141 name2="/usr/portage/sys-boot/mb";
142 HASH_FIND(hh, folderpathtmp, name2, strlen(name2), s);
143 if (s){ printf("_____x__x_z____betty's id is %d\n", s->id); }
144 HASH_FIND_STR(folderpathtmp,"/usr/portage/sys-boot/plymouth-openrc-plugin", s);
145 if (s){ printf("____f__d____ss__plymouth's id is %d\n", s->id); }
146 name2="/usr/portage/sys-boot/plymouth-openrc-plugin";
147 HASH_FIND(hh, folderpathtmp, name2, strlen(name2), s);
148 if (s) printf("found user %s: %d\n", name2, s->id);
149
150 listdir(path,indent+2);
151 }
152 //HASH_FIND(hh, folderpathtmp, path, strlen(path), s);
153 //if (s){ printf("_____bbbb____betty's id is %d\n", s->id); }
154 } else {
155 //printf("%*s- %s\n", indent, "", entry->d_name);
156 }
157 }
158 closedir(dir);
159}
160
161// MAIN main
162 int main(int argc, char *argv[]){
163
164
165//FUNCTION get path for portage ebuild files
166char *get_repo_path;
167
168int len;
169FILE *in;
170extern FILE *popen();
171char buff[512];
172
173if(!(in = popen("portageq get_repo_path / gentoo", "r"))){
174 exit(1);
175}
176while(fgets(buff, sizeof(buff), in)!=NULL){
177 get_repo_path=buff;
178}
179pclose(in);
180len = strlen(get_repo_path);
181if( get_repo_path[len-1] == '\n' )
182get_repo_path[len-1] = 0;
183//
184
185//populate db with info
186// const char *names[] = { "joe", "bob", "betty", NULL };// **n,
187
188//char get_full_path[255];
189//int i=0;
190/*
191for (n = names; *n != NULL; n++) {
192 s = (struct nameidstruct*)malloc(sizeof(struct nameidstruct));
193 strncpy(s->name, *n,10);
194 s->id = i++;
195 HASH_ADD_STR( folderpathtmp, name, s );
196}
197*/
198//
199//int idir=0;
200clock_t tic = clock();
201
202
203clock_t toc = clock();
204
205printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
206g_print("%s\n",get_repo_path);
207
208listdir(get_repo_path,0);
209
210//g_print(checkiffile);
211
212
213
214//open/create sqlite3 db
215sqlite3 *db;
216//sqlite3_stmt *res;
217char *err_msg = 0;
218int rc = sqlite3_open("test.db", &db);
219
220if (rc != SQLITE_OK) {
221 fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
222 sqlite3_close(db);
223 return 1;
224}
225
226/*
227(SELECT CASE WHEN exists(SELECT 1 FROM text WHERE id=150574) THEN 'good' ELSE 'Hello' END)
228
229INSERT OR IGNORE INTO visits VALUES ($ip, 0);
230UPDATE visits SET hits = hits + 1 WHERE ip LIKE $ip;
231void concatenate_string(char *original, char *add)
232{
233 while(*original)
234 original++;
235
236 while(*add)
237 {
238 *original = *add;
239 add++;
240 original++;
241 }
242 *original = '\0';
243}
244This requires the "ip" column to have a UNIQUE (or PRIMARY KEY) constraint.
245
246
247REPLACE INTO `visits` (ip, value) VALUES ($ip, 0);
248
249"DROP TABLE IF EXISTS Cars;"
250*/
251// insert data into db
252char *sql =
253 "CREATE TABLE IF NOT EXISTS Cars(Id INT PRIMARY KEY, Name TEXT, Price INT);"
254 "INSERT OR IGNORE INTO Cars VALUES(1, 'Audi', 52642);"
255 "INSERT OR IGNORE INTO Cars VALUES(2, 'Mercedes', 57127);"
256 "INSERT OR IGNORE INTO Cars VALUES(3, 'Skoda', 9000);"
257 "INSERT OR IGNORE INTO Cars VALUES(4, 'Volvo', 29000);"
258 "INSERT OR IGNORE INTO Cars VALUES(5, 'Bentley', 350000);"
259 "INSERT OR IGNORE INTO Cars VALUES(6, 'Citroen', 21000);"
260 "INSERT OR IGNORE INTO Cars VALUES(7, 'Hummer', 41400);"
261 "INSERT OR IGNORE INTO Cars VALUES(8, 'Volkswagen', 21600);";
262//to print sql used g_print("%s\n",sql);
263
264unsigned int num_dirs;//char *tmphashname;
265num_dirs = HASH_COUNT(folderpathtmp);
266printf("there are %u dirs and %u i's\n", num_dirs,--idir);
267
268
269
270HASH_ITER(hh, folderpathtmp, s, tmp) {
271 printf("hmm id %d: name %s\n", s->id, s->name);
272 /* ... it is safe to delete and free s here */
273}
274
275
276/*
277const char *hashdata;
278for(s=folderpathtmp; s != NULL; s=s->hh.next) {
279 printf("hash id %d: data:%s:\n", s->id, s->name);
280 if(hashdata!=s->name){
281 hashdata=s->name;
282 //printf("hash id %d: data:%s:\n", s->id, s->name);
283 }else{
284 //printf("_hash id %d: data:%s:\n", s->id, s->name);
285 }
286}
287*/
288rc = sqlite3_exec(db, sql, 0, 0, &err_msg);
289//g_print("%i\n",*sql);
290if (rc != SQLITE_OK ) {
291 fprintf(stderr, "SQL error: %s\n", err_msg);
292 sqlite3_free(err_msg);
293 sqlite3_close(db);
294 return 1;
295}
296/* //list all data in table
297sql = "SELECT * FROM Cars";
298rc = sqlite3_exec(db, sql, callback, 0, &err_msg);
299
300if (rc != SQLITE_OK ) {
301 fprintf(stderr, "Failed to select data\n");
302 fprintf(stderr, "SQL error: %s\n", err_msg);
303 sqlite3_free(err_msg);
304 sqlite3_close(db);
305 return 1;
306} */
307//
308// initialize gtk
309GtkBuilder *builder;
310GtkWidget *window;
311GObject *button;
312GObject *window2;
313//GError *error = NULL;
314gtk_init(&argc, &argv);
315
316builder = gtk_builder_new();
317gtk_builder_add_from_file (builder, "hello.glade", NULL);
318
319/* Connect signal handlers to the constructed widgets. */
320window2 = gtk_builder_get_object (builder, "window_main");
321g_signal_connect (window2, "destroy", G_CALLBACK (gtk_main_quit), NULL);
322
323button = gtk_builder_get_object (builder, "quit");
324g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
325
326window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
327
328if(window == NULL){g_print("window is NULL");}
329gtk_builder_connect_signals(builder, NULL);
330
331// get pointers to the two labels
332g_lbl_hello = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_hello"));
333g_lbl_count = GTK_WIDGET(gtk_builder_get_object(builder, "lbl_count"));
334
335
336g_object_unref(builder);
337
338gtk_widget_show(window);
339g_print("\n");
340
341g_print ("Check point 2\n");
342
343//rc = sqlite3_step(res); if (rc == SQLITE_ROW) { printf("%s\n", sqlite3_column_text(res, 0));} sqlite3_finalize(res);
344sqlite3_close(db);
345
346/* free the hash table contents */
347HASH_ITER(hh, folderpathtmp, s, tmp) {
348 HASH_DEL(folderpathtmp, s);
349 free(s);
350}
351//g_print ("Check point 3\n");
352
353gtk_main();
354
355//g_print ("Check point 3\n");
356return 0;
357 }
358// MAIN END
359
360// FUNCTION called when button is clicked
361void on_btn_hello_clicked()
362{
363 static unsigned int count = 0;
364 char str_count[30] = {0};
365
366 gtk_label_set_text(GTK_LABEL(g_lbl_hello), "Hello, world!");
367 count++;
368 sprintf(str_count, "%d", count);
369 gtk_label_set_text(GTK_LABEL(g_lbl_count), str_count);
370}
371
372// FUNCTION called when window is closed
373void on_window_main_destroy()
374{
375 gtk_main_quit();
376}