home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / LASTCA3S / LASTCALL.C next >
Text File  |  1992-03-21  |  7KB  |  253 lines

  1. #include <io.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <dos.h>
  7. #include "user.h"
  8.  
  9. int s_len, userfile, num_users, test = 0, alias = 0;
  10. long length;
  11.  
  12. time_t tnow;
  13. struct tm *tmnow;
  14.  
  15. #define MAX_USERS 500
  16.  
  17. typedef struct _info
  18. {
  19.    char name[36];
  20.    char from[36];
  21.    union stamp_combo ludate;
  22.    unsigned int time;
  23. } INFO;
  24.  
  25. INFO *info[MAX_USERS];
  26.  
  27. FILE *out;
  28.  
  29. int back(const void *a, const void *b);
  30. void qsort2(void *base, unsigned nel, unsigned width,
  31.             int (*comp)(const void *, const void *));
  32.  
  33. main(int argc, char *argv[])
  34. {
  35.    struct _usr users;
  36.  
  37.    int x, y, z;
  38.  
  39.    if(argc < 4)
  40.    {
  41.       puts("Usage:");
  42.       puts("        LASTCALL userfile output_file system_name [A]");
  43.       puts("Example: ");
  44.       puts("        LASTCALL USER.BBS C:\\MAX\\MISC\\LASTCALL.BBS \"The Source for Source\" A");
  45.       puts(" ");
  46.       puts("The A is optional, it is used for Aliases in the Name field.");
  47.       exit(255);
  48.    }
  49.  
  50.    if(argc == 5) alias = 1;
  51.    tzset();
  52.    time(&tnow);
  53.    tmnow = localtime(&tnow);
  54.  
  55.    printf("\nLASTCALL      By Erik VanRiper     (1:107/230@fidonet.org)\n\n" \
  56.            "This program is free for non-commercial use.\n");
  57.    if(alias)
  58.       printf("\nUsing Aliases.\n");
  59.  
  60.    if((userfile=open(argv[1],O_RDONLY | O_BINARY))==-1)
  61.    {
  62.       printf("\nError:  Unable to locate %s\n",argv[1]);
  63.       exit(255);
  64.    }
  65.  
  66.    read(userfile,&users,sizeof(struct _usr));
  67.  
  68.    s_len = users.struct_len ? users.struct_len * 20 : 180;
  69.  
  70.    length = filelength(userfile);
  71.  
  72.    num_users = length / s_len;
  73.    printf("\n%d Users . . .\n",num_users);
  74.  
  75.    for(x = 0; x < MAX_USERS; x++)
  76.    {
  77.       if((info[x] = malloc(sizeof(INFO))) == NULL)
  78.       {
  79.          printf("\nOut of memory!\n");
  80.          exit(255);
  81.       }
  82.    }
  83.  
  84.    printf("\rReading . . .");
  85.    test = read_user(num_users);
  86.    close(userfile);
  87.  
  88.    if((out = fopen(argv[2],"w+b")) == NULL)
  89.       exit(0);
  90.    
  91.    x = strlen(argv[3]);
  92.    x += 27;
  93.    y = (80 - x) / 2;
  94.  
  95.    for(z = 0; z < y; z++) fprintf(out," ");
  96.    fprintf(out,"Å Last callers on %s (%-2.2d/%-2.2d/%-2.2d)\nî",
  97.                 argv[3],
  98.                 tmnow->tm_mon+1,
  99.                 tmnow->tm_mday,
  100.                 tmnow->tm_year);
  101.    
  102.    for(z = 0; z < y; z++) fprintf(out," ");
  103.    for(y = 0;y < x;y++) fprintf(out,"─");
  104.    fprintf(out,"\n\nâ");
  105.    fprintf(out,"  ┌─────────────────────────┬─────────────────────────────┬─────────┬──────┐\n");
  106.    fprintf(out,"  │¢        User Name        â│¢   City / State / Province   â│¢ Time On â│¢ Used â│\n");
  107.    fprintf(out,"  ├─────────────────────────┼─────────────────────────────┼─────────┼──────┤\n");
  108.  
  109.    printf("\rSorting . . .");
  110.    qsort2(info, test, sizeof(INFO *), back);
  111.  
  112.    printf("\rWriting . . .");
  113.  
  114.    z = 0;
  115.    for(x = 0;x < test; x++)
  116.    {
  117.       if(z == 0)
  118.       {
  119.          fprintf(out,"  │Ä %-23s â│Ä %-27s â│Ä  %-2.2d:%-2.2d  â│Ä  %-3d â│\n",
  120.                   info[x]->name,
  121.                   info[x]->from,
  122.                   info[x]->ludate.msg_st.time.hh,
  123.                   info[x]->ludate.msg_st.time.mm,
  124.                   info[x]->time);
  125.          z = 1;
  126.       }
  127.       else
  128.       fprintf(out,"  │ç %-23s â│ç %-27s â│ç  %-2.2d:%-2.2d  â│ç  %-3d â│\n",
  129.                   info[x]->name,
  130.                   info[x]->from,
  131.                   info[x]->ludate.msg_st.time.hh,
  132.                   info[x]->ludate.msg_st.time.mm,
  133.                   info[x]->time);
  134.    }
  135.  
  136.    fprintf(out,"â  └─────────────────────────┴─────────────────────────────┴─────────┴──────┘\n\n");
  137.  
  138.    fclose(out);
  139.    printf("\rDone!        \n");
  140.    for(x = 0; x < MAX_USERS; x++)
  141.    {
  142.       free(info[x]);
  143.    }
  144.    return(1);
  145. }
  146.  
  147. read_user(int p)
  148. {
  149.    struct _usr users[1];
  150.  
  151.    int x, y = 0;
  152.  
  153.    for (x=1;x < p;x++)
  154.    {
  155.       lseek(userfile,(long)x*(long)s_len,SEEK_SET);
  156.       read(userfile,&users[0],sizeof(struct _usr));
  157.       if((users[0].ludate.msg_st.date.mo    == tmnow->tm_mon+1) &&
  158.          (users[0].ludate.msg_st.date.da    == tmnow->tm_mday)  &&
  159.          (users[0].ludate.msg_st.date.yr+80 == tmnow->tm_year)  &&
  160.          ((users[0].bits & BITS_NOULIST) == 0))
  161.       {
  162.          info[y]->ludate    = users[0].ludate;
  163.          info[y]->time      = users[0].time;
  164.          if(alias)
  165.          {
  166.             if(users[0].alias[0] != NULL)
  167.             {
  168.                strcpy(info[y]->name,users[0].alias);
  169.             }
  170.             else
  171.             {
  172.                strcpy(info[y]->name,users[0].name);
  173.             }
  174.          }
  175.          else strcpy(info[y]->name,users[0].name);
  176.          strcpy(info[y]->from,users[0].city);
  177.          y++;
  178.          if(y > MAX_USERS)
  179.          {
  180.             printf("\nError number 72.  Please report this to the author!\n");
  181.             close(userfile);
  182.             for(x = 0; x < MAX_USERS; x++) free(info[x]);
  183.             exit(255);
  184.          }
  185.       }
  186.    }
  187.    return(y);
  188. }
  189.  
  190. int back(const void *a, const void *b)
  191. {
  192.    INFO *ai = *((INFO **)a);
  193.    INFO *bi = *((INFO **)b);
  194.    if(ai->ludate.msg_st.date.yr == bi->ludate.msg_st.date.yr)
  195.    {
  196.       if(ai->ludate.msg_st.date.mo == bi->ludate.msg_st.date.mo)
  197.       {
  198.          if(ai->ludate.msg_st.date.da == bi->ludate.msg_st.date.da)
  199.          {
  200.             if(ai->ludate.msg_st.time.hh == bi->ludate.msg_st.time.hh)
  201.             {
  202.                return((ai->ludate.msg_st.time.mm < bi->ludate.msg_st.time.mm) ? 1 : -1);
  203.             }
  204.             return((ai->ludate.msg_st.time.hh < bi->ludate.msg_st.time.hh) ? 1 : -1);
  205.          }
  206.          return((ai->ludate.msg_st.date.da < bi->ludate.msg_st.date.da) ? 1 : -1);
  207.       }
  208.       return((ai->ludate.msg_st.date.mo < bi->ludate.msg_st.date.mo) ? 1 : -1);
  209.    }
  210.    return((ai->ludate.msg_st.date.yr < bi->ludate.msg_st.date.yr) ? 1 : -1);
  211. }
  212.  
  213. /*   From: Ray Gardner  */
  214.  
  215. /* qsort2()  --  Shell sort   Raymond Gardner   public domain   11/91
  216. **
  217. ** A version of qsort that uses Shell's sort algorithm.
  218. */
  219. void qsort2(void *base, unsigned nel, unsigned width,
  220.             int (*comp)(const void *, const void *))
  221. {
  222.    unsigned int wnel, gap, wgap, i, j, k;
  223.    char *a, *b, tmp;
  224.  
  225.    wnel = width * nel;
  226.    for(gap = 0; ++gap < nel;)
  227.        gap *= 3;
  228.    while ( gap /= 3 )
  229.    {
  230.       wgap = width * gap;
  231.       for ( i = wgap; i < wnel; i += width )
  232.       {
  233.          for ( j = i - wgap; ;j -= wgap )
  234.          {
  235.             a = j + (char *)base;
  236.             b = a + wgap;
  237.             if((*comp)(a, b) <= 0)
  238.                break;
  239.             k = width;
  240.             do
  241.             {
  242.                tmp = *a;
  243.                *a++ = *b;
  244.                *b++ = tmp;
  245.             } while ( --k );
  246.             if(j < wgap)
  247.                break;
  248.          }
  249.       }
  250.    }
  251. }
  252.  
  253.