[EMAIL PROTECTED] (Lars Gullik Bj?nnes) writes:

> Anyway to make a func in FileTools seems like a good option for 1.0.x.

OK, I did this. 

> What should be done now is to make it safe and perhaps move the
>  if((isLink = fileInfo.isLink()))
> stuff into FileTools.C
>
> [...]
>
> In 1.1.x we should rewrite this to use LString.

I am afraid I found it easier to just convert everything to LString now
than to try to add up all the buffer sizes. The Code is much simpler
and cleaner in this way.

Here is the complete patch against vanilla 1.0.0. Do you guys do
ChangeLogs?

1999-02-09  Jan Vroonhof  <[EMAIL PROTECTED]>

        * lyx_main.C (init): Possibly chase symlinks if the binary path is 
        a symlink to a symlink to a ... to the real executable.

        * filedlg.C (Reread): Also clean up use of fixed char[]
        buffers and use LString to avoid buffer overruns. 
        (Reread): Rename sz* types that are no longer char[].

        * filetools.C (LyXReadLink): New function

        * filedlg.C (Reread): Use it. 


Only in lyx-1.0.0.jv/src: Makefile
Only in lyx-1.0.0.jv/src: config.h
diff -ur -x *.o -x *~ lyx-1.0.0/src/filedlg.C lyx-1.0.0.jv/src/filedlg.C
--- lyx-1.0.0/src/filedlg.C	Mon Oct 26 23:17:47 1998
+++ lyx-1.0.0.jv/src/filedlg.C	Tue Feb  9 20:49:07 1999
@@ -215,8 +215,8 @@
 	int i;
 	DIR *pDirectory;
 	struct dirent *pDirEntry;
-	LString szFile;
-	char szBuffer[256], szMode[15], szTime[40];
+	LString File, Buffer;	
+	char szMode[15], szTime[40];
 	FileInfo fileInfo;
 	
 	// Opens directory
@@ -242,14 +242,14 @@
 	// Splits complete directory name into directories and compute depth
 	iDepth = 0;
 	LString line, Temp;
-	szFile = pszDirectory;
-	if (szFile != "/") {
-		szFile.split(Temp, '/');
+	File = pszDirectory;
+	if (File != "/") {
+		File.split(Temp, '/');
 	}
-	while (!szFile.empty() || !Temp.empty()) {
+	while (!File.empty() || !Temp.empty()) {
 		LString dline = "@b"+line + Temp + '/';		
 		fl_add_browser_line(pFileDlgForm->List, dline.c_str());
-		szFile.split(Temp, '/');
+		File.split(Temp, '/');
 		line += ' ';
 		iDepth++;
 	}
@@ -281,9 +281,9 @@
 			continue;
 
 		// gets file status
-		szFile = AddName(pszDirectory, fname);
+		File = AddName(pszDirectory, fname);
 
-		fileInfo.newFile(szFile, true);
+		fileInfo.newFile(File, true);
 
 		fileInfo.modeString(szMode);
 		unsigned int nlink = fileInfo.getNumberOfLinks();
@@ -305,36 +305,33 @@
 		} else
 			szTime[16] = 0;
 		
-		sprintf(szBuffer, "%s %u %s %s %s ", szMode, 
+		char szHeadBuf[128];		  
+		sprintf(szHeadBuf, "%s %u %s %s %s ", szMode, 
 			nlink,
 			user.c_str(),
 			group.c_str(),
-			szTime + 4);
+			szTime + 4);		
+		Buffer = szHeadBuf;
 
-		strcat(szBuffer, pDirEntry->d_name);
-		strcat(szBuffer, fileInfo.typeIndicator());
+		Buffer += pDirEntry->d_name;
+		Buffer += fileInfo.typeIndicator();
 
 		if ((isLink = fileInfo.isLink())) {
-			char *pszLinkBuffer;
-			char szTempLink[512];
-			int nRead;
-
-			fileInfo.newFile(szFile);
-			
-			nRead = readlink(szFile.c_str(), 
-					 szTempLink, sizeof(szTempLink));
-			pszLinkBuffer = new char[nRead + 10];
-			strcat (szBuffer, " -> ");
-
-			if (nRead > 0) strncpy(pszLinkBuffer, 
-					       szTempLink, nRead);
-			pszLinkBuffer[nRead] = 0;
-
-			strcat(pszLinkBuffer, fileInfo.typeIndicator());
-			strcat(szBuffer, pszLinkBuffer);
-			
-			// frees link buffer
-			delete [] pszLinkBuffer;
+		  LString Link;
+
+		  if (LyXReadLink(File,Link)) {
+		       Buffer += " -> ";
+		       Buffer += Link;
+
+		       // This gives the FileType of the file that
+		       // is really pointed too after resolving all
+		       // symlinks. This is not necessarily the same
+		       // as the type of Link (which could again be a
+		       // link). Is that intended?
+		       //                              JV 199902
+		       fileInfo.newFile(File);
+		       Buffer += fileInfo.typeIndicator();
+		  }
 		}
 
 		// filters files according to pattern and type
@@ -346,8 +343,9 @@
 				continue;
 		} else if (!(isDir = fileInfo.isDir()))
 			continue;
