39 |
|
|
40 |
#include "ospath.h" |
#include "ospath.h" |
41 |
|
|
42 |
/* to test this code, 'gcc -DTEST ospath.c && ./a' */ |
//#define OSPATH_DEBUG |
43 |
|
|
44 |
#ifndef __GNUC__ |
#ifndef __GNUC__ |
45 |
# ifndef __FUNCTION__ |
# ifndef __FUNCTION__ |
50 |
|
|
51 |
/* #define VERBOSE */ |
/* #define VERBOSE */ |
52 |
|
|
53 |
#if !defined(TEST) && !defined(VERBOSE) |
#if !defined(VERBOSE) |
54 |
#ifndef NDEBUG |
#ifndef NDEBUG |
55 |
# define NDEBUG |
# define NDEBUG |
56 |
#endif |
#endif |
610 |
char sub[PATH_MAX]; |
char sub[PATH_MAX]; |
611 |
struct FilePath *fp1, *fp2; |
struct FilePath *fp1, *fp2; |
612 |
|
|
613 |
|
#ifdef OSPATH_DEBUG |
614 |
|
fprintf(stderr,"finding parent of %s\n",fp->path); |
615 |
|
#endif |
616 |
D(fp); |
D(fp); |
617 |
|
|
618 |
if(strlen(fp->path) == 0){ |
if(strlen(fp->path) == 0){ |
621 |
ospath_free(fp1); |
ospath_free(fp1); |
622 |
return fp2; |
return fp2; |
623 |
}else if(ospath_isroot(fp)){ |
}else if(ospath_isroot(fp)){ |
624 |
|
#ifdef OSPATH_DEBUG |
625 |
|
fprintf(stderr,"path is root\n"); |
626 |
|
#endif |
627 |
/* stay at root */ |
/* stay at root */ |
628 |
return ospath_new("/"); |
return ospath_new("/"); |
629 |
} |
} |
637 |
) ? length - 1 : length; /* then remove last char */ |
) ? length - 1 : length; /* then remove last char */ |
638 |
|
|
639 |
for(pos = fp->path + offset - 1; *pos!=PATH_SEPARATOR_CHAR && pos>=fp->path; --pos){ |
for(pos = fp->path + offset - 1; *pos!=PATH_SEPARATOR_CHAR && pos>=fp->path; --pos){ |
640 |
#if 0 |
#ifdef OSPATH_DEBUG |
641 |
fprintf(stderr,"CURRENT CHAR: %c\n",*pos); |
CONSOLE_DEBUG("CURRENT CHAR: %c\n",*pos); |
642 |
#endif |
#endif |
643 |
} |
} |
644 |
|
|
853 |
|
|
854 |
int ospath_isroot(struct FilePath *fp) |
int ospath_isroot(struct FilePath *fp) |
855 |
{ |
{ |
856 |
if(!ospath_isvalid(fp)) |
if(!ospath_isvalid(fp)){ |
857 |
{ |
#ifdef OSPATH_DEBUG |
858 |
|
fprintf(stderr,"path is invalid\n"); |
859 |
|
#endif |
860 |
return 0; |
return 0; |
861 |
} |
} |
862 |
|
|
863 |
return strcmp(fp->path, PATH_SEPARATOR_STR) ? 1 : 0; |
return strcmp(fp->path, PATH_SEPARATOR_STR) ? 0 : 1; |
864 |
} |
} |
865 |
|
|
866 |
unsigned ospath_depth(struct FilePath *fp){ |
unsigned ospath_depth(struct FilePath *fp){ |