Skip to content

Instantly share code, notes, and snippets.

@akkijp
Last active October 20, 2015 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akkijp/2562dfa24b26539d642f to your computer and use it in GitHub Desktop.
Save akkijp/2562dfa24b26539d642f to your computer and use it in GitHub Desktop.
カレントワーキングディレクトリを表示する
#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