home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / NWLINNAM.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  3KB  |  122 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  NWLINNAME.C - Novell Netware: getNwLoginName
  5. **  This should work for Advanced NetWare (2.x) -> NetWare386 3.x & 4.x
  6. **
  7. **  Written by david nugent, public domain
  8. */
  9.  
  10. /* #define TEST */            /* Uncomment to compile test main()       */
  11.  
  12. #include <string.h>
  13. #include <process.h>
  14. #include <dos.h>
  15. #include <errno.h>
  16. #include "snpdosys.h"
  17.  
  18. #define addrOffset(val)     (unsigned short)(((long)(val)) & 0xfff)
  19.     /* Invoke Novell Netware API.  */
  20. #define callNetware(func, reqbuf, repbuf, inregs, segregs) \
  21.     { \
  22.         (inregs)->h.ah = (func); \
  23.         (inregs)->x.si = addrOffset(reqbuf); \
  24.         (inregs)->x.di = addrOffset(repbuf); \
  25.         intdosx(inregs, inregs, segregs); \
  26.     }
  27.  
  28. #if defined(_MSC_VER) || defined(_QC) || defined(__WATCOM__)
  29.     #pragma pack(1)
  30. #elif defined(__ZTC__)
  31.     #pragma ZTC align 1
  32. #elif defined(__TURBOC__) && (__TURBOC__ > 0x202)
  33.     #pragma option -a-
  34. #endif
  35.  
  36. /*
  37. **  Get Connection Information E3(16)
  38. */
  39.  
  40. struct _conninfo
  41. {
  42.     unsigned short len;
  43.     unsigned char func;
  44.     unsigned char cno;
  45. };
  46.  
  47. struct _connrep
  48. {
  49.     unsigned short len;
  50.     unsigned long objectID;
  51.     unsigned short objecttype;
  52.     char objectname[48];
  53.     unsigned char logintime[7];
  54.     unsigned char reserved[39];
  55. };
  56.  
  57. #if defined(_MSC_VER) || defined(_QC) || defined(__WATCOM__)
  58.     #pragma pack()
  59. #elif defined(__ZTC__)
  60.     #pragma ZTC align 2
  61. #elif defined(__TURBOC__) && (__TURBOC__ > 0x202)
  62.     #pragma option -a
  63. #endif
  64.  
  65. int getNwLoginName (char * namebuf)
  66. {
  67.       union REGS r;
  68.  
  69.       r.x.ax = 0xDC00;    /* Get Connection Number */
  70.       intdos(&r, &r);
  71.  
  72.       /*
  73.       ** If the connection number is in range 1-100 (ie. valid),
  74.       ** get Connection Information and retrieve the user name.
  75.       */
  76.  
  77.       if (r.h.al <= 0 ||  r.h.al > 100)
  78.             return errno = EINVAL;
  79.  
  80.       else
  81.       {
  82.             struct SREGS s;
  83.             static struct _connrep connrep;
  84.             static struct _conninfo conninfo;
  85.  
  86.             conninfo.len = sizeof(conninfo) - sizeof(conninfo.len);
  87.             conninfo.func = 0x16;   /* Get connection information */
  88.             conninfo.cno = r.h.al;
  89.             connrep.len = sizeof(connrep) - sizeof(connrep.len);
  90.             segread(&s);
  91.             s.es = s.ds;    /* ds:si=request buffer, es:di=reply buffer */
  92.  
  93.             /* login info */
  94.  
  95.             callNetware(0xE3, &conninfo, &connrep, &r, &s);
  96.             if (r.h.al == 0)
  97.             {
  98.                   strcpy (namebuf, connrep.objectname);
  99.                   return 0;
  100.             }
  101.             return r.h.al;
  102.       }
  103. }
  104.  
  105. #if defined(TEST)
  106.  
  107. #include <stdio.h>
  108.  
  109. int
  110. main()
  111. {
  112.       char loginname[48];
  113.       int rc = getNwLoginName (loginname);
  114.  
  115.       if (rc == 0)
  116.             printf ("Your login name is '%s'\n", loginname);
  117.       else  printf ("Error %d calling Netware, %s\n", rc, strerror(errno));
  118.       return 0;
  119. }
  120.  
  121. #endif /* TEST */
  122.