From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/chgext.c | 71 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/chgext.c (limited to 'reference/C/CONTRIB/SNIP/chgext.c') diff --git a/reference/C/CONTRIB/SNIP/chgext.c b/reference/C/CONTRIB/SNIP/chgext.c new file mode 100755 index 0000000..b394572 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/chgext.c @@ -0,0 +1,71 @@ +/* +** chgext.c - Change a file's extension +** +** public domain by Bob Stout +** +** Arguments: 1 - Pathname +** 2 - Old extension (NULL if don't care) +** 3 - New extension +** +** Returns: Pathname or NULL if failed +** +** Note: Pathname buffer must be long enough to append new extension +** +** Side effect: Converts Unix style pathnames to DOS style +*/ + +#include +#include + +char *chgext(char *path, char *oldext, char *newext) +{ + char *p; + + /* Convert to DOS-style path name */ + + for (p = path; *p; ++p) + if ('/' == *p) + *p = '\\'; + + /* Find extension or point to end for appending */ + + if (NULL == (p = strrchr(path, '.')) || NULL != strchr(p, '\\')) + p = strcpy(&path[strlen(path)], "."); + ++p; + + /* Check for old extension */ + + if (oldext && strcmp(p, oldext)) + return NULL; + + /* Add new extension */ + + while ('.' == *newext) + ++newext; + strncpy(p, newext, 3); + return path; +} + +#ifdef TEST + +void main(int argc, char *argv[]) +{ + char *retval, *old_ext = NULL, path[128]; + + if (2 > argc) + { + puts("Usage: CHGEXT path [old_ext]"); + puts("\nChanges extension to \".TST\""); + puts("Old extension optional"); + return; + } + strcpy(path, strupr(argv[1])); + if (2 < argc) + old_ext = strupr(argv[2]); + if (NULL == (retval = chgext(path, old_ext, ".TSTstuff"))) + puts("chgext() failed"); + else printf("chgext(%s, %s, TST)\n...returned...\n%s\n", argv[1], + old_ext ? old_ext : "NULL", path); +} + +#endif /* TEST */ -- cgit v1.2.3-54-g00ecf