| 
									
										
										
										
											2011-05-23 20:15:19 +01:00
										 |  |  | /* See LICENSE file for copyright and license details. */ | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2014-11-13 18:29:30 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-23 20:15:19 +01:00
										 |  |  | #include "util.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | static const char *getpwd(const char *cwd); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | static void | 
					
						
							|  |  |  | usage(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	eprintf("usage: %s [-LP]\n", argv0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-23 20:15:19 +01:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | main(int argc, char *argv[]) | 
					
						
							| 
									
										
										
										
											2011-05-23 20:15:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 	char *cwd; | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | 	char mode = 'L'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 	ARGBEGIN { | 
					
						
							|  |  |  | 	case 'L': | 
					
						
							|  |  |  | 	case 'P': | 
					
						
							|  |  |  | 		mode = ARGC(); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		usage(); | 
					
						
							|  |  |  | 	} ARGEND; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | 	cwd = agetcwd(); | 
					
						
							|  |  |  | 	puts((mode == 'L') ? getpwd(cwd) : cwd); | 
					
						
							| 
									
										
										
										
											2013-06-14 20:20:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-02 23:46:04 +01:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2011-05-23 20:15:19 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-01 14:59:47 +02:00
										 |  |  | static const char * | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | getpwd(const char *cwd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	const char *pwd; | 
					
						
							|  |  |  | 	struct stat cst, pst; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-19 20:59:37 +01:00
										 |  |  | 	if (!(pwd = getenv("PWD")) || pwd[0] != '/' || stat(pwd, &pst) < 0) | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | 		return cwd; | 
					
						
							| 
									
										
										
										
											2014-11-19 20:59:37 +01:00
										 |  |  | 	if (stat(cwd, &cst) < 0) | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | 		eprintf("stat %s:", cwd); | 
					
						
							| 
									
										
										
										
											2014-11-13 18:29:30 +01:00
										 |  |  | 	if (pst.st_dev == cst.st_dev && pst.st_ino == cst.st_ino) | 
					
						
							| 
									
										
										
										
											2011-05-28 15:37:42 +01:00
										 |  |  | 		return pwd; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return cwd; | 
					
						
							|  |  |  | } |