Lars Gullik Bjønnes wrote:
> | } else
> | strcpy(fname, filename);
> | }
> | strcat(fname, ext);
> | }
> 
> It almost seems to me that the else clause should just be
> 
>    strcpy(fname, filename_only(filename));

I think you're correct. Thanks.

> | build_fname(fname, "foo/bar/baz", ".h");
> 
> If my above method was used this would produce just baz.h. but the
> old way would produce foo/bar/baz.h

Correct. 

The test program seems to work exactly as you want.

Jean-Marc, are you happy with the change?

Index: fdesign/fd_printC.c
===================================================================
RCS file: /cvsroot/xforms/xforms/fdesign/fd_printC.c,v
retrieving revision 1.8
diff -u -p -r1.8 fd_printC.c
--- fdesign/fd_printC.c 27 Nov 2003 18:15:00 -0000      1.8
+++ fdesign/fd_printC.c 27 May 2004 16:50:46 -0000
@@ -82,7 +82,7 @@ build_fname(char * fname, char const * f
            strcat(fname, "/");
        strcat(fname, filename_only(filename));
     } else
-       strcpy(fname, filename);
+       strcpy(fname, filename_only(filename));
     strcat(fname, ext);
 }


-- 
Angus
#include <stdio.h>
#include <limits.h>
#include <string.h>

typedef struct {
	char * output_dir;
} FD_Opt;

FD_Opt fdopt;

static char const *
filename_only(char const * filename)
{
	char const * ptr = strrchr(filename, '/');
	if (ptr)
		return ptr+1;
	return filename;
}

/* Given 'filename' (no extension) and 'ext', generate 'fname' */
static void
build_fname(char * fname, char const * filename, char const * ext)
{
	if (fdopt.output_dir) {
		strcpy(fname, fdopt.output_dir);
		if (fname[strlen(fdopt.output_dir) - 1] != '/')
			strcat(fname, "/");
		strcat(fname, filename_only(filename));
	} else
		strcpy(fname, filename_only(filename));
	strcat(fname, ext);
}

int main(int argc, char * argv[])
{
	char fname[PATH_MAX + 1];

	fdopt.output_dir = 0;
	if (argc > 1) {
		fdopt.output_dir = (char *)malloc(strlen(argv[1]) + 1);
		strcpy(fdopt.output_dir, argv[1]);
	}

	build_fname(fname, "foo/bar/baz", ".h");

	printf("Output fname is \"%s\"\n", fname);
	return 0;
}	

Reply via email to