Open64 (mfef90, whirl2f, and IR tools)  TAG: version-openad; SVN changeset: 916
x_libgen.c
Go to the documentation of this file.
00001 /* $Id: x_libgen.c,v 1.2 2004-02-28 21:05:05 eraxxon Exp $ */
00002 /* -*-Mode: C;-*- */
00003 /* * BeginRiceCopyright *****************************************************
00004  * 
00005  * ******************************************************* EndRiceCopyright */
00006 
00007 /*************************** System Include Files ***************************/
00008 
00009 /* #include <libgen.h> */
00010 
00011 #include <stdlib.h> 
00012 #include <limits.h>     /* for 'PATH_MAX' */
00013 #include <string.h>     /* for strcpy(), etc. */
00014 
00015 /**************************** User Include Files ****************************/
00016 
00017 #include "x_libgen.h"
00018 
00019 /**************************** Forward Declarations **************************/
00020 
00021 /****************************************************************************/
00022 
00023 /* Note: Cygwin does not yet support <libgen.h> so we provide our own
00024    implementation.  This code was lifted from be/be/dra_file_util.cxx. */
00025 
00026 static char tempbuf[PATH_MAX];
00027 
00028 extern char*
00029 ux_basename(char *const s)
00030 {
00031   register char *p;
00032   register char *const t = tempbuf;
00033   
00034   if (s == NULL || *s == 0) {
00035     return strcpy(t, ".");
00036   } else {
00037     p = strcpy(t, s);
00038     p += strlen(p);
00039     while( p != t  &&  *--p == '/' )        /* skip trailing /s */
00040       *p = '\0';
00041     while( p != t ) {
00042       if( *--p == '/' )
00043         return  ++p;
00044     }
00045     return p;
00046   }
00047 }
00048 
00049 
00050 extern char*
00051 ux_dirname(char *const s)
00052 {
00053   register char *p;
00054   register char *const t = tempbuf;
00055   
00056   if (s == NULL || *s == 0) {
00057     return strcpy(t, ".");
00058   } else {
00059     p = strcpy(t, s);
00060     p += strlen(p);
00061     while( p != t  &&  *--p == '/' )        /* skip trailing /s */
00062       ;
00063     
00064     if ( p == t && *p == '/' )
00065       return strcpy(t, "/");
00066     
00067     while( p != t ) {
00068       if( *--p == '/' ) {
00069         if ( p == t )
00070           return strcpy(t, "/");
00071         while ( *p == '/' )
00072           p--;
00073         *++p = '\0';
00074         return  t;
00075       }
00076     }
00077     return strcpy(t, ".");
00078   }
00079 }
00080 
00081 /****************************************************************************/
00082 
00083 /* Here is yet another implementation we found in
00084    crayf90/sgi/cwh_pdgcs.cxx.  We provide it for reference. 
00085 
00086    Note that these routines make memory management impossible,
00087    sometimes returning a static or malloc'd string!
00088 */
00089 
00090 #if 0 /* HIDE */
00091 
00092 static char *
00093 basename ( char * const s )
00094 {
00095   char * p;
00096   char * last;
00097   char * name;
00098   int    size;
00099 
00100   if ( s == NULL || *s == '\0' )
00101     return ".";
00102 
00103   else {
00104 
00105     p = s + strlen ( s );
00106 
00107     /* skip trailing '/' */
00108 
00109     while ( p != s && *p == '/' )
00110       --p;
00111 
00112     last = p;
00113 
00114     while ( p != s ) {
00115 
00116       if ( *--p == '/' ) {
00117 
00118         ++p;
00119         break;
00120       }
00121     }
00122 
00123     size = last - p;
00124     name = (char *) malloc ( size + 1);
00125     strncpy ( name, p, size );
00126     name [size] = '\0';
00127 
00128     return name;
00129   }
00130 } /* basename */
00131 
00132 static char *
00133 dirname ( char * const s )
00134 {
00135   char * p;
00136   char * name;
00137   int    size;
00138 
00139   if ( s == NULL || *s == '\0' )
00140     return ".";
00141 
00142   else {
00143 
00144     p = s + strlen ( s );
00145 
00146     /* skip trailing '/' */
00147 
00148     while ( p != s && *p == '/' )
00149       --p;
00150 
00151     while ( p != s ) {
00152 
00153       if ( *--p == '/' ) {
00154 
00155         if ( p == s )
00156           return "/";
00157 
00158         while ( *p == '/' )
00159           --p;
00160 
00161         size = p - s + 1;
00162         name = (char *) malloc ( size + 1 );
00163         strncpy ( name, s, size );
00164         name [size] = '\0';
00165         return name;
00166       }
00167     }
00168 
00169     return ".";
00170   }
00171 } /* dirname */
00172 
00173 #endif /* HIDE */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines