Filename Extensions
David Camp
C04661DC%WUVMD.BITNET at CORNELLC.CCS.CORNELL.EDU
Wed Apr 13 11:38:02 AEST 1988
Netlanders,
A few days ago I posted a note regarding the conditions
for appending an extension to a filename. I have not been
reading the list, so I do not know how extensively I have
been flamed. There is clearly an error in that method, as
it requires the '.' character to be at the very end of the
filename. To help make up for my error, I am including
a function exten(), which fully implements a reasonable
algorithm for applying an extension. Sorry, but it is
specially coded for MS-Dos. If you want to contact me,
do so directly by mail. I would love to read INFO-C, if
only I had the time.
-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 at WUVMD.BITNET |
| Internet: C04661DC%WUVMD.BITNET at CUNYVM.CUNY.EDU |
*----------------------------------------------------------------------*
--------------------- FILE exten c ---------------------
--------------------------- cut here --------------------------------
/* exten.c -- function to conditinally add extension to a filename */
/* written by David J. Camp */
/* of the Washington University Division of Biostatistics */
/* exten (filename, extension) uses strcat to append "." and extension */
/* to filename only if there is no '.' character after the last '/' or */
/* '\\' character in the filename. MS-Dos reserved names are also */
/* excluded. This allows the user to specify a name with a trailing */
/* '.' character indicating they want no extension. */
#include <stddef.h>
#include <string.h>
char * exten (char *, char *);
char * exten (filename, extension)
char * filename;
char * extension;
{
char * ptrslash;
char * ptrback;
char * ptrdot;
char ** ptrdev;
static char * devlist [] = {
"CON", "AUX", "COM1", "COM2", "COM3", "COM4",
"LPT1", "PRN", "LPT2", "LPT3", "NUL", "" };
char nameholder [256];
strcpy (nameholder, filename);
strupr (nameholder);
for (ptrdev= devlist; *ptrdev[0] != '\0'; ptrdev++)
{
if (strcmp (*ptrdev, nameholder) == 0)
break;
}
ptrslash = strrchr (nameholder, '/');
ptrback = strrchr (nameholder, '\\');
ptrdot = strrchr (nameholder, '.');
if (**ptrdev == '\0' && (ptrdot == NULL ||
ptrslash != NULL && ptrdot < ptrslash ||
ptrback != NULL && ptrdot < ptrback))
{
strcat (filename, ".");
strcat (filename, extension);
}
return (filename);
}
--------------------------- cut here --------------------------------
--------------------- FILE testext c ---------------------
--------------------------- cut here --------------------------------
/* testext.c -- program to test exten.c */
#include <stdio.h>
#include <string.h>
char * exten (char *, char *);
main ()
{
char filename [256];
do
{
fscanf (stdin, "%s", filename);
fprintf (stdout, "%s\n", exten (filename, "tst"));
}
while (1);
}
--------------------------- cut here --------------------------------
More information about the Comp.lang.c
mailing list