Automatic Filename Extensions

William E. Davidsen Jr davidsen at steinmetz.ge.com
Tue Apr 12 00:47:51 AEST 1988


In article <12901 at brl-adm.ARPA> C04661DC%WUVMD.BITNET at CUNYVM.CUNY.EDU (David Camp) writes:
| [...]
|      I recently realized that this fails for the odd filename
| with '.' in the path, such as ".filename" or "..filename".
| To fix it, I shall use:
| 
| if (filename [strlen (filename) - 1] != '.')
|     strcat (filename, ".EXT");

in UNIX that's fine, a name like a.b may have .EXT added, but it won't
work on other systems, such as MSDOS and VMS. How about isolating just
the filename via strrchr (or rindex) and then using strchr as you have
been. Something like:
	char *temp;
	if ((temp = strrchr(filename, '/')) == NULL) temp = filename;
	if (strchr(temp, '.') == NULL) strcat (temp, ".EXT");
  Of course you can do this in one statement without the temp if you use
nested ?:, but I won't put that code over my .sig.
-- 
	bill davidsen		(wedu at ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list