'경로에서 파일명'에 해당되는 글 1건

  1. 2010.01.26 경로명에서 디렉토리와 파일이름 분리해서 알아내기 / 얻어내기
Linux2010. 1. 26. 15:54
How seperate directory and filename from path.

getfilename()
getdirectorypath()

이런식으로 좀 구현해주면 덧나나 싶을 정도로
은근히 검색해도 안나오는 녀석이었는데, 은근히 용도가 많은 녀석이다.

#include <libgen.h>
char *dirname(char *path);
char *basename(char *path);

path          dirname        basename
"/usr/lib"     "/usr"        "lib"
"/usr/"        "/"           "usr"
"usr"          "."           "usr"
"/"            "/"           "/"
"."            "."           "."
".."           "."           ".."


[링크 : http://www.joinc.co.kr/modules/moniwiki/wiki.php/man/3/basename]

path가 절대경로이든, 상대경로이든 dirname()과 basename()은 알아서 분리해준다.

[링크 : http://linux.die.net/man/3/basename]
[링크 : http://linux.die.net/man/3/dirname]

#include <string.h>
char *strdup(const char *s);
#define _GNU_SOURCE
#include <string.h>
char *strndup(const char *s, size_t n);
char *strdupa(const char *s);
char *strndupa(const char *s, size_t n);

The strdup() function returns a pointer to a new string which is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

[링크 : http://linux.die.net/man/3/strdup]

[링크 : http://www.unix.com/unix-dummies-questions-answers/100617-how-get-directory-name-its-path.html]

오늘의 교훈 : 쉘에서 되면 당연히 API로도 존재한다!


Posted by 구차니