[Coco] basename() source

Robert Gault robert.gault at worldnet.att.net
Sat Dec 31 14:20:07 EST 2005

After about 15 years of not using C and trying to relearn it in about 
one week, I am remembering why I detest the language. :)

Bob, your program works and seems simple but after digging into this 
further I've come up with some interesting finds and thoughts.

Clearly there is a problem with the Microware compiler but I have found 
what triggers the lead mmnn,pcr syntax.

  strrchr(file,"/" || "\\")
gives the lead opcode but
  strrchr(file,'/' || '\\')
does not. Both of them give the ldd #1, which does not make any sense 
but that's another story. The trigger was the double vs single quote.

A short test program (below)displays some information on how strrchr 
works in the OS-9 software. It also suggests how your program (below) 
could be trimmed of excess code. Here is the program and the relevant 
part of the resulting assembly code.

/* test for strrchr() */

#include <string.h>
#include <stdio.h>

   char *file = "/dd/src/test";
   char *name;

   name = strrchr(file,'/');
   name = strrchr(file,'\\');
   printf ("%s\n",name+1);
   printf ("%s",file);

The above as it stands does not give the desired result but does show 
that strrchr does not change the string file. Here is the relevant 
assembly code.

  ldd #'\             this was #92
  pshs d
  ldd 4,s
  lbsr strrchr
  leas 4,s
  std 0,s             save the result of strrchr
  ldd #'/              this was #47
  pshs d
  ldd 4,s
  lbsr strrchr
  leas 4,s
  std 0,s          if this line and the next are replaced with addd 0,s
  ldd 0,s          then the second result won't trash the first
  addd #1
  pshs d
  leax _3,pcr
  pshs x
  lbsr printf

Note above where the result of one strrchr is trashed by the second. 
Since strrchr returns either $0000 or the address of the right-most 
character, all we need is to add the result of the second strrchr to the 
first to duplicate your code in a smaller package.
   I can't remember or find any C syntax that will result in the above 
code. Hand trimming of the assembly code is not the most desirable thing 
in the world even if it produces better code than the optimizer can.

Anyone see a way to get this result from the C code alone?

Bob Devries wrote:

> Here's my source for basename() :
> char *basename(pathname)
> char *pathname;
> {
>       char *name;
>       char *strrchr();
>       if ((name = strrchr(pathname, '/')) == NULL)
>               {
>               if ((name = strrchr(pathname, '\\')) == NULL)
>                       {
>                       return(pathname);
>                       }
>               }
>       return(name + 1);
> }
> It works on both windows(DOS Box) and OS9 (MESS).
> -- 
> Regards, Bob Devries, Dalby, Queensland, Australia
> Isaiah 50:4 The sovereign Lord has given me
> the capacity to be his spokesman,
> so that I know how to help the weary.
> website: http://www.home.gil.com.au/~bdevasl
> my blog: http://bdevries.invigorated.org/

More information about the Coco mailing list