home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs567s.zip / rcs / src / ms / loginos2.c < prev    next >
C/C++ Source or Header  |  1994-06-04  |  5KB  |  193 lines

  1. /* getlogin for OS/2 */
  2.  
  3. /* NetWare code by Frank Whaley */
  4. /* further hacking by Paul Eggert and Kai Uwe Rommel */
  5. /* LAN Server/Manager and 32-bit code by Kai Uwe Rommel */
  6.  
  7. /* $Id: loginos2.c,v 1.2 1992/01/06 03:18:26 eggert Exp $ */
  8.  
  9. #define INCL_NOPM
  10. #define INCL_DOS
  11. #include <os2.h>
  12. #include <string.h>
  13.  
  14. #pragma pack(1)
  15.  
  16. /* Compilation is supported with emx (gcc) for OS/2 32-bit and with MS
  17.  * C 6.00A for OS/2 16-bit targets. If compiling for emx (32-bit),
  18.  * version 0.8h with at least fix level 7 is absolutely required. */
  19.  
  20. /* Because importing network API functions directly would prevent the
  21.  * executables from loading on systems without the corresponding
  22.  * network software, me must link them in during runtime manually with
  23.  * DosLoadModule/DosGetProcAddress/DosFreeModule. This gets even more
  24.  * difficult and ugly in 32-bit mode, since we have to call the same
  25.  * old 16-bit API's and thunking from gcc (emx) is a bit difficult. */
  26.  
  27. #ifndef __32BIT__
  28. #define DosQueryProcAddr(handle, ord, name, funcptr) \
  29.     DosGetProcAddr(handle, name, funcptr)
  30. #endif
  31.  
  32. /* ----- Novell NetWare ----- */
  33.  
  34. struct info 
  35. {
  36.   USHORT connectionID;
  37.   USHORT connectFlags;
  38.   USHORT sessionID;
  39.   USHORT connectionNumber;
  40.   CHAR serverAddr[12];
  41.   USHORT serverType;
  42.   CHAR serverName[48];
  43.   USHORT clientType;
  44.   CHAR clientName[48];
  45. };
  46.  
  47. typedef struct info FAR *PINFO;
  48.  
  49. #ifndef __32BIT__
  50. #define NWGDCID16 NWGetDefaultConnectionID
  51. #define NWGCS16 NWGetConnectionStatus
  52. #endif
  53.  
  54. USHORT (APIENTRY *NWGDCID16)(PUSHORT);
  55. USHORT (APIENTRY *NWGCS16)(USHORT, PINFO, USHORT);
  56.  
  57. #ifdef __32BIT__
  58. USHORT NWGetDefaultConnectionID(PUSHORT pID)
  59. {
  60.   return (USHORT)
  61.           (_THUNK_PROLOG (4);
  62.            _THUNK_FLAT (pID);
  63.            _THUNK_CALLI (_emx_32to16(NWGDCID16)));
  64. }
  65.  
  66. USHORT NWGetConnectionStatus(USHORT nID, PINFO pInfo, USHORT nSize)
  67. {
  68.   return (USHORT)
  69.           (_THUNK_PROLOG (2+4+2);
  70.            _THUNK_SHORT (nID);
  71.            _THUNK_FLAT (pInfo);
  72.            _THUNK_SHORT (nSize);
  73.            _THUNK_CALLI (_emx_32to16(NWGCS16)));
  74. }
  75. #endif
  76.  
  77. char *nw_getlogin()
  78. {
  79.   static struct info ci;
  80.  
  81.   HMODULE NWCalls;
  82.   USHORT id;
  83.   char buf[256], *res = NULL;
  84.  
  85.   if (DosLoadModule(buf, sizeof(buf), "NWCALLS", &NWCalls) == 0) 
  86.   {
  87.     if (DosQueryProcAddr(NWCalls, 0, "NWGETDEFAULTCONNECTIONID", 
  88.              (PPFN) &NWGDCID16) == 0 &&
  89.         DosQueryProcAddr(NWCalls, 0, "NWGETCONNECTIONSTATUS", 
  90.              (PPFN) &NWGCS16) == 0)
  91.       if (NWGetDefaultConnectionID(&id) == 0 && 
  92.       NWGetConnectionStatus(id, &ci, sizeof(ci)) == 0)
  93.       {
  94.     strlwr(ci.clientName);
  95.     res = ci.clientName;
  96.       }
  97.  
  98.     DosFreeModule(NWCalls);
  99.   }
  100.  
  101.   return res;
  102. }
  103.  
  104. /* ----- Microsoft LAN Manager, IBM LAN Server ----- */
  105.  
  106. #ifdef __32BIT__
  107. #define PSTR _far16ptr
  108. #define _fstrcpy(d, s) strcpy(d, _emx_16to32(s))
  109. #else
  110. #define PSTR PSZ
  111. #define NWGI16 NetWkstaGetInfo
  112. #endif
  113.  
  114. struct wksta_info_10
  115. {
  116.   PSTR computername;
  117.   PSTR username;
  118.   PSTR langroup;
  119.   BYTE ver_major;
  120.   BYTE ver_minor;
  121.   PSTR logon_domain;
  122.   PSTR oth_domains;
  123.   BYTE filler[32];
  124. };
  125.  
  126. typedef struct wksta_info_10 FAR *PWKSTA;
  127.  
  128. static USHORT (APIENTRY *NWGI16)(PSZ pszServer, USHORT sLevel, 
  129.   PWKSTA pbBuffer, USHORT cbBuffer, PUSHORT pcbTotalAvail);
  130.  
  131. #ifdef __32BIT__
  132. USHORT NetWkstaGetInfo(PSZ pszServer, USHORT sLevel, PWKSTA pbBuffer, 
  133.                USHORT cbBuffer, PUSHORT pcbTotalAvail)
  134. {
  135.   return (USHORT)
  136.           (_THUNK_PROLOG (4+2+4+2+4);
  137.            _THUNK_FLAT (pszServer);
  138.            _THUNK_SHORT (sLevel);
  139.            _THUNK_FLAT (pbBuffer);
  140.            _THUNK_SHORT (cbBuffer);
  141.            _THUNK_FLAT (pcbTotalAvail);
  142.            _THUNK_CALLI (_emx_32to16(NWGI16)));
  143. }
  144. #endif
  145.  
  146. char *lan_getlogin()
  147. {
  148.   struct wksta_info_10 wksta;
  149.   static char name[32];
  150.  
  151.   HMODULE NETAPI;
  152.   USHORT total;
  153.   char buf[256], *res = NULL;
  154.  
  155.   if (DosLoadModule(buf, sizeof(buf), "NETAPI", &NETAPI) == 0) 
  156.   {
  157.     if (DosQueryProcAddr(NETAPI, 0, "NETWKSTAGETINFO", (PPFN) &NWGI16) == 0)
  158.       if (NetWkstaGetInfo(0, 10, &wksta, sizeof(wksta), &total) == 0)
  159.       {
  160.     _fstrcpy(name, wksta.username);
  161.     strlwr(name);
  162.     res = name;
  163.       }
  164.  
  165.     DosFreeModule(NETAPI);
  166.   }
  167.  
  168.   return res;
  169. }
  170.  
  171. #ifdef TEST
  172.  
  173. /* ----- testing code ----- */
  174.  
  175. void main(void)
  176. {
  177.   printf("NetWare: <%s>\nLAN Manager: <%s>\n", 
  178.      nw_getlogin(), lan_getlogin());
  179. }
  180.  
  181. #else
  182.  
  183. /* ----- main entry point ----- */
  184.  
  185. char *getlogin()
  186. {
  187.   char *lm = lan_getlogin();
  188.   char *nw = nw_getlogin();
  189.   return lm ? lm : nw ? nw : (char *) 0;
  190. }
  191.  
  192. #endif
  193.