simplegdbmio: my first useful program, check it out!

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
Code:
cd <src dir>; make;
Then manually copy to your ~/bin folder :)
 

Attachments

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

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

Second thing :)
Code:
[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 :)
Code:
[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.
 
Continue:

Code:
[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.

Code:
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] ?

Code:
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.
 
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!
 
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:
  1. 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.
  2. When no args given it segfaults--fixed. Now when improper argslist given. It exit(1) and puts message into stderr.
  3. If no database args it segfaults; fixed, now exits with error.
  4. Errors should be printed to stderr; fixed, not all but the important ones.
  5. Why not use &argv[n] rather than &argv[n]? Because gdbm_open() takes char* not char**.
 

Attachments

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]

Code:
[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)

Code:
[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

Code:
[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 :).
 
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.
 
Back
Top