home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume20 / ftok / part01 / ftok.c next >
Encoding:
C/C++ Source or Header  |  1991-06-08  |  1.2 KB  |  44 lines

  1. /* key_t ftok(filename,c) - Create a unique IPC key based on a filename
  2.  *                          and an 8-bit number.
  3.  *
  4.  * This function takes as parameters a pointer to an ascii string
  5.  * containing the pathname of a file, and an integer.  It then returns
  6.  * a (hopefully) unique IPC key.  The key is a 32-bit integer, and is
  7.  * constructed as follows: the lower 8 bits are the low 8 bits of c.
  8.  * The next 8 bits are the low 8 bits of the device descriptor of the
  9.  * device the file is located on.  The upper 16 bits are the inode
  10.  * of the file.
  11.  *
  12.  * This code copyright (c) Matt Kimmel 1991.  Permission granted for
  13.  * unrestricted use in non-commercial products.
  14.  */
  15. #include <sys/ipc.h>
  16. #include <sys/stat.h>
  17.  
  18. key_t ftok(filename,c)
  19. char *filename;
  20. int c;
  21. {
  22.   struct stat fs;
  23.   union {
  24.     key_t key;
  25.     struct {
  26.       char c;
  27.       char dev;
  28.       int inode;
  29.       } info;
  30.     } keyval;
  31.  
  32.   /* First attempt to stat the file */
  33.   if(stat(filename,&fs) == -1) {
  34.     perror("ftok");
  35.     exit(1); /* Best to exit if this happens, or we may have a major IPC collision... */
  36.     }
  37.  
  38.   keyval.info.c = (char)c;
  39.   keyval.info.dev = (char)fs.st_dev;
  40.   keyval.info.inode = (int)fs.st_ino;
  41.   return(keyval.key);
  42. }
  43.  
  44.