Back to index

tetex-bin  3.0
xdirname.c
Go to the documentation of this file.
00001 /* xdirname.c: return the directory part of a path.
00002 
00003 Copyright (C) 1999 Free Software Foundation, Inc.
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 /* Return directory for NAME.  This is "." if NAME contains no directory
00020    separators (should never happen for selfdir), else whatever precedes
00021    the final directory separator, but with multiple separators stripped.
00022    For example, `xdirname ("/foo//bar.baz")' returns "/foo".  Always
00023    return a new string.  */
00024 
00025 #include <kpathsea/config.h>
00026 #include <kpathsea/c-pathch.h>
00027 
00028 string
00029 xdirname P1C(const_string, name)
00030 {
00031   string ret;
00032   unsigned limit = 0, loc;
00033 
00034   /* Ignore a NULL name. */
00035   if (!name)
00036       return NULL;
00037   
00038   if (NAME_BEGINS_WITH_DEVICE(name)) {
00039       limit = 2;
00040 #if defined(WIN32) || defined(__CYGWIN__)
00041   } else if (IS_UNC_NAME(name)) {
00042       for (limit = 2; name[limit] && !IS_DIR_SEP(name[limit]); limit++)
00043           ;
00044       if (name[limit]) {
00045           for (limit++ ; name[limit] && !IS_DIR_SEP(name[limit]); limit++)
00046               ;
00047           limit--;
00048       } else {
00049           /* malformed UNC name, backup */
00050           limit = 2;
00051       }
00052 #endif
00053   }
00054 
00055   for (loc = strlen (name); loc > limit && !IS_DIR_SEP (name[loc-1]); loc--)
00056       ;
00057 
00058   if (loc == limit && limit > 0) {
00059       if (limit == 2) {
00060           ret = (string)xmalloc(limit + 2);
00061           ret[0] = name[0];
00062           ret[1] = name[1];
00063           ret[2] = '.';
00064           ret[3] = '\0';
00065       } else {
00066           ret = (string)xmalloc(limit + 2);
00067           strcpy(ret, name);
00068       }
00069   } else {
00070       /* If have ///a, must return /, so don't strip off everything.  */
00071       while (loc > limit+1 && IS_DIR_SEP (name[loc-1])) {
00072           loc--;
00073       }
00074       ret = (string)xmalloc(loc+1);
00075       strncpy(ret, name, loc);
00076       ret[loc] = '\0';
00077   }
00078     
00079   return ret;
00080 }
00081 
00082 #ifdef TEST
00083 
00084 char *tab[] = {
00085   "\\\\neuromancer\\fptex\\bin\\win32\\kpsewhich.exe",
00086   "\\\\neuromancer\\fptex\\win32\\kpsewhich.exe",
00087   "\\\\neuromancer\\fptex\\kpsewhich.exe",
00088   "\\\\neuromancer\\kpsewhich.exe",
00089   "p:\\bin\\win32\\kpsewhich.exe",
00090   "p:\\win32\\kpsewhich.exe",
00091   "p:\\kpsewhich.exe",
00092   "p:bin\\win32\\kpsewhich.exe",
00093   "p:win32\\kpsewhich.exe",
00094   "p:kpsewhich.exe",
00095   "p:///kpsewhich.exe",
00096   "/usr/bin/win32/kpsewhich.exe",
00097   "/usr/bin/kpsewhich.exe",
00098   "/usr/kpsewhich.exe",
00099   "///usr/kpsewhich.exe",
00100   "///kpsewhich.exe",
00101   NULL 
00102 };
00103 
00104 int main()
00105 {
00106   char **p;
00107   for (p = tab; *p; p++)
00108     printf("name %s, dirname %s\n", *p, xdirname(*p));
00109   return 0;
00110 }
00111 #endif /* TEST */
00112 
00113 
00114 /*
00115 Local variables:
00116 standalone-compile-command: "gcc -g -I. -I.. -DTEST variable.c kpathsea.a"
00117 End:
00118 */
00119