home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / whoami12.zip / WHOAMI.C < prev    next >
C/C++ Source or Header  |  1996-06-23  |  7KB  |  320 lines

  1. /*
  2.  * whoami (vio and pm version.) for OS/2 internet access. v1.2
  3.  *
  4.  * Simply tells you what hostname and IP address is assigned to your station.
  5.  *
  6.  * Yes, this is partly 'sloppy' coding. It works anyway. :) Actually, I was
  7.  *   experimenting with having the source code for the vio (text-based) and
  8.  *   presentation manager versions of this program in the same file.
  9.  *                             ***BIG MISTAKE***
  10.  *    The product of this experiment was a whole bunch of #ifdefs. Yick.
  11.  *
  12.  * This file looks best if tab stops are set to 4 characters.
  13.  *
  14.  * To compile a Presentation Manager application, #define PM_APP somewhere,
  15.  *  otherwise #define VIO_APP or leave it alone (VIO_APP will be #defined by
  16.  *  default.)
  17.  *
  18.  * This compiles for me under EMX 0.9b with no complaint.
  19.  *   (even with -Wall! Goddamn I'm proud!  :)   )
  20.  *
  21.  * Feel free to use this code in anyway you see fit.
  22.  *  Originally written by Ryan C. Gordon, 1996.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <process.h>
  29. #include <errno.h>
  30. #include <signal.h>
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <sys/wait.h>
  34. #include <netinet/in.h>
  35. #include <netdb.h>
  36. #include <ctype.h>
  37.  
  38. #ifdef PM_APP
  39.     #define INCL_WIN
  40.     #define INCL_GPI
  41.     #include <os2.h>
  42.     #ifdef VIO_APP
  43.         #undef VIO_APP
  44.     #endif
  45.  
  46.     #define UM_WHOAMI_DONE    WM_USER + 2
  47.     #define ADDSPACE        6
  48.  
  49. #else
  50.     #if !defined VIO_APP
  51.         #define VIO_APP
  52.     #endif
  53.  
  54.         /* we don't need to #include <os2.h>,     */
  55.         /*    so here's some boolean data types... */
  56.     typedef unsigned long BOOL;
  57.     #define TRUE    (1)
  58.     #define FALSE    (0)
  59. #endif
  60.  
  61.     /* constants : show IP address, hostname, or both? */
  62. #define JUSTSHOW_BOTH 0
  63. #define JUSTSHOW_IP   1
  64. #define JUSTSHOW_HOST 2
  65.  
  66.     /* command line arguments. */
  67. static char ARG_KEEPON[]   = "/k";        /* keep trying until info is found. */
  68. static char ARG_JUSTIP[]   = "/ip";        /* only display the IP address.        */
  69. static char ARG_JUSTHOST[] = "/host";    /* only display the hostname.        */
  70. static char ARG_EXE[]       = "/exe";    /* execute w/ env variable set.        */
  71.  
  72.     /* string literals.  For translating to other languages... */
  73. #ifdef PM_APP
  74.  static char TITLEBAR[]  = "PMwhoami v1.2";
  75.  static char NO_PM_EXE[] = "Please use the text version of this program.";
  76. #else
  77.  static char ENVSTRING[] = "HOSTNAME";
  78. #endif
  79.  static char NOHOST[]    = "Couldn't get hostname information.";
  80.  static char BOGUSARGS[] = "Invalid arguments.";
  81.  
  82.  
  83.     /* global variables. */
  84. BOOL bogusargs = FALSE;
  85. BOOL keepon = FALSE;
  86. BOOL shouldexe = FALSE;
  87. int justshow = JUSTSHOW_BOTH;
  88. int errorlevel = 0;
  89. char myname[500];
  90. char *iakenv;
  91.  
  92.  
  93. #ifdef PM_APP
  94.     HWND hwndFrame, hwndClient;
  95. #endif
  96.  
  97. void getinfo(void)
  98. {
  99.     struct hostent *hp;
  100.     BOOL getout = FALSE;
  101.     unsigned int work;
  102.     int looper;    
  103.     char ipaddr[20];
  104.  
  105. #ifdef PM_APP
  106.     if (shouldexe)
  107.     {
  108.         strcpy(myname, NO_PM_EXE);
  109.         return;
  110.     } /* if */
  111. #endif
  112.  
  113.     if (bogusargs)
  114.     {
  115.         strcpy(myname, BOGUSARGS);
  116.         return;
  117.     } /* if */
  118.  
  119.     ipaddr[0] = '\0';
  120.  
  121.     while (!getout)
  122.     {
  123.  
  124.         gethostname(myname, 256);
  125.         hp = gethostbyname(myname);
  126.  
  127.         if (hp != NULL)
  128.         {
  129.             getout = TRUE;
  130.             strcpy(myname, hp->h_name);
  131.  
  132.             if (justshow != JUSTSHOW_HOST)
  133.             {
  134.                 strcat(myname, "  (");
  135.  
  136.                 for (looper = 0; looper < hp->h_length; looper++)
  137.                 {
  138.                     work = (unsigned char) hp->h_addr_list[0][looper];
  139.                     _itoa(work, &ipaddr[strlen(ipaddr)], 10);
  140.                     strcat(ipaddr, ".");
  141.                 } /* for */
  142.  
  143.                 ipaddr[strlen(ipaddr) - 1] = '\0';  /* lose last '.'... */
  144.  
  145.                 if (justshow == JUSTSHOW_IP)
  146.                     strcpy(myname, ipaddr);
  147.                 else
  148.                 {
  149.                     strcat(myname, ipaddr);
  150.                     strcat(myname, ")");
  151.                 } /* else */
  152.  
  153.             } /* if */    
  154.  
  155.         } /* if */
  156.  
  157.         else if (keepon == FALSE)
  158.         {
  159.             strcpy(myname, NOHOST);
  160.             getout = TRUE;
  161.             errorlevel = 1;
  162.         } /* else */
  163.  
  164.     } /* while */
  165.  
  166. } /* getinfo */
  167.  
  168.  
  169. #ifdef PM_APP
  170. void whoamiThread(void *x)
  171. {
  172.  
  173.     getinfo();
  174.     WinPostMsg(hwndClient, UM_WHOAMI_DONE, NULL, NULL);
  175.     _endthread();
  176. } /* whoamiThread */
  177.  
  178.  
  179. void fontxy(HPS hps, INT *x, INT *y)
  180. /* FONTMETRICS is a HUGE structure...rather than keep that whole thing in
  181.  *  memory for the two elements I need, I just store it temporarily on the
  182.  *  stack, by using this separate function.
  183.  */
  184. {
  185.     FONTMETRICS fm;
  186.  
  187.     GpiQueryFontMetrics(hps, sizeof (fm), &fm);
  188.     *x = fm.lAveCharWidth;
  189.     *y = fm.lMaxBaselineExt;
  190. } /* fontxy */
  191.  
  192.  
  193. MRESULT EXPENTRY whoamiWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  194. {
  195.     static int tidWork;
  196.     static INT cxChar, cyChar;
  197.     HPS hps;
  198.     RECTL rcl;
  199.  
  200.     switch (msg)
  201.     {
  202.         case WM_CREATE:
  203.             tidWork = _beginthread(whoamiThread, NULL, 32768, (void *) NULL);
  204.             if (tidWork == -1)
  205.                 WinPostMsg(hwnd, WM_QUIT, NULL, NULL);
  206.             return(0);
  207.  
  208.         case UM_WHOAMI_DONE:
  209.             hps = WinGetPS(hwnd);
  210.             fontxy(hps, &cxChar, &cyChar);
  211.             WinReleasePS(hps);                           
  212.  
  213.             WinSetWindowPos(hwndFrame, NULLHANDLE,
  214.                             0, 0,
  215.                             cxChar * (strlen(myname) + ADDSPACE),
  216.                             cyChar * 3,
  217.                             SWP_SHOW | SWP_SIZE | SWP_MOVE);
  218.  
  219.             WinSetFocus(HWND_DESKTOP, hwndFrame);
  220.             return(0);
  221.  
  222.         case WM_PAINT:
  223.             hps = WinBeginPaint(hwnd, NULLHANDLE, NULL);
  224.  
  225.             WinQueryWindowRect(hwnd, &rcl);
  226.             WinDrawText(hps, -1, myname, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  227.                         DT_CENTER | DT_VCENTER | DT_ERASERECT);
  228.  
  229.             WinEndPaint(hps);
  230.             return(0);
  231.     } /* switch */
  232.  
  233.     return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  234. } /* whoamiWndProc */
  235.  
  236. #endif
  237.  
  238.  
  239. int main(int argc, char **argv)
  240. /*
  241.  * The mainline. Ooh yeah.
  242.  */
  243. {
  244.  
  245. #ifdef PM_APP
  246.     static CHAR  szClientClass [] = "PMwhoamiClass";
  247.     static ULONG flFrameFlags = (FCF_TITLEBAR   | FCF_SYSMENU    |
  248.                                  FCF_SIZEBORDER | FCF_MINMAX     |
  249.                                  FCF_TASKLIST   | FCF_ICON        );
  250.     HAB hab;
  251.     HMQ hmq;
  252.     QMSG qmsg;
  253. #endif
  254.  
  255.     int looper;
  256.     int exearg = 0;
  257.  
  258.     for (looper = 1; looper < argc; looper++)
  259.     {
  260.         if (stricmp(argv[looper], ARG_KEEPON) == 0)
  261.             keepon = TRUE;
  262.         else if (stricmp(argv[looper], ARG_JUSTHOST) == 0)
  263.             justshow = JUSTSHOW_HOST;
  264.         else if (stricmp(argv[looper], ARG_JUSTIP) == 0)
  265.             justshow = JUSTSHOW_IP;
  266.         else if (stricmp(argv[looper], ARG_EXE) == 0)
  267.             shouldexe = TRUE;
  268.         else
  269.             bogusargs = TRUE;
  270.  
  271.         if (shouldexe)
  272.         {
  273.             exearg = looper + 1;
  274.             looper = argc; /* break loop. */
  275.         } /* if */
  276.     } /* for */
  277.  
  278. #ifdef PM_APP
  279.  
  280.     hab = WinInitialize(0);
  281.     hmq = WinCreateMsgQueue(hab, 0);
  282.  
  283.     WinRegisterClass(hab, szClientClass, whoamiWndProc, CS_SIZEREDRAW, 0);
  284.  
  285.     hwndFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE,
  286.                                     &flFrameFlags, szClientClass,
  287.                                     TITLEBAR, 0L, 0, 1, &hwndClient);
  288.  
  289.     while (WinGetMsg(hab, &qmsg, NULLHANDLE, 0, 0))
  290.         WinDispatchMsg(hab, &qmsg);
  291.  
  292.     /* Deinitialize Presentation Manager stuff... */
  293.     WinDestroyWindow(hwndFrame);
  294.     WinDestroyMsgQueue(hmq);
  295.     WinTerminate(hab);
  296.  
  297. #else
  298.     getinfo();    
  299.  
  300.     if (shouldexe)
  301.     {
  302.         if (argv[exearg] == NULL)
  303.             printf("%s\n", BOGUSARGS);
  304.         else
  305.         {
  306.             iakenv = malloc(strlen(ENVSTRING) + strlen(myname) + 2);
  307.             sprintf(iakenv, "%s=%s", ENVSTRING, myname);
  308.             putenv(iakenv);
  309.             spawnvp(P_NOWAIT, argv[exearg], &argv[exearg + 1]);
  310.         } /* else */
  311.     } /* if */
  312.     else
  313.         printf("%s\n", myname);
  314. #endif
  315.  
  316.     return(errorlevel);
  317.     
  318. } /* main */
  319.  
  320.