home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 21 / CD_ASCQ_21_040595.iso / dos / prg / c / freedos3 / source / jh_utils / fbasenam.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-01-07  |  2.5 KB  |  93 lines

  1. #define ANSI            /* Uncomment to use ANSI C */
  2.  
  3. /*
  4. Here it is.  Sorry about the lack of comments in the code, but I've found
  5. it's more useful (and often more efficient) to spend more time commenting
  6. the function header than it is to comment the code.  But first, here is
  7. some sample output of the test program:
  8.  
  9. Path name = "c:\private\freedos\freedos.exe"
  10. Base name = "FREEDOS"
  11.  
  12. Path name = "c:\foo.old\foo"
  13. Base name = "FOO"
  14.  
  15. Path name = "foo.exe"
  16. Base name = "FOO"
  17. */
  18.  
  19. /*
  20.    John Hall, <jchall@tasc.com>
  21.  
  22.    This program is free software; you can redistribute it and/or modify
  23.    it under the terms of the GNU General Public License as published by
  24.    the Free Software Foundation; either version 2 of the License, or
  25.    (at your option) any later version.
  26.  
  27.    This program is distributed in the hope that it will be useful,
  28.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.    GNU General Public License for more details.
  31.  
  32.    You should have received a copy of the GNU General Public License
  33.    along with this program; if not, write to the Free Software
  34.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36.  
  37. #include <stdio.h>        /* for NULL */
  38. #include <string.h>        /* for string functions (strrchr, strcpy, etc.) */
  39.  
  40. typedef int BOOL;
  41. #define FALSE (0)
  42. #define TRUE  (1)
  43.  
  44. /************************************************************************
  45.  * Returns in szDest the program name extracted from szPathName,
  46. assumed
  47.  * to originate from argv[0].  The program name is extracted by stripping
  48.  * off all leading paths as well as any extension to the file name.  The
  49.  * program name is converted to uppercase.
  50.  *
  51.  * For example, after calling it like so:
  52.  *
  53.  * fBaseName(szBuffer, "c:\\free-dos\\beta\\_type.exe");
  54.  *
  55.  * 'szBuffer' would contain:
  56.  *
  57.  * _TYPE
  58.  *
  59.  * Returns: TRUE if successful, FALSE otherwise
  60.  */
  61. BOOL 
  62. fBasename (char *szDest, char *szPathName)
  63. {
  64.   char *szBaseName, *pchDot;
  65.   int cch;
  66.  
  67.   if ((szDest == NULL) || (szPathName == NULL))
  68.     return FALSE;
  69.  
  70.   szBaseName = strrchr (szPathName, '\\');
  71.   if (szBaseName == NULL)
  72.     szBaseName = szPathName;
  73.   else
  74.     szBaseName++;
  75.   pchDot = strrchr (szPathName, '.');
  76.  
  77.   if ((pchDot == NULL) || (pchDot < szBaseName))
  78.     strcpy (szDest, szBaseName);
  79.   else
  80.     {
  81.       cch = pchDot - szBaseName;
  82.  
  83.       strncpy (szDest, szBaseName, cch);
  84.       szDest[cch] = '\0';
  85.     }
  86.  
  87. #ifndef ANSI
  88.   strcpy (szDest, strupr (szDest));
  89. #endif /* ANSI */
  90.  
  91.   return TRUE;
  92. }
  93.