home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / top2src.zip / TOPMAINT.ZIP / TOPMAINT.C < prev    next >
C/C++ Source or Header  |  2000-07-10  |  15KB  |  529 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include <conio.h>
  7. #include <dos.h>
  8. #include <time.h>
  9. #include "top.h"
  10.  
  11. #define CLI_FULL        0
  12. #define CLI_DELETE      1
  13. #define CLI_PURGE       2
  14. #define CLI_PACK        3
  15. #define CLI_SETSEC      4
  16. #define CLI_RENAME      5
  17. #define CLI_DEFIX       6
  18.  
  19. user_data_typ ubuf;
  20.  
  21. void main(int argc, char *argv[]);
  22. void showcmdline(XINT cmd);
  23. void deleteuser(char *nam);
  24. void packusers(void);
  25. void purgeusers(unsigned XINT sec, unsigned XINT days);
  26. XINT calc_days_ago(XDATE *lastdate);
  27. void setusersec(unsigned XINT sec, char *nam);
  28.  
  29. void main(int argc, char *argv[])
  30. {
  31.     XINT d;
  32.  
  33.     clrscr();
  34.     printf("TOPMaint v2.00a - User maintenance utility for TOP v2.00\n");
  35.     printf("By Paul Sidorsky, ISMWare\n\n");
  36.  
  37.     for (d = 1; d < argc; d++)
  38.     {
  39.         if (!stricmp(argv[d], "DELETE") && d != argc - 1)
  40.         {
  41.             if (!stricmp(argv[d + 1], "?"))
  42.             {
  43.                 showcmdline(CLI_DELETE);
  44.                 exit(0);
  45.             }
  46.             deleteuser(argv[d + 1]);
  47.             exit(0);
  48.         }
  49.         if (!stricmp(argv[d], "PURGE") && d != argc - 1)
  50.         {
  51.             unsigned XINT sec = 0, days = 0;
  52.  
  53.             if (!stricmp(argv[d + 1], "?"))
  54.             {
  55.                 showcmdline(CLI_PURGE);
  56.                 exit(0);
  57.             }
  58.             while (++d < argc)
  59.             {
  60.                 if (!strnicmp(argv[d], "/S", 2))
  61.                 {
  62.                     sec = strtol(&argv[d][2], NULL, 10);
  63.                 }
  64.                 if (!strnicmp(argv[d], "/D", 2))
  65.                 {
  66.                     days = strtol(&argv[d][2], NULL, 10);
  67.                 }
  68.             }
  69.  
  70.             if (sec != 0 || days != 0)
  71.             {
  72.                 purgeusers(sec, days);
  73.                 exit(0);
  74.             }
  75.         }
  76.         if (!stricmp(argv[d], "PACK"))
  77.         {
  78.             if (d != argc - 1 && !stricmp(argv[d + 1], "?"))
  79.             {
  80.                 showcmdline(CLI_PACK);
  81.                 exit(0);
  82.             }
  83.             packusers();
  84.             exit(0);
  85.         }
  86.         if (!stricmp(argv[d], "SETSEC") && d != argc - 1)
  87.         {
  88.             if (!stricmp(argv[d + 1], "?"))
  89.             {
  90.                 showcmdline(CLI_SETSEC);
  91.                 exit(0);
  92.             }
  93.             if (d < argc - 2)
  94.             {
  95.                 setusersec(strtol(argv[d + 1], NULL, 10), argv[d + 2]);
  96.                 exit(0);
  97.             }
  98.         }
  99.     }
  100.  
  101.     showcmdline(CLI_FULL);
  102.  
  103. }
  104.  
  105. void showcmdline(XINT cmd)
  106. {
  107.  
  108.     if (cmd == CLI_FULL)
  109.     {
  110.         printf("TOPMaint Command Line:\n\n");
  111.         printf("TOPMAINT <command> [<options>]\n\n");
  112.  
  113.         printf("    <command> is one of the following:\n\n");
  114.  
  115.         printf("*   DELETE          Removes a user from the user file.\n");
  116.         printf("    PURGE           Purges users who no longer call the system.\n");
  117.         printf("    PACK            Removes empty (deleted) user records from the user file.\n");
  118.         printf("*   SETSEC          Sets a user's security level.\n");
  119. //        printf("    RENAME          Changes a user's handle.\n");
  120. //        printf("    DEFIX           Undoes the automatic name "fixing" (capitalization).\n");
  121.  
  122.         printf("\n    NOTE:  Commands marked with a * require user interaction and should not\n");
  123.         printf("be run inside a batch file.\n\n");
  124.  
  125.         printf("    For help on any command, enter:\n\n");
  126.  
  127.         printf("TOPMAINT <command> ?\n");
  128.     }
  129.     if (cmd == CLI_DELETE)
  130.     {
  131.         printf("DELETE Command Syntax:\n\n");
  132.         printf("TOPMAINT DELETE \"<user>\"\n\n");
  133.  
  134.         printf("    <user> is the name or partial name of the user you wish to delete.\n");
  135.         printf("           If you wish to specify a first and last name, <user> must be\n");
  136.         printf("           enclosed in quotation marks.\n\n");
  137.  
  138.         printf("    TOPMaint will scan USERS.TOP for users whos real name or handle match\n");
  139.         printf("<user> and prompt for confirmation before deleting.  If you are specifying a\n");
  140.         printf("partial name, pressing N will cause TOP to continue scanning the user file.\n\n");
  141.  
  142.         printf("Examples:\n\n");
  143.  
  144.         printf("TOPMAINT DELETE Simpson\n");
  145.         printf("TOPMAINT DELETE \"Homer Simpson\"\n");
  146.         printf("TOPMAINT DELETE \"Simp\"\n");
  147.         printf("TOPMAINT DELETE \"Paul Sidorsky\"\n");
  148.     }
  149.     if (cmd == CLI_PURGE)
  150.     {
  151.         printf("PURGE Command Syntax:\n\n");
  152.         printf("TOPMAINT PURGE [/S<sec>] [/D<days>]\n\n");
  153.  
  154.         printf("    <sec> is the minimum security a user must have to _NOT_ be purged.  Users\n");
  155.         printf("          who have a security equal to or above the specified security will\n");
  156.         printf("          not be deleted under any circumstances.  If not specified, 0 is\n");
  157.         printf("          assumed (i.e. users will be deleted regardless of security).\n");
  158.         printf("    <days> is the maximum number of days permitted since the user last entered\n");
  159.         printf("           TOP.  Users who have not entered TOP in at least the specified\n");
  160.         printf("           number of days will be deleted.  If not specified or set to 0, all\n");
  161.         printf("           who's security is low enough will be deleted!\n\n");
  162.  
  163.         printf("Examples:\n\n");
  164.  
  165.         printf("TOPMAINT PURGE /S100\n");
  166.         printf("TOPMAINT PURGE /D30\n");
  167.         printf("TOPMAINT PURGE /S500 /D60\n");
  168.         printf("TOPMAINT PURGE /D45 /S10\n");
  169.     }
  170.     if (cmd == CLI_PACK)
  171.     {
  172.         printf("PACK Command Syntax:\n\n");
  173.         printf("TOPMAINT PACK\n\n");
  174.  
  175.         printf("    TOPMaint will remove all blank (deleted) user records from USERS.TOP.\n");
  176.         printf("This operation should be run after a DELETE or PURGE command to compress the\n");
  177.         printf("size of the user file and speed user searching in TOP.\n\n");
  178.  
  179.         printf("Example:\n\n");
  180.  
  181.         printf("TOPMAINT PACK\n");
  182.     }
  183.     if (cmd == CLI_SETSEC)
  184.     {
  185.         printf("SETSEC Command Syntax:\n\n");
  186.         printf("TOPMAINT SETSEC <sec> \"<user>\"\n\n");
  187.  
  188.         printf("    <sec> is the new security you wish to assign, from 0 to 65535.\n");
  189.         printf("    <user> is the name or partial name of the user you wish to delete.\n");
  190.         printf("           If you wish to specify a first and last name, <user> must be\n");
  191.         printf("           enclosed in quotation marks.\n\n");
  192.  
  193.         printf("    TOPMaint will scan USERS.TOP for users whos real name or handle match\n");
  194.         printf("<user> and prompt for confirmation before changing the user's security.  If\n");
  195.         printf("you are specifying a partial name, pressing N will cause TOP to continue\n");
  196.         printf("scanning the user file.\n\n");
  197.  
  198.         printf("Examples:\n\n");
  199.  
  200.         printf("TOPMAINT SETSEC 20 Simpson\n");
  201.         printf("TOPMAINT SETSEC 100 \"Homer Simpson\"\n");
  202.         printf("TOPMAINT SETSEC 0 \"Simp\"\n");
  203.         printf("TOPMAINT SETSEC 65535 \"Paul Sidorsky\"\n");
  204.     }
  205.  
  206. }
  207.  
  208. void deleteuser(char *nam)
  209. {
  210.     XINT d;
  211.     XINT nu;
  212.     FILE *fil;
  213.     char trn[51], tha[51];
  214.     XINT key;
  215.  
  216.     printf("DELETE - Deleting \"%s\"...\n\n", nam);
  217.  
  218.     strupr(nam);
  219.  
  220.     fil = fopen("USERS.TOP", "r+b");
  221.     if (fil == NULL)
  222.     {
  223.         printf("Can't open USERS.TOP!  Aborting...\n");
  224.         return;
  225.     }
  226.  
  227.     printf("Scanning USERS.TOP...     ");
  228.  
  229.     nu = filelength(fileno(fil)) / sizeof(user_data_typ);
  230.  
  231.     for (d = 0; d < nu; d++)
  232.     {
  233.         fseek(fil, (long) d * sizeof(user_data_typ), SEEK_SET);
  234.         fread(&ubuf, sizeof(user_data_typ), 1, fil);
  235.         strcpy(trn, ubuf.realname);
  236.         strcpy(tha, ubuf.handle);
  237.         strupr(trn);
  238.         strupr(tha);
  239.         if (strstr(trn, nam) || strstr(tha, nam))
  240.         {
  241.             printf("\b\b\b\b\b%5i", d);
  242.             printf("\nDo you mean %s (%s)? ", ubuf.realname, ubuf.handle);
  243.             key = toupper(getch());
  244.             if (key == 'Y')
  245.             {
  246.                 printf("Yes\n");
  247.                 strcpy(trn, ubuf.realname);
  248.                 memset(&ubuf, 0, sizeof(user_data_typ));
  249.                 fseek(fil, (long) d * sizeof(user_data_typ), SEEK_SET);
  250.                 fwrite(&ubuf, sizeof(user_data_typ), 1, fil);
  251.                 fclose(fil);
  252.                 printf("\"%s\" has been deleted!\n", trn);
  253.                 return;
  254.             }
  255.             printf("No\nScanning USERS.TOP...     ");
  256.         }
  257.         printf("\b\b\b\b\b%5i", d);
  258.     }
  259.  
  260.     fclose(fil);
  261.  
  262.     printf("\nScanning completed.\n");
  263.  
  264.     printf("\nDeletion completed.\n");
  265.  
  266. }
  267.  
  268. void packusers(void)
  269. {
  270.     XINT d, e = 0, n = 0;
  271.     XINT nu;
  272.     FILE *ifil, *ofil;
  273.  
  274.     printf("PACK - Removing blank user records from USERS.TOP...\n\n");
  275.  
  276.     ifil = fopen("USERS.TOP", "rb");
  277.     if (ifil == NULL)
  278.     {
  279.         printf("Can't open USERS.TOP!  Aborting...\n");
  280.         return;
  281.     }
  282.  
  283.     ofil = fopen("USERS.NEW", "wb");
  284.     if (ofil == NULL)
  285.     {
  286.         printf("Can't open USERS.NEW!  Aborting...\n");
  287.         return;
  288.     }
  289.  
  290.     printf("Scanning USERS.TOP...\n");
  291.  
  292.     nu = filelength(fileno(ifil)) / sizeof(user_data_typ);
  293.  
  294.     for (d = 0; d < nu; d++)
  295.     {
  296.         printf("%5i", d);
  297.         fseek(ifil, (long) d * sizeof(user_data_typ), SEEK_SET);
  298.         fread(&ubuf, sizeof(user_data_typ), 1, ifil);
  299.         if (ubuf.realname[0])
  300.         {
  301.             fseek(ofil, (long) e * sizeof(user_data_typ), SEEK_SET);
  302.             fwrite(&ubuf, sizeof(user_data_typ), 1, ofil);
  303.             e++;
  304.             printf("\r");
  305.         }
  306.         else
  307.         {
  308.             n++;
  309.             printf("\n");
  310.         }
  311.     }
  312.  
  313.     fclose(ifil);
  314.     fclose(ofil);
  315.  
  316.     printf("Scanning completed.\n");
  317.     printf("%i empty user records were removed.\n\n", n);
  318.     printf("Deleting USERS.TOP...\n");
  319.  
  320.     unlink("USERS.TOP");
  321.  
  322.     printf("Renaming USERS.NEW to USERS.TOP...\n");
  323.  
  324.     rename("USERS.NEW", "USERS.TOP");
  325.  
  326.     printf("\nPack completed.\n");
  327.  
  328. }
  329.  
  330. void purgeusers(unsigned XINT sec, unsigned XINT days)
  331. {
  332.     XINT d, n = 0;
  333.     XINT nu;
  334.     FILE *fil;
  335.  
  336.     if (sec == 0)
  337.     {
  338.         sec = 65535;
  339.     }
  340.  
  341.     printf("PURGE - Deleting users with security < %u who are idle at least %u days...\n\n",
  342.            sec, days);
  343.  
  344.     fil = fopen("USERS.TOP", "r+b");
  345.     if (fil == NULL)
  346.     {
  347.         printf("Can't open USERS.TOP!  Aborting...\n");
  348.         return;
  349.     }
  350.  
  351.     printf("Scanning USERS.TOP...\n");
  352.  
  353.     nu = filelength(fileno(fil)) / sizeof(user_data_typ);
  354.  
  355.     for (d = 0; d < nu; d++)
  356.     {
  357.         printf("\r%5i", d);
  358.         fseek(fil, (long) d * sizeof(user_data_typ), SEEK_SET);
  359.         fread(&ubuf, sizeof(user_data_typ), 1, fil);
  360.         if (!ubuf.realname[0])
  361.             {
  362.             continue;
  363.             }
  364.         if (ubuf.security < sec && calc_days_ago(&ubuf.last_use) >= days)
  365.         {
  366.             printf(" - Deleting %s (%s)...\n%5i", ubuf.realname,
  367.                    ubuf.handle, d);
  368.             memset(&ubuf, 0, sizeof(user_data_typ));
  369.             fseek(fil, (long) d * sizeof(user_data_typ), SEEK_SET);
  370.             fwrite(&ubuf, sizeof(user_data_typ), 1, fil);
  371.             n++;
  372.         }
  373.     }
  374.  
  375.     fclose(fil);
  376.  
  377.     printf("\nScanning completed.\n");
  378.     printf("%i users were deleted.\n\n", n);
  379.  
  380.     printf("Purge completed.\n");
  381.  
  382. }
  383.  
  384. void setusersec(unsigned XINT sec, char *nam)
  385. {
  386.     XINT d;
  387.     XINT nu;
  388.     FILE *fil;
  389.     char trn[51], tha[51];
  390.     XINT key;
  391.  
  392.     printf("SETSEC - Setting security of \"%s\" to %u...\n\n", nam, sec);
  393.  
  394.     strupr(nam);
  395.  
  396.     fil = fopen("USERS.TOP", "r+b");
  397.     if (fil == NULL)
  398.     {
  399.         printf("Can't open USERS.TOP!  Aborting...\n");
  400.         return;
  401.     }
  402.  
  403.     printf("Scanning USERS.TOP...     ");
  404.  
  405.     nu = filelength(fileno(fil)) / sizeof(user_data_typ);
  406.  
  407.     for (d = 0; d < nu; d++)
  408.     {
  409.         fseek(fil, (long) d * sizeof(user_data_typ), SEEK_SET);
  410.         fread(&ubuf, sizeof(user_data_typ), 1, fil);
  411.         strcpy(trn, ubuf.realname);
  412.         strcpy(tha, ubuf.handle);
  413.         strupr(trn);
  414.         strupr(tha);
  415.         if (strstr(trn, nam) || strstr(tha, nam))
  416.         {
  417.             printf("\b\b\b\b\b%5i", d);
  418.             printf("\nDo you mean %s (%s)? ", ubuf.realname, ubuf.handle);
  419.             key = toupper(getch());
  420.             if (key == 'Y')
  421.             {
  422.                 printf("Yes\n");
  423.                 ubuf.security = sec;
  424.                 fseek(fil, (long) d * sizeof(user_data_typ), SEEK_SET);
  425.                 fwrite(&ubuf, sizeof(user_data_typ), 1, fil);
  426.                 fclose(fil);
  427.                 printf("\"%s\" now has a security level of %u.\n",
  428.                        ubuf.realname, sec);
  429.                 return;
  430.             }
  431.             printf("No\nScanning USERS.TOP...     ");
  432.         }
  433.         printf("\b\b\b\b\b%5i", d);
  434.     }
  435.  
  436.     fclose(fil);
  437.  
  438.     printf("\nScanning completed.\n");
  439.  
  440.     printf("\nSetSec completed.\n");
  441.  
  442. }
  443.  
  444. XINT calc_days_ago(XDATE *lastdate)
  445. {
  446.     struct date thisdate;
  447.     long d1 = 0, d2 = 0;
  448.     long daysago = 0;
  449.     XINT d;
  450.  
  451.     getdate(&thisdate);
  452.  
  453.     for (d = 1; d < lastdate->da_year; d++)
  454.     {
  455.         d1 += 365;
  456.         if (d % 4 == 0)
  457.         {
  458.             d1++;
  459.         }
  460.     }
  461.     for (d = 1; d < lastdate->da_mon; d++)
  462.     {
  463.         switch(d)
  464.         {
  465.             case 1 : d1 += 31; break;
  466.             case 2 :
  467.                 d1 += 28;
  468.                 if (lastdate->da_year % 4 == 0)
  469.                 {
  470.                     d1++;
  471.                 }
  472.                 break;
  473.             case 3 : d1 += 31; break;
  474.             case 4 : d1 += 30; break;
  475.             case 5 : d1 += 31; break;
  476.             case 6 : d1 += 30; break;
  477.             case 7 : d1 += 31; break;
  478.             case 8 : d1 += 31; break;
  479.             case 9 : d1 += 30; break;
  480.             case 10: d1 += 31; break;
  481.             case 11: d1 += 30; break;
  482.         }
  483.     }
  484.     for (d = 1; d < lastdate->da_day; d++)
  485.     {
  486.         d1++;
  487.     }
  488.  
  489.     for (d = 1; d < thisdate.da_year; d++)
  490.     {
  491.         d2 += 365;
  492.         if (d % 4 == 0)
  493.         {
  494.             d2++;
  495.         }
  496.     }
  497.     for (d = 1; d < thisdate.da_mon; d++)
  498.     {
  499.         switch(d)
  500.         {
  501.             case 1 : d2 += 31; break;
  502.             case 2 :
  503.                 d2 += 28;
  504.                 if (thisdate.da_year % 4 == 0)
  505.                 {
  506.                     d2++;
  507.                 }
  508.                 break;
  509.             case 3 : d2 += 31; break;
  510.             case 4 : d2 += 30; break;
  511.             case 5 : d2 += 31; break;
  512.             case 6 : d2 += 30; break;
  513.             case 7 : d2 += 31; break;
  514.             case 8 : d2 += 31; break;
  515.             case 9 : d2 += 30; break;
  516.             case 10: d2 += 31; break;
  517.             case 11: d2 += 30; break;
  518.         }
  519.     }
  520.     for (d = 1; d < thisdate.da_day; d++)
  521.     {
  522.         d2++;
  523.     }
  524.  
  525.     daysago = d2 - d1;
  526.  
  527.     return daysago;
  528. }
  529.