/* * (C) Curaga * An utility for fast symlinking in tce-load for tcz's * Under the GPLv2 * * v1.2 * General cleanup * Removal support * Fixed a bug in --force (had a wrong pointer in there since 1.1) * * v1.1.1 * Remove the user.tar.gz symlink * * v1.1 * Allocate space from the heap instead of from the stack * (our 12kb could have blown the default 8kb) * No more _GNU_SOURCE (a relic from earlier) * Performance & ram usage optimizations, thanks ^hats^ * Directory support * */ #include #include #include #include #include #include #include #define strEqual(a,b) (0 == strcmp(a,b)) #define TWELVE 12 #define QUITIT(a) free(dest); return a int main(int argc, char **argv){ char *dest, *appname, *myptr; FILE *input; size_t len,appnamelen; appname = getenv("APPNAME"); if (appname==NULL) { puts("This utility is meant to be used from inside tce-load"); return 1; } printf("App %s\n",appname); appnamelen=strlen(appname); if ( (dest=calloc(PATH_MAX,sizeof(char)) )==NULL ){ puts("Not enough RAM!"); return 1; } if ( argc>1 && strEqual(argv[1],"--dir") ) { puts("Creating directories..."); if ( (input=popen("find /tmp/tcloop/${APPNAME} -type d","r"))==NULL) { QUITIT(1); } while ( fgets(dest,PATH_MAX,input) != NULL ){ len=strlen(dest)-1; if( dest[len] == '\n' ) dest[len]='\0'; myptr=&dest[(TWELVE+appnamelen)]; printf("Mkdir %s\n", myptr); mkdir(myptr, (mode_t) (S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)); // That permissions beast above is the "portable" way // of saying 0755. } pclose(input); QUITIT(0); } if ( (input=popen("find /tmp/tcloop/${APPNAME} -not -type d","r"))==NULL) { QUITIT(1); } if ( argc>1 && strEqual(argv[1],"--force") ) { puts("Forcing"); while ( fgets(dest,PATH_MAX,input) != NULL ){ len=strlen(dest)-1; if( dest[len] == '\n' ) dest[len]='\0'; myptr=&dest[(TWELVE+appnamelen)]; printf("Symlinking %s to %s\n",dest,myptr); remove(myptr); symlink(dest,myptr); } remove("/user.tar.gz"); pclose(input); QUITIT(0); } if ( argc>1 && strEqual(argv[1],"--remove") ) { puts("Removing..."); while ( fgets(dest,PATH_MAX,input) != NULL ){ len=strlen(dest)-1; if( dest[len] == '\n' ) dest[len]='\0'; myptr=&dest[(TWELVE+appnamelen)]; unlink(myptr); } pclose(input); QUITIT(0); } while ( fgets(dest,PATH_MAX,input) != NULL ){ len=strlen(dest)-1; if( dest[len] == '\n' ) dest[len]='\0'; myptr=&dest[(TWELVE+appnamelen)]; printf("Symlinking %s to %s\n",dest,myptr); symlink(dest,myptr); } remove("/user.tar.gz"); pclose(input); QUITIT(0); }