1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __USE_BSD
#include <unistd.h>
#include <errno.h>
#define FALSE 0
#define TRUE 1
int double_buffers( size_t s, char** b1, char** b2, char** b3, char** b4 ) {
char* n;
n = realloc( *b1, s*2 );
if ( n == NULL ) return FALSE;
else *b1 = n;
n = realloc( *b2, s*2 );
if ( n == NULL ) return FALSE;
else *b2 = n;
n = realloc( *b3, s*2 );
if ( n == NULL ) return FALSE;
else *b3 = n;
n = realloc( *b4, s*2 );
if ( n == NULL ) return FALSE;
else *b4 = n;
return TRUE;
}
int main(void) {
size_t sz_buffs = 1000;
char *buffer = malloc(sz_buffs); /* user's input */
char *cwd = malloc(sz_buffs); /* expanded dirname */
char *basename = malloc(sz_buffs); /* basename */
char *last = malloc(sz_buffs); /* directory the link was in */
char *pch;
if ( buffer == NULL ) return EXIT_FAILURE;
if ( cwd == NULL ) return EXIT_FAILURE;
if ( basename == NULL ) return EXIT_FAILURE;
for(;;) {
if ( fgets(buffer, sz_buffs, stdin) == NULL ) break;
for ( pch = buffer + strlen(buffer) - 1;
*pch != '\n' && pch == buffer + sz_buffs - 2;
pch += strlen(pch) - 1 )
{
assert( pch[1] == '\0' );
if ( double_buffers( sz_buffs, &buffer, &cwd, &basename, &last )) {
sz_buffs *= 2;
} else {
break;
}
pch = buffer + strlen(buffer) - 1;
if ( fgets(pch+1, sz_buffs/2+1, stdin) == NULL ) break;
}
assert( pch == buffer + strlen(buffer) - 1 );
if ( *pch == '\n' ) *pch = '\0';
for ( ; pch > buffer && *pch != '/'; pch-- )
;
if ( buffer[0] != '/' ) { /* don't even try relative paths */
if (printf("%s\n", buffer) < 0)
return EXIT_FAILURE;
continue;
}
strncpy( last, buffer, pch - buffer + 1 );
last[pch-buffer+1] = '\0';
for(;;) {
int numchars;
/* basename == link */
numchars = readlink( buffer, basename, sz_buffs - 1 );
if ( numchars != -1 && (unsigned)numchars < sz_buffs - 1 ) {
chdir(last);
basename[numchars] = 0;
strcpy(buffer,basename);
break;
}
if ( (unsigned)numchars == sz_buffs - 1 || errno == ENAMETOOLONG ) {
if (double_buffers( sz_buffs, &buffer, &cwd,
&basename, &last )) {
sz_buffs *= 2;
} else {
fprintf( stderr, "Error. Out of memory." );
exit(EXIT_FAILURE);
}
} else {
break;
}
}
for ( pch = buffer+strlen(buffer);
pch > buffer && *pch != '/';
pch-- )
;
if ( *pch != '/' ) { /* then buffer = "xyzzy", w/o a dir */
strcpy( basename, buffer );
strcpy( buffer, "./" );
} else {
strcpy( basename, pch + 1 );
*(pch+1) = '\0';
}
if ( 0 == chdir( buffer ) ) {
while ( NULL == getcwd( cwd, sz_buffs ) ) {
if ( double_buffers( sz_buffs, &buffer, &cwd,
&basename, &last ) )
{
sz_buffs *= 2;
} else {
break;
}
}
pch = cwd + strlen(cwd) - 1;
if ( *pch != '/' ) {
pch[1] = '/'; pch[2] = '\0';
}
} else {
if ( buffer[0] == '/' ) {
strcpy( cwd, buffer );
} else {
strcpy( cwd, last );
strcat( cwd, buffer ); /* may overflow XXXXX */
}
pch = cwd + strlen(cwd) - 1;
if ( *pch != '/' ) {
pch[1] = '/'; pch[2] = '\0';
}
}
if (printf("%s%s\n", cwd, basename) < 0)
return EXIT_FAILURE;
}
if (fclose(stdout) != 0)
return EXIT_FAILURE;
free(cwd);
free(buffer);
free(basename);
return 0;
}
|