1 /*	$NetBSD: file_id.c,v 1.1.1.1 2009/06/23 10:08:46 tron Exp $	*/
2 
3 /*++
4 /* NAME
5 /*	file_id 3
6 /* SUMMARY
7 /*	file ID printable representation
8 /* SYNOPSIS
9 /*	#include <file_id.h>
10 /*
11 /*	const char *get_file_id(fd)
12 /*	int	fd;
13 /*
14 /*	int	check_file_id(fd, id)
15 /*	int	fd;
16 /*	const char *id;
17 /* DESCRIPTION
18 /*	get_file_id() queries the operating system for the unique identifier
19 /*	for the specified file and returns a printable representation.
20 /*	The result is volatile.  Make a copy if it is to be used for any
21 /*	appreciable amount of time.
22 /*
23 /*	check_file_id() tests if an open file matches the given
24 /*	printable FILE ID representation.
25 /*
26 /*	Arguments:
27 /* .IP fd
28 /*	A valid file descriptor that is associated with an open file.
29 /* .IP id
30 /*	Printable file ID.
31 /* DIAGNOSTICS
32 /*	All errors are fatal.
33 /* LICENSE
34 /* .ad
35 /* .fi
36 /*	The Secure Mailer license must be distributed with this software.
37 /* AUTHOR(S)
38 /*	Wietse Venema
39 /*	IBM T.J. Watson Research
40 /*	P.O. Box 704
41 /*	Yorktown Heights, NY 10598, USA
42 /*--*/
43 
44 /* System library. */
45 
46 #include <sys_defs.h>
47 #include <sys/stat.h>
48 #include <string.h>
49 
50 /* Utility library */
51 
52 #include <msg.h>
53 #include <vstring.h>
54 
55 /* Global library. */
56 
57 #include "file_id.h"
58 
59 /* get_file_id - lookup file ID, convert to printable form */
60 
61 const char *get_file_id(int fd)
62 {
63     static VSTRING *result;
64     struct stat st;
65 
66     if (result == 0)
67 	result = vstring_alloc(1);
68     if (fstat(fd, &st) < 0)
69 	msg_fatal("fstat: %m");
70     vstring_sprintf(result, "%lX", (long) st.st_ino);
71     return (vstring_str(result));
72 }
73 
74 /* check_file_id - make sure file name matches ID */
75 
76 int     check_file_id(int fd, const char *name)
77 {
78     return (strcmp(get_file_id(fd), name));
79 }
80