[comp.lang.c] Automatic Filename Extensions

C04661DC%WUVMD.BITNET@CUNYVM.CUNY.EDU (David Camp) (04/11/88)

     A programming technique I have frequently used is:

if (strchr (filename, '.') == NULL)
    strcat (filename, ".EXT");

in order to implement default filename extensions.  If the user
wanted to specify a filename with no extension, he simply ends
it with a '.' character.

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

I hope this is helpful to others struggling to get it right.
-David-

*----------------------------------------------------------------------*
| (314) 362-3635                  Mr. David J. Camp                    |
|                          ^      Division of Biostatistics, Box 8067  |
| Room 1108D             < * >    Washington University Medical School |
| 706 South Euclid         v      660 South Euclid                     |
|                                 Saint Louis, MO 63110                |
| Bitnet: C04661DC@WUVMD.BITNET                                        |
| Internet: C04661DC%WUVMD.BITNET@CUNYVM.CUNY.EDU                      |
*----------------------------------------------------------------------*

craig@srs.UUCP (Craig Schmackpfeffer) (04/11/88)

In article <12901@brl-adm.ARPA> C04661DC%WUVMD.BITNET@CUNYVM.CUNY.EDU (David Camp) writes:
>
>     A programming technique I have frequently used is:
>
>if (strchr (filename, '.') == NULL)
>    strcat (filename, ".EXT");
>-David-

Here at SR, we have many signal processing programs which are able to
contort signals into just about any new format.  We have adopted "."
suffixes (which can be supplied on the command line) to help organize 
this mess.

The big problem occurs when you try to do > 1 extension.  Our solution
was to write an add_extension(fname, extension) function.  If extension is
not empty (or not NULL), fname is truncated after the last '.' and extension
is appended.  It works very nicely.

Craig
-- 
Craig Schmackpfeffer  @ S.R. Systems
{allegra,rutgers,ames}!rochester!srs!craig

davidsen@steinmetz.ge.com (William E. Davidsen Jr) (04/11/88)

In article <12901@brl-adm.ARPA> C04661DC%WUVMD.BITNET@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@ge-crd.arpa)
  {uunet | philabs | seismo}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me

loafman@convex.UUCP (04/12/88)

/* Written  9:47 am  Apr 11, 1988 by davidsen@steinmetz.Sun.COM */
[...]
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:
[...]
/* End of text from convex:comp.lang.c */

Actually MSDOS is a little more perverted than you have illustrated,
i.e. the name:
     d:\bin\glarp.20
may be a filename _or_ a directory.  The only way to tell for sure is
to use something along the lines of an access() call to check it.
I've found no syntax yet that will guarantee what that name means.

--------------------------------------------------------
Kenneth W. Loafman @ Convex Computer Corp, Dallas, Texas
USPSnail:   701 North Plano Rd, Richardson TX 75083-3851
UUCP:	    {allegra,ihnp4,uiucdcs,ctvax}!convex!loafman
CompuServe: 72345,233              Phone: (214) 952-0829
Disclaimer: opinion->loafman != opinion->convex
--------------------------------------------------------