I have a function that begins like this:
int parseFile(char *filename, char *dirname) {
int rv, roomStarter = 0;
char buffer[100];
FILE *fp;
//char * slash = malloc(1 * sizeof(char));
char * catfile = malloc(strlen(filename) + strlen(dirname) + 2);
strcpy(catfile,dirname);
strcat(catfile,"/");
strcat(catfile,filename);
printf("file is %s and dirname is %s and size is %d catfile is %s \n",filename,dirname,strlen(dirname),catfile);
fp = fopen(catfile, "r");
[snip]
The output is not what I expected, which is a qualified path to unk_room. I either get something like this:
$ ./prog
file is unk_room and dirname is ./dirname and size is 21
file is unk_room and dirname is ./dirname and size is 21 catfile is
Error opening file unk_room.
$
(Note, catfile is empty, and it tries to open filename)
or, errors about corrupt memory:
catfile is ./myprog.23202/
catfile is ./myprog.23202/gib_room
*** Error in `./myprog': double free or corruption (out): 0x0000000001f43000 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x81679)[0x7fa1eb7d7679]
/lib64/libc.so.6(+0xd6702)[0x7fa1eb82c702]
/lib64/libc.so.6(regfree+0x11)[0x7fa1eb837421]
./myprog[0x401ac0]
./myprog[0x401b90]
./myprog[0x401d3e]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7fa1eb778505]
./myprog[0x4010a9]
======= Memory map: ========
00400000-00403000 r-xp 00000000 00:32 3111810123 /nfs/stak/users/smithj/CS344/hw2/myprog
00602000-00603000 r--p 00002000 00:32 3111810123 /nfs/stak/users/smithj/CS344/hw2/myprog
00603000-00604000 rw-p 00003000 00:32 3111810123 /nfs/stak/users/smithj/CS344/hw2/myprog
01f3a000-01f7c000 rw-p 00000000 00:00 0 [heap]
7fa1e4000000-7fa1e4021000 rw-p 00000000 00:00 0
7fa1e4021000-7fa1e8000000 ---p 00000000 00:00 0
What am I missing?