17aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley/* readlink.c - Return string representation of a symbolic link. 2fece5cb6d796119eccb1ae0074e5b3aaccbb74eeRob Landley * 32896480c4918f2accccb8301bec457a7bff7377eRob Landley * Copyright 2007 Rob Landley <rob@landley.net> 42896480c4918f2accccb8301bec457a7bff7377eRob Landley 51dd3704c5ffea926f61a96bb7de7d9dbee52fa44Paul BarkerUSE_READLINK(NEWTOY(readlink, "<1>1fenq[-fe]", TOYFLAG_USR|TOYFLAG_BIN)) 655928b1e0a08d84a5cbc50020f0a8c1024f5b6ceRob Landley 72896480c4918f2accccb8301bec457a7bff7377eRob Landleyconfig READLINK 87aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley bool "readlink" 93a99aef5072b6bdc50be26c23d49d5260042e385Rob Landley default y 107aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley help 11bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley usage: readlink FILE 122896480c4918f2accccb8301bec457a7bff7377eRob Landley 13bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley With no options, show what symlink points to, return error if not symlink. 142896480c4918f2accccb8301bec457a7bff7377eRob Landley 15bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley Options for producing cannonical paths (all symlinks/./.. resolved): 167aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley 173a99aef5072b6bdc50be26c23d49d5260042e385Rob Landley -e cannonical path to existing entry (fail if missing) 183a99aef5072b6bdc50be26c23d49d5260042e385Rob Landley -f full path (fail if directory missing) 19bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley -n no trailing newline 20bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley -q quiet (no output, just error code) 212896480c4918f2accccb8301bec457a7bff7377eRob Landley*/ 225288145aa65813308c52e56714bea7728bcd4175Rob Landley 23bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley#define FOR_readlink 245288145aa65813308c52e56714bea7728bcd4175Rob Landley#include "toys.h" 255288145aa65813308c52e56714bea7728bcd4175Rob Landley 26efda21ca931766eed6cfc49d1b2122c53827d9fcRob Landleyvoid readlink_main(void) 275288145aa65813308c52e56714bea7728bcd4175Rob Landley{ 287aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley char *s; 296c437186c0f30d18d739648146399990501a823dRob Landley 307aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley // Calculating full cannonical path? 316c437186c0f30d18d739648146399990501a823dRob Landley 32fe91e68e8d1e6a2974b57a9855032ad94d137e8eRob Landley if (toys.optflags & (FLAG_f|FLAG_e)) 33fe91e68e8d1e6a2974b57a9855032ad94d137e8eRob Landley s = xabspath(*toys.optargs, toys.optflags & FLAG_e); 34fe91e68e8d1e6a2974b57a9855032ad94d137e8eRob Landley else s = xreadlink(*toys.optargs); 355288145aa65813308c52e56714bea7728bcd4175Rob Landley 367aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (s) { 37bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley if (!(toys.optflags & FLAG_q)) 38bd2e2279d2b8f491ff9a56f6bd9000b0215778f8Rob Landley xprintf((toys.optflags & FLAG_n) ? "%s" : "%s\n", s); 397aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley if (CFG_TOYBOX_FREE) free(s); 407aa651a6a4496d848f86de9b1e6b3a003256a01fRob Landley } else toys.exitval = 1; 415288145aa65813308c52e56714bea7728bcd4175Rob Landley} 42