home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / Em / OID.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-17  |  3.0 KB  |  134 lines

  1.  
  2. /*
  3.  * @(#)OID.c    1.2  2/23/90
  4.  */
  5.  
  6.  
  7. /* Generation of OIDs */
  8. #include <stdio.h>
  9. #include "Kernel/h/system.h"
  10. #include "Kernel/h/mmTypes.h"
  11. #include "Kernel/h/emTypes.h"
  12. #include "Kernel/h/kmdTypes.h"
  13.  
  14. extern char *sys_errlist[];
  15.  
  16. #ifndef NODISK
  17. extern char NODEDIR[];
  18. extern char nodisk;
  19. #endif
  20.  
  21. /* The compiler has reserved OIDs 0xff000000 - 0xffffffff.
  22.  * And for Cheating Created obj   0xfe000000 - 0xfeffffff.
  23.  * 
  24.  * The kernel has reserved OIDs from 0 to 1023 for special purposes,
  25.  * 0     is NULL.
  26.  * 1-1023 so far unassigned.
  27.  * LNN<<24 + 0 is the nodeOID for node LNN.
  28.  */
  29.  
  30. #define WRITEFREQ 256
  31.  
  32. static OID nextOID, nextNonReservedOID;
  33.  
  34. #ifdef NODISK
  35. #else
  36. FILE    *cpfp;
  37. FILE    *oidfp;
  38. #define OIDFILENAME "OID"
  39. char    OIDFileName[200];
  40. extern  int errno;
  41. #endif
  42.  
  43. /* Do the fsync, but only if DOFSYNC set */
  44. #define DOFSYNC     0
  45.  
  46. #ifndef NODISK
  47. /*ARGSUSED*/
  48. #endif
  49. void updateOIDFile(fNextNonReservedOID)
  50. OID                fNextNonReservedOID;
  51. {
  52. #ifndef NODISK
  53.   if(!nodisk){
  54.     check(fseek(oidfp, (long) 0, 0));
  55.     fprintf(oidfp, "%08x\n", fNextNonReservedOID);
  56.     if (!DOFSYNC) return;
  57.     (void) fflush(oidfp);
  58.     if ((fsync(fileno(oidfp)) < 0) ) {
  59.       ErrMsg("updateOIDFile: fsync returned %s for file %s\n",
  60.          sys_errlist[errno], OIDFileName);
  61.     }
  62.   }
  63. #endif
  64. }
  65.  
  66. OID getNextOID()
  67. {
  68.   if ( nextOID >= nextNonReservedOID ){
  69.     nextNonReservedOID += WRITEFREQ;
  70.     updateOIDFile(nextNonReservedOID);
  71.   }
  72.   return (nextOID++);
  73. }
  74.  
  75. /* Defines the next OID to use */
  76. void SetNextOID(fNextOID)
  77. OID fNextOID;
  78.   nextOID = fNextOID;
  79.   updateOIDFile(nextOID+WRITEFREQ);
  80. }
  81.  
  82. /* Initialize the OID allocation stuff */
  83. void OIDInit()
  84. {
  85. #ifndef NODISK
  86.   if(nodisk){
  87. #endif
  88.     nextOID = (OID) (GetLNN() << 24) | 0x1;
  89.     nextNonReservedOID = nextOID + WRITEFREQ;
  90.     DebugMsg(2, "No OID file, starting OID allocation at: 0x%05x\n", nextOID);
  91. #ifndef NODISK
  92.   } else {
  93.     (void) sprintf(OIDFileName, "%s/%s", NODEDIR, OIDFILENAME);
  94.     if ( (oidfp = fopen(OIDFileName, "r+")) == (FILE *) NULL) {
  95.       /* No OID file */
  96.       nextOID = (OID) (GetLNN() << 24) | 0x1;
  97.       nextNonReservedOID = nextOID + WRITEFREQ;
  98.       DebugMsg(2, "No OID file, starting OID allocation at: 0x%05x\n", nextOID);
  99.       if ( (oidfp = fopen(OIDFileName, "w+")) == (FILE *) NULL) {
  100.     ErrMsg("Cannot create OID file: %s\n", OIDFileName);
  101.       } else {
  102.     updateOIDFile(nextNonReservedOID);
  103.       }
  104.     } else {
  105.       /* Read in the nextOID */
  106.       if(fscanf(oidfp, "%x", &nextOID) != 1) {
  107.     ErrMsg("Could not read next OID properly from OID file\n");
  108.     nextOID = (OID) (GetLNN() << 24) | 0x1;
  109.       }
  110.       DebugMsg(2, "Next OID to assign: 0x%08x\n", ++nextOID);
  111.       nextNonReservedOID = nextOID+WRITEFREQ;
  112.       updateOIDFile(nextNonReservedOID);
  113.     }
  114.   }
  115. #endif
  116. }
  117.  
  118. /* Set the next NonReserved OID to the nextOID since we do not need
  119.  * any more of them.
  120.  */
  121. void shutdownOID()
  122. {
  123.   updateOIDFile(++nextOID);
  124. #ifdef BSD
  125.   (void) fflush(oidfp);
  126.   if ((fsync(fileno(oidfp)) < 0) ) {
  127.     ErrMsg("OIDInit: fsync returned %s for file %s\n",
  128.        sys_errlist[errno], OIDFileName);
  129.   }
  130.   (void) fclose(oidfp);
  131. #endif
  132. }
  133.