uuprog
diff pwd.c @ 0:4f628556fa3e
uuprog initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 25 Aug 2011 08:53:01 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/pwd.c Thu Aug 25 08:53:01 2011 +0300 1.3 @@ -0,0 +1,39 @@ 1.4 +/*! gcc -std=c89 -pedantic -Wall -o pwd pwd.c */ 1.5 +#include <stdio.h> 1.6 +#include <stdlib.h> 1.7 +#include <string.h> 1.8 +#include <errno.h> 1.9 +#include <unistd.h> 1.10 + 1.11 +int main(void) 1.12 +{ 1.13 + char *tmp, *wdbuf; 1.14 + size_t bufsz = 64; 1.15 + 1.16 + if(!(wdbuf = malloc(bufsz))) { 1.17 + perror("malloc failed"); 1.18 + return 1; 1.19 + } 1.20 + 1.21 + while(!(tmp = getcwd(wdbuf, bufsz)) && errno == ERANGE) { 1.22 + size_t newsz = bufsz ? bufsz * 2 : 2; 1.23 + 1.24 + if(!(tmp = realloc(wdbuf, newsz))) { 1.25 + fprintf(stderr, "failed to allocate: %u bytes: %s\n", (unsigned int)newsz, strerror(errno)); 1.26 + free(wdbuf); 1.27 + return 1; 1.28 + } 1.29 + wdbuf = tmp; 1.30 + bufsz = newsz; 1.31 + } 1.32 + 1.33 + if(!tmp) { 1.34 + perror("getcwd failed"); 1.35 + free(wdbuf); 1.36 + return 1; 1.37 + } 1.38 + 1.39 + puts(wdbuf); 1.40 + free(wdbuf); 1.41 + return 0; 1.42 +}