home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / EVENTS / ICESTAT.ZIP / ICESTAT.C < prev    next >
C/C++ Source or Header  |  1994-11-12  |  8KB  |  280 lines

  1. /*
  2.    ICESTAT.C - iceStat v1.00
  3.    Copyright (C) 1994 by Life Force.
  4.    You may use any of this code that you wish, as long as you
  5.    give credit to Life Force.  If you use the spin function,
  6.    you must give credit to Tolkien.
  7.    Most of this code is ANSI C compatible.
  8.    It will compile successfully under Borland's Turbo C++ v3.00
  9.    for DOS.  You may have to make minor modifications for other
  10.    compilers.
  11.  
  12.    last modification:  12 nov 1994
  13. */
  14.  
  15. #pragma warn -aus
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. /* vardec.h is a header file from the WWIV software by Wayne Bell.
  20.    You need to include a copy of vardec.h from WWIV version 4.22
  21.    or greater.  Versions v4.23+ may require additional header files.
  22.    Read the compiler's error message to find out which one(s).
  23. */
  24. #include "vardec.h"
  25. #include <sys\stat.h>
  26. #include <sys\types.h>
  27. #include <time.h>
  28. #include <dos.h>
  29.  
  30. userrec thisuser;  /* userrec data structure defined in vardec.h */
  31. char    ansi,            /* ansi enabled? */
  32.         data[81],        /* data dir with trailing backslash */
  33.         systemname[51];
  34. int     usernum;
  35. void    show_help (void),
  36.         goxy (int x, int y),
  37.         left (int amount),
  38.         display_data (void),
  39.         spin(unsigned char *s);
  40. int     test_ansi (char *filename),
  41.         display (char *filename),
  42.         read_chaintxt (char *filename),
  43.         read_userlst (void);
  44.  
  45. FILE    *chaintxt,
  46.         *userlst;
  47. /*=========================================================================*/
  48. void main (int argc, char *argv[])
  49. {
  50.     if (argc < 2)
  51.     {
  52.         show_help();
  53.         exit (1);
  54.     }
  55.  
  56.     if (!test_ansi(argv[1])) /* if they don't have ANSI, simply exit */
  57.         exit (1);
  58.  
  59.     if (!read_chaintxt(argv[1]))
  60.     {
  61.         printf ("\n## Error:  unable to read data from %s. \n", argv[1]);
  62.         exit (2);
  63.     }
  64.  
  65.     if (!read_userlst())
  66.     {
  67.         printf ("\n## Error:  unable to read data from %sUSER.LST. \n", data);
  68.         exit (2);
  69.     }
  70.  
  71.     printf ("\x1b[2J"); /* clear screen */
  72.     if (!display ("ICESTAT.ANS"))
  73.     {
  74.         printf ("\n## Error:  unable to display ICESTAT.ANS. \n");
  75.         exit (2);
  76.     }
  77.  
  78.     display_data ();
  79.     printf ("\x1b[2J"); /* clear screen */
  80. }
  81.  
  82. /*=========================================================================*/
  83. /* goxy moves the cursor to a specific point on the screen using
  84.    ANSI escape sequences.
  85. */
  86. void goxy(int x, int y)
  87. {
  88.     printf ("\x1b[%d;%dH", y, x);
  89. }
  90. /*=========================================================================*/
  91. /* left uses ANSI escape sequences to cause the cursor to move left a
  92.    specified amount.
  93. */
  94. void left(int amount)
  95. {
  96.     printf ("\x1b[%dD", amount);
  97. }
  98. /*=========================================================================*/
  99. /* test_ansi examines CHAIN.TXT to determine if the user has ANSI graphics
  100.    support turned on.
  101. */
  102. int test_ansi (char *filename)
  103. {
  104.     char    buffer[81],
  105.             ansi_status;
  106.     register int i;
  107.  
  108.     if ((chaintxt = fopen(filename, "rt")) == NULL)
  109.       {
  110.         printf ("\n## Error:  unable to read data from %s. \n", filename);
  111.         exit (2);
  112.       }
  113.  
  114.     for (i = 1; i < 14; i++)   /* this loop moves to line #14 */
  115.         fgets (buffer, 80, chaintxt);
  116.     ansi_status = getc (chaintxt);
  117.     fclose (chaintxt);
  118.     if (ansi_status != '0')
  119.         return 1;
  120.     else
  121.         return 0;
  122. }
  123. /*=========================================================================*/
  124. void show_help (void)
  125. {
  126.     printf ("\n\niceStat v1.00 \n");
  127.     printf ("Copyright (C) 1994 by Life Force \n");
  128.     printf ("Beta testing by Troy Lancaster & Jim Nunn. \n");
  129.     printf ("Compiled under Turbo C++ v3.00 on %s \n", __DATE__);
  130.     printf ("Syntax is: \n");
  131.     printf ("    ICESTAT <chain.txt> \n");
  132.     printf ("Example: \n");
  133.     printf ("    ICESTAT C:\\WWIV\\CHAIN.TXT \n\n");
  134.     printf ("This program is FREEWARE and may be freely used and copied. \n");
  135. }
  136. /*=========================================================================*/
  137. /* display outputs the contents of a specificed file to standard output
  138.    device by block reading the file into memory, then block writing the
  139.    memory buffer to stdout.
  140. */
  141. int     display (char *filename)
  142. {
  143.     FILE    *fp;
  144.     struct  stat    buff;
  145.     char    *membuf;
  146.  
  147.     if ((fp = fopen(filename, "rb")) == NULL)
  148.       return(0);
  149.  
  150.     stat(filename, &buff);
  151.  
  152.     if ((membuf = malloc(buff.st_size + 1024)) == NULL) {
  153.       printf("\ncannot allocate memory\n");
  154.       fclose(fp);
  155.       return(0);
  156.     }
  157.  
  158.     fread( (void *)membuf, buff.st_size, 1, fp);
  159.     fwrite( (void *)membuf, buff.st_size, 1, stdout);
  160.     fclose(fp);
  161.     free(membuf);
  162.     return(1);
  163. }
  164. /*=========================================================================*/
  165. /* read_chaintxt reads in data from specific lines of the specified
  166.    file name.
  167. */
  168. int  read_chaintxt (char *filename)
  169. {
  170.     char    temp[7];
  171.     int     i;
  172.  
  173.     if ((chaintxt = fopen(filename, "rt")) == NULL)
  174.       return (0);
  175.  
  176.     fgets (temp, 6, chaintxt);   /* grab first line and */
  177.     usernum = atoi (temp);       /* put it into usernum */
  178.  
  179.     for (i = 1; i < 18; i++)                 /* grab data dir off line #18 */
  180.         fgets (data, 81, chaintxt);
  181.     data[strlen (data) - 1] = '\0';
  182.  
  183.     for (i = 18; i < 22; i++)  /* grab system name off line #22 */
  184.         fgets (systemname, 51, chaintxt);
  185.     systemname[strlen (systemname) - 1] = '\0';
  186.  
  187.     fclose (chaintxt);
  188.     return (1);
  189. }
  190. /*=========================================================================*/
  191. int  read_userlst (void)
  192. {
  193.     char    path[128];
  194.     long    size;
  195.  
  196.     size = sizeof (userrec);
  197.  
  198.     sprintf (path, "%sUSER.LST", data);
  199.     if ((userlst = fopen (path, "rb")) == NULL)
  200.       return (0);
  201.  
  202.     fseek (userlst, size * (long) (usernum), SEEK_SET);
  203.     fread (&thisuser, size, 1, userlst);
  204.  
  205.     fclose (userlst);
  206.     return (1);
  207. }
  208. /*=========================================================================*/
  209. void    display_data (void)
  210. {
  211.     time_t now;  /* holds time data */
  212.     int ch;
  213.  
  214.     time (&now);  /* fill structure with current time info */
  215.  
  216.     goxy (39, 4);
  217.     printf ("\x1b[0;37;40m"); /* set color to low white */
  218.     printf ("%s #%d", thisuser.name, usernum);
  219.     goxy (39, 5); printf (systemname);
  220.     goxy (39, 6); printf ("%s", ctime (&now));
  221.     goxy (18, 12); printf (thisuser.city);
  222.     goxy (31, 13); printf ("%d", thisuser.msgpost);
  223.     goxy (28, 14); printf ("%d", (thisuser.emailsent + thisuser.emailnet
  224.                                    + thisuser.feedbacksent) );
  225.     goxy (19, 15); printf ("%dbps", thisuser.lastrate);
  226.     goxy (25, 16); printf ("%dK", thisuser.uk);
  227.     goxy (25, 17); printf ("%dK", thisuser.dk);
  228.     goxy (24, 18); printf ("%lu minutes", (unsigned long) thisuser.timeon / (unsigned long) 60);
  229.     goxy (23, 19);
  230.     if (thisuser.forwardusr)
  231.         printf ("%d@%d", thisuser.forwardusr, thisuser.forwardsys);
  232.     else
  233.         printf ("<not forwarded>");
  234.     goxy (27, 20); printf ("%d", thisuser.logons);
  235.     goxy (52, 13); printf ("%c", thisuser.sex);
  236.     goxy (51, 14); printf ("%d", thisuser.age);
  237.     goxy (63, 15); printf ("%d", thisuser.sl);
  238.     goxy (70, 16); printf ("%d", thisuser.dsl);
  239.     goxy (69, 17); printf ("%d", thisuser.uploaded);
  240.     goxy (69, 18); printf ("%d", thisuser.downloaded);
  241.     goxy (59, 19); printf ("%s", thisuser.laston);
  242.     goxy (61, 20); printf ("%d", thisuser.waiting);
  243.  
  244.     goxy (34, 22);
  245.  
  246.     while ( !kbhit() )
  247.     {
  248.         printf ("\x1b[1;34;40mPRESS A KEY");  /* switch color to blue */
  249.         left (11);
  250.         spin ("PRESS A KEY");
  251.         left (11);
  252.     }
  253.     ch = getch();
  254.     printf ("\x1b[0;0;40m");  /* reset colors */
  255. }
  256.  
  257. /*=========================================================================*/
  258. /* spin is a slightly modified version of Tolkien's spin function from
  259.    his ENHANCE.C.  If you use this function, you -must- give credit
  260.    to Tolkien.
  261. */
  262. void spin (unsigned char *s)
  263. {
  264.     register int i = 0,
  265.                  dly = 18;
  266.  
  267.     printf ("\x1b[1;36;40m");  /* switch color to cyan */
  268.     while (s[i]) 
  269.     {
  270.         delay(dly); putchar('/');  left(1);
  271.         delay(dly); putchar('-');  left(1);
  272.         delay(dly); putchar('\\'); left(1);
  273.         delay(dly); putchar('|');  left(1);
  274.         delay(dly); putchar(s[i]); delay(dly);
  275.         i++;
  276.     }
  277. }
  278.  
  279. /*=========================================================================*/
  280.