Last active
October 20, 2015 01:48
-
-
Save akkijp/2562dfa24b26539d642f to your computer and use it in GitHub Desktop.
カレントワーキングディレクトリを表示する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <errno.h> | |
#define INIT_BUFSIZE 1024 | |
/* | |
* return "/home/k4zzk/Desktop" | |
*/ | |
char* m_getcwd(void){ | |
char *buf, *tmp; | |
size_t size = INIT_BUFSIZE; | |
buf = malloc(size); | |
if(!buf) return NULL; | |
for(;;){ | |
errno = 0; | |
if(getcwd(buf, size)) return buf; | |
if(errno != ERANGE) break; | |
size *= 2; | |
tmp = realloc(buf, size); | |
if (!tmp) break; | |
buf = tmp; | |
} | |
free(buf); | |
return NULL; | |
} | |
int main(void){ | |
char *cwd; | |
cwd = m_getcwd(); | |
printf("%s\n", cwd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment