在linux系统下,简单实现类似windows的_splitpath函数的功能
2011-11-13 15:06
1026 查看
在移植一个测试程序的过程中,遇到_splitpath函数,由于没有发现linux系统有类似的函数的,于是自己写了一个代码如下:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <libgen.h>
void _splitpath(const char *path, char *drive, char *dir, char *fname, char *ext)
{
char *tmp_path;
int conut;
char tmp[PATH_MAX];
char tmp_name[PATH_MAX];
char tmp_ext[PATH_MAX];
int check=0;
int check_1=0;
int i,j=0,k=0;
int conut_1,conut_2, conut_3;
tmp_path = NULL;
if( ( realpath(path, tmp_path) == NULL) )
{
tmp_path = path;
}
conut=0;
do
{
if( tmp_path[conut] == ':')
{
check=1;
conut_1 = conut;
}
if (tmp_path[conut] == '/')
{
conut_2 = conut;
}
if (tmp_path[conut] == '.')
{
check_1 = 1;
conut_3 = conut;
}
conut++;
}while(tmp_path[conut] != '\0');
if ( check == 1)
{
fname = basename(tmp_path);
dir = dirname(tmp_path);
for( i = 0 ; i< conut; i++)
{
if ( dir[i] != ':')
{
if( i>=2 )
{
tmp[i-2]=dir[i];
}
}
else
{
drive=dir[i-1];
}
}
memset(dir,0,sizeof(dir));
strcpy(dir,tmp);
}
else
{
fname = basename(tmp_path);
dir = dirname(tmp_path);
}
if (check_1 == 1)
{
while( fname[j] != '\0')
{
#ifdef 0 //后缀名不带"."
if ( fname[j] == '.')
{
k=j+1;
}
else
{
tmp_name[j]=fname[j];
if (j >=k && k != 0)
{
tmp_ext[j-k]=fname[j];
}
}
#endif
#ifdef 1 ////后缀名带"."
if ( fname[j] == '.')
{
k=j;
}
if ( j >= k && k !=0 )
{
tmp_ext[j-k]=fname[j ];
}
else
{
tmp_name[j]= fname[j];
}
#endif
j++;
}
memset(ext,0,sizeof(ext));
memset(fname, 0, sizeof(fname));
strcpy(fname, tmp_name);
strcpy(ext,tmp_ext);
}
printf("drive=====%c\n",drive);
printf("dir=======%s\n",dir);
printf("fname========%s\n",fname);
printf("ext======%s\n",ext);
}
int main()
{
char tracePath[PATH_MAX]="c:/test/tmp/share/copytest.test";
char ext[PATH_MAX];
char traceFile[PATH_MAX];
char dir[PATH_MAX];
char drive[PATH_MAX];
//strcpy(tracePath,"c:/test/tmp/1234/456/name.c");
_splitpath(tracePath,drive,dir,traceFile,ext);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <libgen.h>
void _splitpath(const char *path, char *drive, char *dir, char *fname, char *ext)
{
char *tmp_path;
int conut;
char tmp[PATH_MAX];
char tmp_name[PATH_MAX];
char tmp_ext[PATH_MAX];
int check=0;
int check_1=0;
int i,j=0,k=0;
int conut_1,conut_2, conut_3;
tmp_path = NULL;
if( ( realpath(path, tmp_path) == NULL) )
{
tmp_path = path;
}
conut=0;
do
{
if( tmp_path[conut] == ':')
{
check=1;
conut_1 = conut;
}
if (tmp_path[conut] == '/')
{
conut_2 = conut;
}
if (tmp_path[conut] == '.')
{
check_1 = 1;
conut_3 = conut;
}
conut++;
}while(tmp_path[conut] != '\0');
if ( check == 1)
{
fname = basename(tmp_path);
dir = dirname(tmp_path);
for( i = 0 ; i< conut; i++)
{
if ( dir[i] != ':')
{
if( i>=2 )
{
tmp[i-2]=dir[i];
}
}
else
{
drive=dir[i-1];
}
}
memset(dir,0,sizeof(dir));
strcpy(dir,tmp);
}
else
{
fname = basename(tmp_path);
dir = dirname(tmp_path);
}
if (check_1 == 1)
{
while( fname[j] != '\0')
{
#ifdef 0 //后缀名不带"."
if ( fname[j] == '.')
{
k=j+1;
}
else
{
tmp_name[j]=fname[j];
if (j >=k && k != 0)
{
tmp_ext[j-k]=fname[j];
}
}
#endif
#ifdef 1 ////后缀名带"."
if ( fname[j] == '.')
{
k=j;
}
if ( j >= k && k !=0 )
{
tmp_ext[j-k]=fname[j ];
}
else
{
tmp_name[j]= fname[j];
}
#endif
j++;
}
memset(ext,0,sizeof(ext));
memset(fname, 0, sizeof(fname));
strcpy(fname, tmp_name);
strcpy(ext,tmp_ext);
}
printf("drive=====%c\n",drive);
printf("dir=======%s\n",dir);
printf("fname========%s\n",fname);
printf("ext======%s\n",ext);
}
int main()
{
char tracePath[PATH_MAX]="c:/test/tmp/share/copytest.test";
char ext[PATH_MAX];
char traceFile[PATH_MAX];
char dir[PATH_MAX];
char drive[PATH_MAX];
//strcpy(tracePath,"c:/test/tmp/1234/456/name.c");
_splitpath(tracePath,drive,dir,traceFile,ext);
return 0;
}
相关文章推荐
- windows 上的 _splitpath 函数在 linux 平台下的简单实现
- 实现一个简单类似linux上cat命令的功能函数
- linux下利用系统函数实现rm -rf的功能!
- 20、在Linux中实现类似windows中获取配置文件的函数GetProfileString
- windows客户端实现类似windows系统右键新建菜单功能
- Linux下简单配置SAMBA服务,实现与Windows系统文件共享.
- linux 调用系统函数来实现 ATM 自动取款机功能 (进程间通信)
- Linux下简单Shell实现(二)基本功能---主函数及init()函数
- 教你如何实现windows与linux跨系统平台的一种简单的共享!!
- 简单实现windows系统普通或强制关机重启注销的功能
- linux 调用系统函数来实现 ATM 自动取款机功能 (进程间通信)
- 简单实现Linux/Windows系统间文件互传
- 在Linux中实现类似windows中获取配置文 件的函数GetProfileString
- ORACLE 在linux等系统中利用rlwrap实现windows上下左右代码功能
- .NET开发实现类似Web Parts功能,超简单实现
- [EXCEL]实现类似Switch case的函数功能
- C语言简单宏函数实现C++模板功能(类型本身作为函数参数)
- scp:实现网络中Linux系统下文件互相拷贝功能
- 用 scanf 系列函数实现简单的 email 地址合法性检查功能
- linux下增加一个新的系统调用实现pstree功能