comp.lang.ada
 help / color / mirror / Atom feed
* Filename comparison ... HELP NEEDED!
@ 1994-12-02 11:40 Nigel Jewell
  1994-12-02 15:14 ` Frank Pilhofer
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Nigel Jewell @ 1994-12-02 11:40 UTC (permalink / raw)


Hi,

I apologise if this is irrelevant to any groups posted to, but I want as many
ideas/solutions as possible

I have a _small_ problem with an application that I am trying to write on a UNIX
platform using both C and Ada.  

Basically a user selects a file to load.  This file may point to other files.
For example:

file1:
	$HOME/test/file2
	../file3

file2:
	../file3

file3:

Each of these is loaded recursively therefore when a reference to file3 is seen
the file3 is loaded and processed in the same way.  Therefore I need to be able 
to check if a file has already been loaded.  In this case file3 from both files
1 and 2.

Here is the problem.

What if a user uses $HOME/test/file2 in one file and ~/test/file2 in another
file, I cannot check that the file hasn't already been loaded.  One
solution suggested to me is to scan the filename for environment variables and
then substitute them in the string, using $HOME for the tilde.  This is not
a perfect solution though.  What if the user uses $HOME/test/dir1/../file3 and
$HOME/test/file3 ... do I have to go through and convert every filename checking
for:

environment variables: both $var and ${var}
username specification: e.g ~bob
the use of "." and ".."
links could also cause a problem.

this is a very long winded way of doing it ... and it may cause considerable
delays the more files there are to load.  Also I can't believe there isn't an
easier way than this!

Does anybody know of an good solution to seeing if two filenames given actually
point at the same file in the file system?  (Probably a solution in C).

Any help would be REALLY REALLY appreciated.

Thanks in advance.

Nige.

PS. Replies by email would be prefered.

===============================================================================
E-mail : jewell@cci.de                               Tel. : +49 (0)5931-805-463
     Competence Center Informatik GmbH, Lohberg 10, 49716 Meppen. Germany.

**** Any views expressed may not be those shared by my employer, CCI GmbH. ****
===============================================================================





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Filename comparison ... HELP NEEDED!
  1994-12-02 11:40 Nigel Jewell
@ 1994-12-02 15:14 ` Frank Pilhofer
  1994-12-02 16:53 ` Tanmoy Bhattacharya
  1994-12-12  7:50 ` Lachlan Roche
  2 siblings, 0 replies; 7+ messages in thread
From: Frank Pilhofer @ 1994-12-02 15:14 UTC (permalink / raw)


In <3bn13h$jmo@speedy.cci.de> jewell@cci.de (Nigel Jewell) writes:
>Does anybody know of an good solution to seeing if two filenames given actually
>point at the same file in the file system?  (Probably a solution in C).

 A stat() on any file will give you a device id and an inode. These values
can be used to identify a file. If you stat() both files, and both the device
id and the inode are equal, then you have the same file. Only, this does not
work with linked files (but with symlinks).

	Frank

-- 
 + Frank Pilhofer                             fp@informatik.uni-frankfurt.de +
 | Darmstaedter Str. 22                                                      |
 | D-63225 Langen, Germany                    RAD Host (PAL/NTSC video,UNIX) |
 +---------------------------------------------------------------------------+



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Filename comparison ... HELP NEEDED!
  1994-12-02 11:40 Nigel Jewell
  1994-12-02 15:14 ` Frank Pilhofer
@ 1994-12-02 16:53 ` Tanmoy Bhattacharya
  1994-12-05  8:00   ` Nigel Jewell
  1994-12-12  7:50 ` Lachlan Roche
  2 siblings, 1 reply; 7+ messages in thread
From: Tanmoy Bhattacharya @ 1994-12-02 16:53 UTC (permalink / raw)


In article <3bn13h$jmo@speedy.cci.de>, jewell@cci.de (Nigel Jewell) writes:
|> Hi,
|> 
|> I apologise if this is irrelevant to any groups posted to, but I want as many
|> ideas/solutions as possible
|> 

You are gravely mistaken if you think your apologies are enough compensation
for wasting many people's time all over the world simply becuase you `wanted'
as many ideas/solution. Most of the net is not for whining children. Please do not
post to irrelevant newgroups. Refrain from posting if you have no idea of
netiquette.

What you asked has nothing to do with C or Ada or miscellaneous languages. It is
a UNIX question and as such is appropriate only there.

<snip>
|> Does anybody know of an good solution to seeing if two filenames given actually
|> point at the same file in the file system?  (Probably a solution in C).

Understand the difference between programming and coding. What you are asking for
is a `programming' and `binding' question, not a coding question. Language is
mostly irrelevant to this question.

In any case keeping track of files by there beginning inode numbers is much
easier than by their names. 

Cheers
Tanmoy
-- 
tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545
Other networks see <news:news.ansers?internetwork-mail-guide>or 
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Filename comparison ... HELP NEEDED!
  1994-12-02 16:53 ` Tanmoy Bhattacharya
@ 1994-12-05  8:00   ` Nigel Jewell
  0 siblings, 0 replies; 7+ messages in thread
From: Nigel Jewell @ 1994-12-05  8:00 UTC (permalink / raw)


In article eh8@newshost.lanl.gov, tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya) writes:
>You are gravely mistaken if you think your apologies are enough compensation
>for wasting many people's time all over the world simply becuase you `wanted'
>as many ideas/solution. Most of the net is not for whining children. Please do not
>post to irrelevant newgroups. Refrain from posting if you have no idea of
>netiquette.

