[Toybox] [PATCH] toys/pwd: add -L and -P flags

Daniel Walter d.walter at 0x90.at
Wed Feb 22 01:21:45 PST 2012


[toys/pwd] added -L and -P flags

 * -L get path from ENV
 * -P do not show symlinks

--
regards
daniel
--
diff -r 42a322adbd17 toys/pwd.c
--- a/toys/pwd.c	Tue Feb 21 21:39:20 2012 -0600
+++ b/toys/pwd.c	Wed Feb 22 10:18:44 2012 +0100
@@ -4,11 +4,10 @@
  *
  * Copyright 2006 Rob Landley <rob at landley.net>
  *
- * See http://www.opengroup.org/onlinepubs/009695399/utilities/echo.html
+ * See http://www.opengroup.org/onlinepubs/009695399/utilities/pwd.html
  *
- * TODO: add -L -P
 
-USE_PWD(NEWTOY(pwd, NULL, TOYFLAG_BIN))
+USE_PWD(NEWTOY(pwd, "LP", TOYFLAG_BIN))
 
 config PWD
 	bool "pwd"
@@ -17,14 +16,38 @@
 	  usage: pwd
 
 	  The print working directory command prints the current directory.
+
+	  -L	use PWD from environment.
+	  -P	avoid all symlinks
 */
 
 #include "toys.h"
 
+#define	FLAG_L	(1<<1)
+#define	FLAG_P	1
+
 void pwd_main(void)
 {
-	char *pwd = xgetcwd();
+	char *pwd = NULL;
+	/* imply -P automatically, since _most_ pwd implementations
+	 * behave like this (checked on linux, FreeBSD, etc.).
+	 * NOTE: SUS says that -L should be implied
+	 */
+	int phy = 1;
+
+	/* check if logical path is wanted */
+	if (toys.optflags & FLAG_L) {
+		phy = 0;
+		pwd = getenv("PWD");
+	}
+	/* either physical path is wanted, or getenv failed
+	 * in both cases use physical path
+	 */
+	if (!pwd) {
+		pwd = xgetcwd();
+		phy = 1;
+	}
 
 	xprintf("%s\n", pwd);
-	if (CFG_TOYBOX_FREE) free(pwd);
+	if (CFG_TOYBOX_FREE && phy) free(pwd);
 }

 1329902505.0


More information about the Toybox mailing list