[comp.sources.misc] v20i045: ftok - ftok

Mathew Kimmel <kimmel@umvlsi.ecs.umass.edu> (06/08/91)

Submitted-by: Mathew Kimmel <kimmel@umvlsi.ecs.umass.edu>
Posting-number: Volume 20, Issue 45
Archive-name: ftok/part01
Environment: Coherent

This is a clone of the ftok() function used with interprocess
communications functions.  This is a standard function on most unixes,
but Coherent lacks it, so I wrote my own version.  Basically, what it
does is return a unique key to be used with the {msg,sem,shm}get
functions, based on a filename and an integer.  For details about the
algorithm and usage, see the man page and comments at the beginning of
ftok.c

-Matt
---cut here---
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  ftok.c ftok.man
# Wrapped by kimmel@umvlsi.ecs.umass.edu on Fri Jun  7 01:44:52 1991
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f ftok.c -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ftok.c\"
else
echo shar: Extracting \"ftok.c\" \(1216 characters\)
sed "s/^X//" >ftok.c <<'END_OF_ftok.c'
X/* key_t ftok(filename,c) - Create a unique IPC key based on a filename
X *                          and an 8-bit number.
X *
X * This function takes as parameters a pointer to an ascii string
X * containing the pathname of a file, and an integer.  It then returns
X * a (hopefully) unique IPC key.  The key is a 32-bit integer, and is
X * constructed as follows: the lower 8 bits are the low 8 bits of c.
X * The next 8 bits are the low 8 bits of the device descriptor of the
X * device the file is located on.  The upper 16 bits are the inode
X * of the file.
X *
X * This code copyright (c) Matt Kimmel 1991.  Permission granted for
X * unrestricted use in non-commercial products.
X */
X#include <sys/ipc.h>
X#include <sys/stat.h>
X
Xkey_t ftok(filename,c)
Xchar *filename;
Xint c;
X{
X  struct stat fs;
X  union {
X    key_t key;
X    struct {
X      char c;
X      char dev;
X      int inode;
X      } info;
X    } keyval;
X
X  /* First attempt to stat the file */
X  if(stat(filename,&fs) == -1) {
X    perror("ftok");
X    exit(1); /* Best to exit if this happens, or we may have a major IPC collision... */
X    }
X
X  keyval.info.c = (char)c;
X  keyval.info.dev = (char)fs.st_dev;
X  keyval.info.inode = (int)fs.st_ino;
X  return(keyval.key);
X}
X
END_OF_ftok.c
if test 1216 -ne `wc -c <ftok.c`; then
    echo shar: \"ftok.c\" unpacked with wrong size!
fi
# end of overwriting check
fi
if test -f ftok.man -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"ftok.man\"
else
echo shar: Extracting \"ftok.man\" \(496 characters\)
sed "s/^X//" >ftok.man <<'END_OF_ftok.man'
X.TH ftok
X.SH USAGE
X.PP
Xkey_t ftok(pathname,c);
Xchar *pathname;
Xint c;
X.SH SYNOPSIS
X.PP
Xftok() takes as parameters the pathname of
Xan existing file or directory, and a number.
XFrom these, it assembles a (hopefully) unique
Xkey to be used with the interprocess communcation
Xsystem calls (specifically, msgget(), semget() and
Xshmget()).  The method used for generating this
Xkey is complex; note, however, that only the lower
X8 bits of c are used.
X.SH SEE ALSO
X.PP
Xipc.h, msgget(), semget(), shmget()
END_OF_ftok.man
if test 496 -ne `wc -c <ftok.man`; then
    echo shar: \"ftok.man\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0

exit 0 # Just in case...
-- 
Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
Sterling Software, IMD           UUCP:     uunet!sparky!kent
Phone:    (402) 291-8300         FAX:      (402) 291-4362
Please send comp.sources.misc-related mail to kent@uunet.uu.net.