Angus Leeming <[EMAIL PROTECTED]> writes:

| I believe that we achieve what you're looking for with
| -    } else
| -       strcpy(fname, filename);
| +    } else {
| +       size_t const buffer_size = 512;
| +       char buffer[buffer_size];
| +       char * cwd = getcwd(buffer, buffer_size);
| +       if (cwd) {
| +               strcpy(fname, cwd);
| +               strcat(fname, "/");
| +               strcat(fname, filename_only(filename));
| +       } else
| +               strcpy(fname, filename);

Why isn't filename_only used here?

| 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 {
|               size_t const buffer_size = 512;
|               char buffer[buffer_size];
|               char * cwd = getcwd(buffer, buffer_size);
|               if (cwd) {
|                       strcpy(fname, cwd);
|                       strcat(fname, "/");
|                       strcat(fname, filename_only(filename));
|               } else
|                       strcpy(fname, filename);
|       }
|       strcat(fname, ext);
| }

It almost seems to me that the else clause should just be

   strcpy(fname, filename_only(filename));

and that you don't have to fiddle with getcwd at all?

| int main(int argc, char * argv[])
| {
|       char fname[512];

Is this big enough?
(I have no idea what the actual value in xforms is, but PATH_MAX is a
nice value)

|       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");

If my above method was used this would produce just baz.h. but the old
way would produce foo/bar/baz.h

-- 
        Lgb

Reply via email to