The net is not for whining children ... yet you still feel it necessary to have a 
whine about a cross posting!?!  Surely the net is a source of information for all
those that have access to use it.  Doesn't it seem a bit ironic that the net exists
for such a purpose, but we get flamed if we try and use it as such.  I think that
you are the one that needs to cut down on the whining ...

Irrelevant newsgroups?

I have been posted solutions in C, Ada, some UNIX specific, some not, some answers
via the shell.  Maybe not as irrelevant as you wish then to be.

Nige.

"Knowledge is a dangerous thing: Here is the bufferfly, here are the wings ..."
					                            New Model Army. 





^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Filename comparison ... HELP NEEDED!
@ 1994-12-05 18:27 Bennett, Chip (KTR) ~U
  0 siblings, 0 replies; 7+ messages in thread
From: Bennett, Chip (KTR) ~U @ 1994-12-05 18:27 UTC (permalink / raw)


In comp.lang.ada you wrote

> Does anybody know of an good solution to seeing if two filenames given
actually
> point at the same file in the file system?  (Probably a solution in C).

You could use stat or lstat to get info about a file that is the same (file
system and inode number) even if the aliases for the file are different.
 Your choice of stat or lstat depends on whether you want symbolic links to
act like "different" files or not.

Also, this can be easily done in Ada using the system interface package
provided with the compiler.  If your compiler doesn't have a UNIX system
interface package (most do), the bindings to stat and lstat and the
conversion of the "C" data structure to Ada is also straight forward.

I agree with another response to this post, that it was inappropriate for
you to splatter the news groups with this question.  However, I think I
saved your good name in THIS group by pointing out the "Ada" aspects.  :-)

*****************************************************************
* Chip Bennett, GDE Systems Inc | BennettC@j64.stratcom.af.mil  *
* USSTRATCOM/J64213             | Voice (402)294-7360           *
* 901 SAC Blvd, Suite 2B24      | FAX   (402)294-7912           *
* Offutt AFB, NE 68113-6600     | Proud member of Team Ada      *
* Opinions expressed here are my own _so_,  TTFWTW              *
*****************************************************************



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Filename comparison ... HELP NEEDED!
  1994-12-02 11:40 Nigel Jewell
  1994-12-02 15:14 ` Frank Pilhofer
  1994-12-02 16:53 ` Tanmoy Bhattacharya
@ 1994-12-12  7:50 ` Lachlan Roche
  1994-12-12 15:28   ` David Emery
  2 siblings, 1 reply; 7+ messages in thread
From: Lachlan Roche @ 1994-12-12  7:50 UTC (permalink / raw)



In article <3bn13h$jmo@speedy.cci.de> jewell@cci.de (Nigel Jewell) writes:
>Does anybody know of an good solution to seeing if two filenames given actually
>point at the same file in the file system?  (Probably a solution in C).

The canonical method is inode comparison. If the inodes are the same, its
the same file.

eg.

/* do these two names refer to a single file? */
int is_alias( char *name1, char *name2 )
{
  struct stat stat1, stat1;

  stat( name1, &stat1 )
  stat( name2, &stat2 )

  return stat1.st_ino == stat2.st_ino;
}

--
Lachlan Roche	... in Brisbane, Australia ...		lr@quux.apana.org.au



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Filename comparison ... HELP NEEDED!
  1994-12-12  7:50 ` Lachlan Roche
@ 1994-12-12 15:28   ` David Emery
  0 siblings, 0 replies; 7+ messages in thread
From: David Emery @ 1994-12-12 15:28 UTC (permalink / raw)


Sorry, comparing inodes is insufficient.  Inodes are unique on a given
device.  Two files, on two separate devices, may have the same inode
number.  Thus, the C return should be 

	return ((stat1.st_ino == stat2.st_ino) 
		&& (stat1.st_dev == stat2.st_dev));

Implemented in POSIX/Ada, the same function would be

with POSIX, POSIX_File_Status;
function same_file (f1, f2 : POSIX.pathname)
    return boolean
is
  function "=" (l, r : POSIX_FIle_Status.File_ID) 
      return boolean renames POSIX_File_Status."=";
  function "=" (l, r : POSIX_FIle_Status.Device_ID) 
      return boolean renames POSIX_File_Status."=";
  status1, status2 : POSIX_File_Status.status;
begin
  status1 := POSIX_File_Status.Get_File_Status (f1);
  status2 := POSIX_File_Status.Get_File_Status (f2);
  return ((POSIX_File_Status.Device_Id_Of (status1) 
	   = POSIX_File_Status.Device_ID_Of (Status2))
          and then	-- no need to check file_ID if Device_IDs
			-- are different.
	  (POSIX_File_Status.File_Id_Of (status1) 
	   = POSIX_File_Status.File_ID_Of (Status2))
	 );
end same_file;
				dave

p.s.  Does anyone know if device numbers are 'unique' for NFS-mounted
file systems?
--
--The preceeding opinions do not necessarily reflect the opinions of
--The MITRE Corporation or its sponsors. 
-- "A good plan violently executed -NOW- is better than a perfect plan
--  next week"                                      George Patton
-- "Any damn fool can write a plan.  It's the execution that gets you
--  all screwed up"                              James Hollingsworth
-------------------------------------------------------------------------



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1994-12-12 15:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1994-12-05 18:27 Filename comparison ... HELP NEEDED! Bennett, Chip (KTR) ~U
  -- strict thread matches above, loose matches on Subject: below --
1994-12-02 11:40 Nigel Jewell
1994-12-02 15:14 ` Frank Pilhofer
1994-12-02 16:53 ` Tanmoy Bhattacharya
1994-12-05  8:00   ` Nigel Jewell
1994-12-12  7:50 ` Lachlan Roche
1994-12-12 15:28   ` David Emery

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox