[C 언어] 사용자 홈 Dir (~) 알아내는 방법?

nephro의 이미지

FILE *fp;

fp = fopen("~/test.txt","w");

인 경우 파일을 만들지 못하네요.

fp = fopen("/home/user/test.txt","w"); 는 제대로 만들어 지구요.

프로그램 안에서, 사용자 홈의 절대경로를 알아내는 방법이 있습니까?

원초적인 질문같아 죄송합니다.

beta의 이미지

struct passwd *getpwnam(const char *name);

              struct passwd {
                      char    *pw_name;       /* user name */
                      char    *pw_passwd;     /* user password */
                      uid_t   pw_uid;         /* user id */
                      gid_t   pw_gid;         /* group id */
                      char    *pw_gecos;      /* real name */
                      char    *pw_dir;        /* home directory */
                      char    *pw_shell;      /* shell program */
              };

발 담갔다. 이제 익숙해 지는길만이..

gold_ya의 이미지

getpwnam()에보면은 pw_dir에서 가져오면 될꺼 같네요..

nephro의 이미지

감사합니다. 좋은 성탄절 되세요.

자유를 꿈꾸는자 고개를 들어 하늘을 보라.

nephro의 이미지

가르쳐주신 대로, getpwnam()을 이용하니 절대경로를 알아낼 수 있었습니다.

그런데, getpwnam은 계정의 ID를 알아야 하더군요.

풀그림의 사용자가 누가될지 모르기 때문에 getpwnam("계정이름") 을

사용하는데 문제가 생기겠네요.

계정 이름을 알아내야 문제가 해결될것 같은데요,

C 프로그램 안에서 현재 사용자의 계정이름을 알아낼 방법좀 가르쳐 주세요.

자유를 꿈꾸는자 고개를 들어 하늘을 보라.

정인철의 이미지

그냥

/etc/passwd 화일을 열어보시면 어떨까요?

woonuk의 이미지

getuid() or geteuid() 으로 사용자 uid 를 구하고,
getpwuid() 를 호출하면 원하는 pw_dir 을 구할수 있겠네요.

man getuid

getuid returns the real user ID of the current process.
geteuid returns the effective user ID of the current process.
preisner의 이미지

환경변수를 이용하시는게 더 편하겠네요.

getenv("HOME");

그럼..

lunarainbow의 이미지

getpwnam(getlogin());

getlogin() 로그인한 사용자의 아이디 리턴...

단, AAA 로 로긴후, su - BBB 를 할 경우, BBB로 실행하면 AAA를 리턴합니다. 이점 주의하세요 ^^
(기억이 맞다면, 맞을겁니다.)

pynoos의 이미지

nephro의 이미지

정말 감사합니다.
woonuk 님과 preisner 님의 방법으로 모두 성공하였습니다. 이제 어떤걸 쓸지 행복한 고민만 남았네요 ^^;

자유를 꿈꾸는자 고개를 들어 하늘을 보라.

nephro의 이미지

pynoos 님의 링크를 보니, 며칠전의 내용조차 검색하지 못하고 질문을 올렸군요. 죄송..
답변주신 모든분들께 감사드립니다.

자유를 꿈꾸는자 고개를 들어 하늘을 보라.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.