home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / LMUTIL.ZIP / USERNAME.C < prev    next >
C/C++ Source or Header  |  1991-03-19  |  3KB  |  132 lines

  1. /*
  2. ** Program: USERNAME
  3. **
  4. ** Author:  Roger L Soles
  5. **
  6. ** Description:
  7. **
  8. ** This example C program uses the NetWkstaGetInfo call to obtain
  9. ** information to determine whether a user is logged on or not.
  10. **
  11. ** Instructions:
  12. **
  13. ** USERNAME [-?q] USER_NAME
  14. **
  15. ** Errorlevel = 0, user is currently logged on on at this workstation
  16. ** Errorlevel = 1, user is not currently logged on at this workstation
  17. **
  18. ** if '-q' is not specified, command will execute in verbose mode
  19. **    (it will provide messages on the screen).
  20. **
  21. */
  22.  
  23. #define LINT_ARGS
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #define INCL_NETGROUP
  28. #define INCL_NETWKSTA
  29. #define INCL_NETERRORS
  30. #include <lan.h>
  31. #include "getsrv.h"
  32.  
  33. void main (int argc, char * argv[])
  34. {
  35.  
  36.    char far * cptr;
  37.    int qmode = 0;
  38.  
  39.    API_RET_TYPE uRetCode = 0;
  40.  
  41.    char far * pszServer;
  42.    char far * pszUserName;
  43.    char far * pszName;
  44.  
  45.    argv++; argc--;
  46.  
  47.    cptr = argv[0];
  48.    if ( *cptr++ == '-' )
  49.    {
  50.       switch (*cptr)
  51.       {
  52.       case 'q':
  53.       case 'Q':
  54.          /*
  55.          ** quite mode inhibits messages -- rather uses only
  56.          ** the error level
  57.          */
  58.          qmode = 1;
  59.          break;
  60.       case '?':
  61.       default:
  62.          printf ("\nUSAGE:  USERNAME [-?q] USER_NAME\n");
  63.          exit (1);
  64.       }
  65.  
  66.       argv++; argc--;
  67.    }
  68.  
  69.    if (argc >= 1)
  70.    {
  71.       /*
  72.       ** User Name
  73.       */
  74.       strupr (argv[0]);
  75.       pszName = argv[0];
  76.    }
  77.    else
  78.    {
  79.       pszName = NULL;
  80.    }
  81.  
  82.    /*
  83.    ** Get Logon Server & User Name
  84.    */
  85.    if ( get_logon_server (&pszServer, &pszUserName) != 0 )
  86.    {
  87.       if (qmode == 0)
  88.       {
  89.          if (pszName == NULL)
  90.          {
  91.             printf ("Workstation Not Logged In\n");
  92.          }
  93.          else
  94.          {
  95.             printf ("%Fs is not currently logged on\n", pszName);
  96.          }
  97.       }
  98.       exit (1);
  99.    }
  100.  
  101.    /*
  102.    ** No user name given, so report who is logged in
  103.    */
  104.    if (pszName == NULL)
  105.    {
  106.       if (qmode == 0)
  107.       {
  108.          printf ("%Fs is currently logged onto %Fs\n", pszUserName, pszServer);
  109.       }
  110.       exit (0);
  111.    }
  112.  
  113.    /*
  114.    ** Check to see if current user is as expected
  115.    */
  116.    if ( strcmp (pszName, pszUserName ) == 0 )
  117.    {
  118.       if (qmode == 0)
  119.       {
  120.          printf ("%Fs is currently logged onto %Fs\n", pszName, pszServer);
  121.       }
  122.       exit (0);
  123.    }
  124.  
  125.    if (qmode == 0)
  126.    {
  127.          printf ("%Fs is not currently logged onto %Fs\n", pszName, pszServer);
  128.    }
  129.    exit (1);
  130. }
  131.  
  132.