-		
-		pCurrentNames[iNumNames].pszLsEntry = szBuffer;
+
+		// Note pszLsEntry is an LString!
+		pCurrentNames[iNumNames].pszLsEntry = Buffer;
 
 		// creates used name
 		LString temp = fname;
diff -ur -x *.o -x *~ lyx-1.0.0/src/filedlg.h lyx-1.0.0.jv/src/filedlg.h
--- lyx-1.0.0/src/filedlg.h	Mon Oct 26 23:17:47 1998
+++ lyx-1.0.0.jv/src/filedlg.h	Tue Feb  9 19:57:00 1999
@@ -80,7 +80,7 @@
 class LyXDirEntry
 {
 private:
-	friend class LyXFileDlg;
+	friend class LyXFileDlg;    
 	LString pszName;
 	LString pszDisplayed;
 	LString pszLsEntry;
diff -ur -x *.o -x *~ lyx-1.0.0/src/filetools.C lyx-1.0.0.jv/src/filetools.C
--- lyx-1.0.0/src/filetools.C	Mon Oct 26 23:17:49 1998
+++ lyx-1.0.0.jv/src/filetools.C	Tue Feb  9 20:50:24 1999
@@ -955,3 +955,16 @@
 	}
 	return prefix + relhome;
 }
+
+bool LyXReadLink(LString const & File, LString & Link)
+{
+	char LinkBuffer[512];
+                // Should be PATH_MAX but that needs autconf support
+	int nRead;
+	nRead = readlink(File.c_str(), LinkBuffer,sizeof(LinkBuffer)-1);
+	if (nRead <= 0)
+		return false;
+	LinkBuffer[nRead] = 0;
+	Link = LinkBuffer;
+	return true;
+}
diff -ur -x *.o -x *~ lyx-1.0.0/src/filetools.h lyx-1.0.0.jv/src/filetools.h
--- lyx-1.0.0/src/filetools.h	Wed Dec  9 02:50:08 1998
+++ lyx-1.0.0.jv/src/filetools.h	Tue Feb  9 19:57:31 1999
@@ -287,4 +287,8 @@
 */
 LString ReplaceEnvironmentPath(LString const &path);
 
+/* Set Link to the path File Points to as a symbolic link.
+   Return True if succesfull, False other wise */
+bool LyXReadLink(LString const & file, LString & Link);
+
 #endif
Only in lyx-1.0.0.jv/src: lyx
diff -ur -x *.o -x *~ lyx-1.0.0/src/lyx_main.C lyx-1.0.0.jv/src/lyx_main.C
--- lyx-1.0.0/src/lyx_main.C	Fri Nov 27 09:06:49 1998
+++ lyx-1.0.0.jv/src/lyx_main.C	Tue Feb  9 19:58:34 1999
@@ -154,7 +154,7 @@
 	// Determine path of binary
 	//
 
-	LString binpath = argv[0];
+	LString fullbinpath, binpath = argv[0];
 	binpath.subst('\\','/');
 	LString binname = OnlyFilename(argv[0]);
 	// Sorry for system specific code. (SMiyata)
@@ -167,7 +167,8 @@
 		binsearchpath += ";."; // This will make "src/lyx" work always :-)
 		binpath = FileOpenSearch(binsearchpath, argv[0]);
 	}
-	
+
+	fullbinpath = binpath;
 	binpath = MakeAbsPath(OnlyPath(binpath));
 
 	if (binpath.empty()) {
@@ -185,6 +186,8 @@
 	// 2) LYX_DIR_10x environment variable
 	// 3) Maybe <path of binary>/TOP_SRCDIR/lib
 	// 4) <path of binary>/../share/<name of binary>/
+	// 4a) repeat 4 after following the Symlink if <path of
+	//     binary> is a symbolic link.
 	// 5) hardcoded lyx_dir
 	// The directory is checked for the presence of the file
 	// "chkconfig.ltx", and if that is present, the directory
@@ -215,10 +218,28 @@
 			      Error::INIT);
 		build_lyxdir.clean();
 	}
-	  
-	// Path of binary/../share/name of binary/
-	searchpath += NormalizePath(binpath + "../share/" + 
+
+
+        bool FollowLink;
+	do {
+	  // Path of binary/../share/name of binary/
+		searchpath += NormalizePath(binpath + "../share/" + 
 		      OnlyFilename(binname)) + ';';
+
+	  // Follow Symlinks
+		FileInfo file(fullbinpath,true);
+		FollowLink = file.isLink();
+		if (FollowLink) {
+			LString Link;
+			if (LyXReadLink(fullbinpath,Link)) {
+				fullbinpath = Link;
+				binpath = MakeAbsPath(OnlyPath(fullbinpath));
+			}
+			else {
+				FollowLink = false;
+			}
+		}
+	} while (FollowLink);
 
 	// Hardcoded dir
 	searchpath += LYX_DIR;
Only in lyx-1.0.0.jv/src: lyx_main.C.orig


Reply via email to