[GRASS5] DB default settings now defined in new mapset

Paul Kelly paul-grass at stjohnspoint.co.uk
Tue Oct 18 11:07:56 EDT 2005


Hello Markus

On Tue, 18 Oct 2005, Markus Neteler wrote:

> Unfortunately, I don't manage to program it:
> In file lib/init/mke_mapset.c
>
> /* generate DB settings file in new mapset */
>        sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
>        if ((fd = creat (buffer, O_RDWR)) < 0) {
>                perror (buffer);
>                G_fatal_error("Cannot create VAR file in new mapset");
>        }
>
>        fprintf (fd, "DB_DRIVER: dbf\n");
>        fprintf (fd, "DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n");
>        fclose (fd);
>
> mke_mapset.c: In function `make_mapset':
> mke_mapset.c:33: warning: assignment makes pointer from integer without a cast
>
> My usual string problems... Then it happily segfaults.
> Any idea?

I think creat() only returns a file descriptor. But fprintf and fclose 
have a stream as their first argument. So you need to call fdopen() to 
derive a stream from the file descriptor returned by creat. I think. But 
why not just do it all in one step with fopen() instead of creat(). I have 
never seen creat() used like that.

Also to be more buffer overflow-safe you could write
sprintf(buffer,"'%s'/'%s'/VAR", location, mapset);
instead as
char *buffer;
G_asprintf(&buffer,"'%s'/'%s'/VAR", location, mapset);
/* do something with buffer */
G_free(buffer);

Hope this helps a little bit.

Paul




More information about the grass-dev mailing list