# simplegdbmio: my first useful program, check it out!



## lockfile (Oct 21, 2012)

I wrote this program to finally test out my c skills. I know I still have none, but at least I finally managed to write something useful. The hardest point was getting my head wrapped around pointers. I spent about 4 days writing the program and about 2 days messing around with the pointers. Pitiful I know.

I personally am going to use it for some scripts I got lying around here. This program can by like the pgsql or mysql command line tools to access PGSQL AND MYSQL databases. Can I call it gdbmnosql? Well, nonetheless, this program will have plenty of improvements in the future. The code seems noobish, as I only know the basics of good design practices. 

And thanks in advance for the comments!


Synopsis
        [cmd=]simplegdbmio gdbmfile "key string"[/cmd]
        [cmd=]simplegdbmio gdbmfile "key string" "value string"[/cmd]

Purpose
Write or read simple key value pairs to a gdbm database. To read use two arguments. To write, use three arguments.

To install

```
cd <src dir>; make;
```
Then manually copy to your ~/bin folder


----------



## tmw (Oct 24, 2012)

Hello,
First thing 
gdbm is by default installed in /usr/local so I modify your Makefile to:


```
gcc -L/usr/local/lib -I/usr/local/include -o simplegdbmio simplegdbmio.c -lgdbm
```

Second thing 

```
[tmw@amszilas ~/simplegdbmio-1.0.0]$ ./simplegdbmio 
Segmentation fault: 11 (core dumped)
```
You are assuming that argv values will be in place. You should check if user provide arguments.

Third thing 

```
[tmw@amszilas ~/simplegdbmio-1.0.0]$ ./simplegdbmio gdbmfile "key string"
error: database failed to open
exitting....
Segmentation fault: 11 (core dumped)
```

if the gdbm file does not exist you are not exit the program and goes forward, you should exit to avoid this Segmentation fault.

Those are my comments for the first look at your code.


----------



## tmw (Oct 24, 2012)

Continue:


```
[tmw@amszilas /usr/home/tmw/simplegdbmio-1.0.0]$ ./simplegdbmio plik lala lala
error: database failed to open
exitting....
Segmentation fault: 11 (core dumped)
```

If file does not exists the program crash.


```
GDBM_FILE dbf =  gdbm_open(&argv[1][0],0,GDBM_WRITER,01700,NULL);
```

Why do you write &argv[1][0] and not just argv[1] ?


```
if ( !read_data(dbf,key)) // if read fails give error
     printf("error: reading GDBM file failed\n");
   }
```

This is not a bug but IMO error messages should go to the stderr.


----------



## lockfile (Oct 27, 2012)

Yea the program is very rudimentary. There is very little error checking throughout the entire program. It is a newb project after all, but yes, you're right. I will go in there and fix up some stuff. 

Thanks for reviewing. The code has been on here for some time and I was wondering how long unti someone said something!


----------



## lockfile (Oct 27, 2012)

*Errors fixed*

I wrote this thing just to get something of use made, I did not have any error checking code in it because it's just a toy to me. See bugfixed v1.0.1.

Issues addressed:

Include freebsd version, namely addressing compiler flags. I'm on Linux, but I come on here because guys on here are well... different. Makefile has been changed. But I can't assure that it will compile. I don't even have FreeBSD in a VM at the moment.
When no args given it segfaults--fixed. Now when improper argslist given. It exit(1) and puts message into stderr.
If no database args it segfaults; fixed, now exits with error.
Errors should be printed to stderr; fixed, not all but the important ones.
Why not use &argv[n] rather than &argv[n]_? Because gdbm_open() takes char* not char**._


----------



## tmw (Oct 28, 2012)

Hello,
First thing. Your Makefile is wrong, you have a typo in -L option it is not /usr/include/lib but /usr/local/lib

And what about issue 5  argv[n] it is char *, argv is char **. Please have a look the first compilation with argv[n]


```
[tmw@freebsd ~/simplegdbmio-1.0.1]$ grep 'gdbm_open' simplegdbmio.c
GDBM_FILE dbf =  gdbm_open(argv[1],0,GDBM_WRITER,01700,NULL);
   fputs("gdbm_open() fail\n",stderr);
[tmw@freebsd ~/simplegdbmio-1.0.1]$ make
gcc -o simplegdbmio simplegdbmio.c -L/usr/local/lib -I/usr/local/include -lgdbm
```

No pointer error or warning but when we will write argv (this is just to show the ** and * issue)


```
[tmw@freebsd ~/simplegdbmio-1.0.1]$ make
gcc -o simplegdbmio simplegdbmio.c -L/usr/local/lib -I/usr/local/include -lgdbm
simplegdbmio.c: In function 'main':
simplegdbmio.c:25: warning: passing argument 1 of 'gdbm_open' from incompatible pointer type
```

Then you have incompatibile pointer warning  because pointer is ** not * and argv[n] is *

Nevertheless your code is not working on FreeBSD 8.2  please have a look


```
[tmw@freebsd ~/simplegdbmio-1.0.1]$ ./simplegdbmio gdbmfile "key string" "value string"
Segmentation fault: 11 (core dumped)
```

Or I`m doing something wrong? 
IMO you should install freebsd on some virtualbox or whatever and test it there because when people on freebsd forum will download it and try to run, they will probably do it on freebsd .


----------



## lockfile (Oct 31, 2012)

I don't know man :q

I'll install 8.2 in a VM to see what combobulations are happening; I have a disk laying around here in my collection. My code compiles fine with GCC 4.4.6 on CentOS (my personal "workstation" distro).

It works on my machine x86-64 Linux (CENTOS). Compiles fine, runs fine, and does not segfaults. This is weird. I'm using GNU's GDBM if that makes any sense, but it's pure ANSI-C so there should not be much trouble with this simple program.


----------

