home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / ExtendedApp.m < prev    next >
Encoding:
Text File  |  1994-03-06  |  4.1 KB  |  125 lines

  1. //
  2. //    ExtendedApp.m -- a class to make extra information available via the
  3. //        application object.  You no longer need to link against ni_s.
  4. //        Written by Don Yacktman (c) 1993, 1994 by Don Yacktman.
  5. //                Version 1.2  All rights reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //        This is a free object!  Contact the author for the latest version.
  10. //        Don Yacktman, 4279 N. Ivy Lane, Provo, UT, 84604
  11. //        e-mail:  Don_Yacktman@byu.edu
  12. //
  13. //    This object is included in the MiscKit by permission from the author
  14. //    and its use is governed by the MiscKit license, found in the file
  15. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  16. //    for a list of all applicable permissions and restrictions.
  17. //    
  18.  
  19. #import <misckit/misckit.h>
  20. #import <mach/mach_error.h>
  21. #import <strings.h>
  22. #import <bsd/pwd.h>        // for getpwuid
  23. #import <libc.h>        // for chdir, getwd, getuid
  24. #include <sys/types.h>
  25. #include <bsd/grp.h>
  26.  
  27. int MiscOSVersion() { // looks at kernel version string to get OS sub version
  28.     // we could return 3.x as a float, but I am assuming version 3 or greater
  29.     // since really the only info I need is if 3.1 additions are available
  30.     // or not.  (GameKit needs to know which SoundKit stuff it can use...)
  31.     kern_return_t       ret;
  32.     kernel_version_t    string;
  33.     id versString, temp;
  34.  
  35.     ret=host_kernel_version(host_self(), string);
  36.     if (ret != KERN_SUCCESS) {
  37.         mach_error("host_kernel_version() call failed", ret);
  38.         return(-1);
  39.     }
  40.     // use a MiscString to parse it; this is probably not the best way
  41.     // to handle this since it expects the 3rd word to be "3.x:"
  42.     // (but x can be any size, at least...)  the dependency is that
  43.     // the version must be the 3rd word, and the last char should
  44.     // be tossed.
  45.     temp = [MiscString newWithString:string];
  46.     versString = [temp wordNum:3]; [temp free];
  47.     [versString removeFrom:([versString length] - 1) length:1];
  48.     temp = [versString midFrom:2 length:([versString length] - 2)];
  49.     ret = atoi([temp stringValue]); [temp free];
  50.     return ret;
  51. }
  52.  
  53. @implementation ExtendedApp
  54.  
  55. // There's nothing left; it's all in a category now.
  56.  
  57. @end
  58.  
  59. @implementation Application(MiscAppExtensions)
  60.  
  61. - (const char *)appDirectory
  62. {
  63.     return [(NXBundle *)[NXBundle mainBundle] directory];
  64. }
  65.  
  66. - (int)osVersion { return MiscOSVersion(); }
  67. - (int)userIDNum { return getuid(); }
  68. - (int)groupIDNum { return getgid(); }
  69. - (int)effectiveUserIDNum { return geteuid(); }
  70. - (int)effectiveGroupIDNum { return getegid(); }
  71. - (unsigned long int)hostID { return gethostid(); }
  72. - (NXAtom)userHomeDirectory { return NXHomeDirectory(); }
  73. - (NXAtom)userLoginName { return NXUniqueString(NXUserName()); }
  74. - (NXAtom)userRealName { return [self realNameFor:[self userLoginName]]; }
  75.  
  76. - (NXAtom)realHostName {
  77.     char realHostName[MAXHOSTNAMELEN];    // host running the application
  78.     gethostname(realHostName, MAXHOSTNAMELEN);
  79.     return NXUniqueString(realHostName);
  80. }
  81.  
  82. // Attempt to get the real name corresponding to user id.
  83. // Thanks to shayman@objectario.com (Steve Hayman) for recommending
  84. // using this over a NetInfo search.  This is much simpler...and
  85. // I had forgotten that this function actually does the right thing
  86. // for NetInfo, no NetInfo, YP, etc!  Much better solution...
  87. - (NXAtom)realNameFor:(NXAtom)userId
  88. {
  89.     struct passwd *user = getpwnam(userId); // get the passwd file entry
  90.     if (!user) return userId; // fall back on uid if error
  91.     return NXUniqueString(user->pw_gecos);    // unique full name and return it
  92. }
  93.  
  94. // The next two methods were donated by John_Karabaic@NeXT.COM (John Karabaic)
  95.  
  96. // returns YES or NO depending on whether the person running the app
  97. // is a member of the group specified in the message
  98.  
  99. - (BOOL) groupMemberForID:(gid_t)anID
  100. {
  101.     int numReturned, c;
  102.     int groupArray[NGROUPS];
  103.     
  104.     numReturned = getgroups(NGROUPS, groupArray);    
  105.     for (c=0; c < numReturned; c++) {
  106.         if (groupArray[c] == anID) return YES;
  107.     }    
  108.     return NO;
  109. }
  110.  
  111. - (BOOL) groupMemberForString:(const char *)aString
  112. {
  113.     char localString[MAXNAMLEN+1];
  114.     struct group *aGroup;
  115.     
  116.     if (localString && *localString) {
  117.         strncpy(localString, aString, MAXNAMLEN);
  118.         aGroup = getgrnam(localString);
  119.         return [self groupMemberForID:(aGroup->gr_gid)];
  120.     }
  121.     return NO;
  122. }
  123.  
  124. @end
  125.