Newsgroups: comp.sources.misc Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!uunet!sparky!kent From: Mathew Kimmel Subject: v20i045: ftok - ftok() clone for Coherent, Part01/01 Message-ID: <1991Jun7.182157.12796@sparky.IMD.Sterling.COM> X-Md4-Signature: d7780d390f6d99715394e0694f57b2ff Sender: kent@sparky.IMD.Sterling.COM (Kent Landfield) Organization: Sterling Software, IMD Date: Fri, 7 Jun 1991 18:21:57 GMT Approved: kent@sparky.imd.sterling.com Lines: 118 Submitted-by: Mathew Kimmel 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 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 X#include 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.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