home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / LFILE.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  20KB  |  515 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "lora_api.h"
  21.  
  22. CHAR Symbol = '>';
  23. USHORT Column = 1;
  24. class TConfig *Cfg;
  25.  
  26. VOID ExportFilesBBS (VOID)
  27. {
  28.    FILE *fp;
  29.    USHORT i;
  30.    CHAR Path[128], *p;
  31.    ULONG Total;
  32.    class TFileData *Data;
  33.    class TFileBase *File;
  34.  
  35.    printf (" * Export to FILES.BBS\r\n");
  36.  
  37.    if ((Data = new TFileData (Cfg->SystemPath)) != NULL) {
  38.       if (Data->First () == TRUE)
  39.          do {
  40.             Total = 0L;
  41.             printf (" +-- %-15.15s %-45.45s ", Data->Key, Data->Display);
  42.             if ((File = new TFileBase (Cfg->SystemPath, Data->Key)) != NULL) {
  43.                sprintf (Path, "%sfiles.bbs", Data->Download);
  44.                if ((fp = fopen (Path, "wt")) != NULL) {
  45.                   File->SortByName ();
  46.                   if (File->First () == TRUE)
  47.                      do {
  48.                         Total++;
  49.                         if ((p = (CHAR *)File->Description->First ()) == NULL)
  50.                            p = "";
  51.                         fprintf (fp, "%-12.12s (%3lu) %s\n", File->Name, File->DlTimes, p);
  52.                         while ((p = (CHAR *)File->Description->Next ()) != NULL) {
  53.                            for (i = 0; i < Column; i++)
  54.                               fputc (' ', fp);
  55.                            fprintf (fp, "%c%s\n", Symbol, p);
  56.                         }
  57.                      } while (File->Next () == TRUE);
  58.                   fclose (fp);
  59.                }
  60.                delete File;
  61.             }
  62.             printf ("Total: %5lu\r\n", Total);
  63.          } while (Data->Next () == TRUE);
  64.       delete Data;
  65.    }
  66. }
  67.  
  68. VOID ImportFilesBBS (VOID)
  69. {
  70.    FILE *fp;
  71.    USHORT PendingWrite;
  72.    ULONG Total;
  73.    CHAR Path[128], Temp[128], *p, *Name;
  74.    struct stat statbuf;
  75.    struct tm *ltm;
  76.    class TFileData *Data;
  77.    class TFileBase *File;
  78.  
  79.    printf (" * Import from FILES.BBS\r\n");
  80.  
  81.    unlink ("filebase.dat");
  82.    unlink ("filebase.idx");
  83.  
  84.    if ((Data = new TFileData (Cfg->SystemPath)) != NULL) {
  85.       if (Data->First () == TRUE)
  86.          do {
  87.             PendingWrite = FALSE;
  88.             Total = 0L;
  89.             printf (" +-- %-15.15s %-45.45s ", Data->Key, Data->Display);
  90.             if ((File = new TFileBase (Cfg->SystemPath, Data->Key)) != NULL) {
  91.                sprintf (Path, "%sfiles.bbs", Data->Download);
  92.                if ((fp = fopen (Path, "rt")) != NULL) {
  93.                   while (fgets (Temp, sizeof (Temp) - 1, fp) != NULL) {
  94.                      if ((p = strchr (Temp, 0x0A)) != NULL)
  95.                         *p = '\0';
  96.                      if (Temp[0] != ' ' && Temp[0] != 0x09) {
  97.                         if (PendingWrite == TRUE) {
  98.                            File->Add ();
  99.                            Total++;
  100.                            File->Clear ();
  101.                            PendingWrite = FALSE;
  102.                         }
  103.                         if ((Name = strtok (Temp, " ")) != NULL) {
  104.                            if ((p = strtok (NULL, "")) != NULL) {
  105.                               while (*p == ' ')
  106.                                  p++;
  107.                               if (*p == '(' || *p == '[') {
  108.                                  while (*p != ')' && *p != ']' && *p != '\0') {
  109.                                     if (isdigit (*p)) {
  110.                                        File->DlTimes *= 10;
  111.                                        File->DlTimes += *p - '0';
  112.                                     }
  113.                                     p++;
  114.                                  }
  115.                                  if (*p == ')' || *p == ']') {
  116.                                     p++;
  117.                                     while (*p == ' ')
  118.                                        p++;
  119.                                  }
  120.                               }
  121.                               if (*p != '\0')
  122.                                  File->Description->Add (p, (USHORT)(strlen (p) + 1));
  123.                            }
  124.                            sprintf (Path, "%s%s", Data->Download, Name);
  125. #if defined(__LINUX__)
  126.                            strlwr (Path);
  127. #endif
  128.                            if (!stat (Path, &statbuf)) {
  129.                               strcpy (File->Area, Data->Key);
  130.                               strcpy (File->Name, Name);
  131.                               sprintf (File->Complete, "%s%s", Data->Download, Name);
  132.                               File->Size = statbuf.st_size;
  133.                               ltm = localtime ((time_t *)&statbuf.st_mtime);
  134.                               File->UplDate.Day = File->Date.Day = (UCHAR)ltm->tm_mday;
  135.                               File->UplDate.Month = File->Date.Month = (UCHAR)(ltm->tm_mon + 1);
  136.                               File->UplDate.Year = File->Date.Year = (USHORT)(ltm->tm_year + 1900);
  137.                               File->UplDate.Hour = File->Date.Hour = (UCHAR)ltm->tm_hour;
  138.                               File->UplDate.Minute = File->Date.Minute = (UCHAR)ltm->tm_min;
  139.                               File->Uploader = "Sysop";
  140.                               File->CdRom = Data->CdRom;
  141.                               PendingWrite = TRUE;
  142.                            }
  143.                            else
  144.                               File->Clear ();
  145.                         }
  146.                      }
  147.                      else if (PendingWrite == TRUE) {
  148.                         p = Temp;
  149.                         while (*p == ' ' || *p == 0x09)
  150.                            p++;
  151.                         if (*p == '>' || *p == '|' || *p == '+') {
  152.                            p++;
  153.                            File->Description->Add (p);
  154.                         }
  155.                      }
  156.                   }
  157.                   fclose (fp);
  158.  
  159.                   if (PendingWrite == TRUE) {
  160.                      File->Add ();
  161.                      File->Clear ();
  162.                      Total++;
  163.                      PendingWrite = FALSE;
  164.                   }
  165.                }
  166.                else
  167.                   printf ("Error\r\n");
  168.  
  169.                File->Close ();
  170.                delete File;
  171.  
  172.                Data->ActiveFiles = Total;
  173.                Data->Update ();
  174.             }
  175.             printf ("Total: %5lu\r\n", Total);
  176.          } while (Data->Next () == TRUE);
  177.       delete Data;
  178.    }
  179. }
  180.  
  181. VOID PurgeFiles (USHORT DaysOld)
  182. {
  183.    ULONG Total, Deleted;
  184.    time_t Today, FileDate;
  185.    struct tm ltm;
  186.    class TFileData *Data;
  187.    class TFileBase *File;
  188.  
  189.    printf (" * Purging Files\r\n");
  190.    Today = time (NULL) / 86400L;
  191.  
  192.    if ((Data = new TFileData (Cfg->SystemPath)) != NULL) {
  193.       if (Data->First () == TRUE)
  194.          do {
  195.             Deleted = Total = 0L;
  196.             printf (" +-- %-15.15s %-29.29s ", Data->Key, Data->Display);
  197.             if ((File = new TFileBase (Cfg->SystemPath, Data->Key)) != NULL) {
  198.                if (File->First () == TRUE)
  199.                   do {
  200.                      Total++;
  201.  
  202.                      ltm.tm_mday = File->UplDate.Day;
  203.                      ltm.tm_mon = File->UplDate.Month - 1;
  204.                      ltm.tm_year = File->UplDate.Year - 1900;
  205.                      ltm.tm_hour = File->UplDate.Hour;
  206.                      ltm.tm_min = File->UplDate.Minute;
  207.                      ltm.tm_sec = 0;
  208.                      FileDate = mktime (<m) / 86400L;
  209.  
  210.                      if ((Today - FileDate) > DaysOld) {
  211.                         unlink (File->Complete);
  212.                         File->Delete ();
  213.                         Deleted++;
  214.                      }
  215.                   } while (File->Next () == TRUE);
  216.                delete File;
  217.             }
  218.             printf ("Total: %5lu, Deleted: %5lu\r\n", Total, Deleted);
  219.          } while (Data->Next () == TRUE);
  220.       delete Data;
  221.    }
  222. }
  223.  
  224. VOID PackFilebase (VOID)
  225. {
  226.    class TFileBase *File;
  227.  
  228.    printf (" * Packing (Compressing) Filebase\r\n");
  229.  
  230.    if ((File = new TFileBase (Cfg->SystemPath, "")) != NULL) {
  231.       File->Pack ();
  232.       delete File;
  233.    }
  234. }
  235.  
  236. VOID CreateFilesList (VOID)
  237. {
  238.    FILE *fp;
  239.    CHAR *p;
  240.    class TFileData *Data;
  241.    class TFileBase *File;
  242.  
  243.    printf (" * Creating List of Files (bbslist.txt)\r\n");
  244.  
  245.    if ((fp = _fsopen ("bbslist.txt", "wt", SH_DENYNO)) != NULL) {
  246.       if ((Data = new TFileData (Cfg->SystemPath)) != NULL) {
  247.          if (Data->First () == TRUE)
  248.             do {
  249.                printf (" +-- %-15.15s %-29.29s ", Data->Key, Data->Display);
  250.                if ((File = new TFileBase (Cfg->SystemPath, Data->Key)) != NULL) {
  251.                   File->SortByName ();
  252.                   if (File->First () == TRUE) {
  253.                      fprintf (fp, "\nLibrary: %s\n", Data->Key);
  254.                      fprintf (fp, "Description: %s\n", Data->Display);
  255.                      fprintf (fp, "There are %lu files available for download\n\n", Data->ActiveFiles);
  256.  
  257.                      fprintf (fp, "File Name    Size  Date  Description\n============ ===== ===== =====================================================\n");
  258.                      do {
  259.                         if ((p = (PSZ)File->Description->First ()) == NULL)
  260.                            p = "";
  261.                         fprintf (fp, "%-12.12s %4ldK %02d/%02d %.53s\n", File->Name, (File->Size + 1023) / 1024, File->UplDate.Month, File->UplDate.Year % 100, p);
  262.                         while ((p = (PSZ)File->Description->Next ()) != NULL)
  263.                            fprintf (fp, "                         %.53s\n", p);
  264.                      } while (File->Next () == TRUE);
  265.  
  266.                      fprintf (fp, "\n");
  267.                   }
  268.                   delete File;
  269.                }
  270.             } while (Data->Next () == TRUE);
  271.  
  272.          delete Data;
  273.       }
  274.       fclose (fp);
  275.    }
  276. }
  277.  
  278. VOID UpdateFilebase (USHORT KeepDate)
  279. {
  280.    FILE *fp;
  281.    DIR *dir;
  282.    ULONG Total, Added;
  283.    CHAR *p, Path[128], Temp[128];
  284.    time_t today;
  285.    struct stat statbuf;
  286.    struct tm *ltm;
  287.    struct dirent *ent;
  288.    class TFileData *Data;
  289.    class TFileBase *File;
  290.    class TPacker *Packer;
  291.  
  292.    printf (" * Updating filebase\r\n");
  293.  
  294.    unlink ("file_id.diz");
  295.    Packer = new TPacker (Cfg->SystemPath);
  296.  
  297.    if ((Data = new TFileData (Cfg->SystemPath)) != NULL) {
  298.       if (Data->First () == TRUE)
  299.          do {
  300.             Total = 0L;
  301.             Added = 0L;
  302.             cprintf (" +-- %-15.15s %-32.32s ", Data->Key, Data->Display);
  303.             if ((File = new TFileBase (Cfg->SystemPath, Data->Key)) != NULL) {
  304.                strcpy (Temp, Data->Download);
  305.                if (Temp[strlen (Temp) - 1] == '\\' || Temp[strlen (Temp) - 1] == '/')
  306.                   Temp[strlen (Temp) - 1] = '\0';
  307.  
  308.                if (File->First () == TRUE)
  309.                   do {
  310.                      sprintf (Path, "%s%s", Data->Download, File->Name);
  311.                      if (stat (AdjustPath (Path), &statbuf))
  312.                         File->Delete ();
  313.                   } while (File->Next () == TRUE);
  314.  
  315.                File->SortByName ();
  316.                if ((dir = opendir (AdjustPath (Temp))) != NULL) {
  317.                   while ((ent = readdir (dir)) != NULL) {
  318.                      if (!strcmp (ent->d_name, ".") || !strcmp (ent->d_name, ".."))
  319.                         continue;
  320.                      if (!stricmp (ent->d_name, "files.bbs") || !stricmp (ent->d_name, "descript.ion"))
  321.                         continue;
  322.                      if (File->Read (ent->d_name) == FALSE) {
  323.                         sprintf (Path, "%s%s", Data->Download, ent->d_name);
  324.                         if (!stat (AdjustPath (Path), &statbuf)) {
  325.                            File->Clear ();
  326.                            strcpy (File->Area, Data->Key);
  327.                            strcpy (File->Name, ent->d_name);
  328.                            strcpy (File->Complete, Path);
  329.                            File->Size = statbuf.st_size;
  330.                            ltm = localtime ((time_t *)&statbuf.st_mtime);
  331.                            File->Date.Day = (UCHAR)ltm->tm_mday;
  332.                            File->Date.Month = (UCHAR)(ltm->tm_mon + 1);
  333.                            File->Date.Year = (USHORT)(ltm->tm_year + 1900);
  334.                            File->Date.Hour = (UCHAR)ltm->tm_hour;
  335.                            File->Date.Minute = (UCHAR)ltm->tm_min;
  336.                            if (KeepDate == FALSE) {
  337.                               today = time (NULL);
  338.                               ltm = localtime (&today);
  339.                               File->UplDate.Day = (UCHAR)ltm->tm_mday;
  340.                               File->UplDate.Month = (UCHAR)(ltm->tm_mon + 1);
  341.                               File->UplDate.Year = (USHORT)(ltm->tm_year + 1900);
  342.                               File->UplDate.Hour = (UCHAR)ltm->tm_hour;
  343.                               File->UplDate.Minute = (UCHAR)ltm->tm_min;
  344.                            }
  345.                            else {
  346.                               File->UplDate.Day = File->Date.Day;
  347.                               File->UplDate.Month = File->Date.Month;
  348.                               File->UplDate.Year = File->Date.Year;
  349.                               File->UplDate.Hour = File->Date.Hour;
  350.                               File->UplDate.Minute = File->Date.Minute;
  351.                            }
  352.                            File->Uploader = "Sysop";
  353.                            File->CdRom = Data->CdRom;
  354.                            File->Description->Add ("Description missing");
  355.                            if (Packer != NULL) {
  356.                               if (Packer->CheckArc (Path) == TRUE) {
  357. #if defined(__LINUX__)
  358.                                  mkdir ("lfiletmp", 0666);
  359. #else
  360.                                  mkdir ("lfiletmp");
  361. #endif
  362.                                  if (strstr (Packer->UnpackCmd, "%3") == NULL && strstr (Packer->UnpackCmd, "%f") == NULL)
  363.                                     strcat (Packer->UnpackCmd, " %3");
  364.                                  Packer->DoUnpack (Path, "lfiletmp", "file_id.diz");
  365.                                  strcpy (Temp, "lfiletmp\\file_id.diz");
  366.                                  if (!stat (AdjustPath (Temp), &statbuf)) {
  367.                                     if ((fp = fopen (Temp, "rt")) != NULL) {
  368.                                        File->Description->Clear ();
  369.                                        while (fgets (Temp, sizeof (Temp) - 1, fp) != NULL) {
  370.                                           if ((p = strchr (Temp, '\n')) != NULL)
  371.                                              *p = '\0';
  372.                                           if ((p = strchr (Temp, '\r')) != NULL)
  373.                                              *p = '\0';
  374.                                           File->Description->Add (Temp);
  375.                                        }
  376.                                        fclose (fp);
  377.                                     }
  378.                                     strcpy (Temp, "lfiletmp\\file_id.diz");
  379.                                     unlink (AdjustPath (Temp));
  380.                                  }
  381.                                  rmdir ("lfiletmp");
  382.                               }
  383.                            }
  384.                            File->Add ();
  385.                            Added++;
  386.                         }
  387.                      }
  388.                      Total++;
  389.                   }
  390.                   closedir (dir);
  391.                }
  392.                delete File;
  393.             }
  394.             cprintf ("Total: %5lu Added: %5lu\r\n", Total, Added);
  395.          } while (Data->Next () == TRUE);
  396.       delete Data;
  397.    }
  398.  
  399.    if (Packer != NULL)
  400.       delete Packer;
  401. }
  402.  
  403. void main (int argc, char *argv[])
  404. {
  405.    int i;
  406.    USHORT Purge, Pack, Import, Export, DaysOld, List, Update;
  407.    USHORT KeepDate;
  408.    CHAR *Config = NULL;
  409.  
  410. #if !defined(__LINUX__)
  411.    _grow_handles (64);
  412. #endif
  413.  
  414.    Update = Purge = Pack = List = Import = Export = FALSE;
  415.    KeepDate = FALSE;
  416.    DaysOld = 65535U;
  417.  
  418.    cprintf ("\r\nLFILE; %s v%s - File maintenance utility\r\n", NAME, VERSION);
  419.    cprintf ("       Copyright (c) 1991-96 by Marco Maccaferri. All Rights Reserved.\r\n\r\n");
  420.  
  421. /*
  422.    if (ValidateKey ("bbs", NULL, NULL) == KEY_UNREGISTERED) {
  423.       cprintf ("* * *     WARNING: No license key found    * * *\r\n");
  424.       if ((i = CheckExpiration ()) == 0) {
  425.          cprintf ("* * *   This evaluation copy has expired   * * *\r\n\a\r\n");
  426.          exit (0);
  427.       }
  428.       else
  429.          cprintf ("* * * You have %2d days left for evaluation * * * \r\n\a\r\n", i);
  430.    }
  431. */
  432.  
  433.    if (argc <= 1) {
  434.       cprintf (" * Command-line parameters:\r\n\r\n");
  435.  
  436.       cprintf ("        -U[K]     Update FILEBASE\r\n");
  437.       cprintf ("                  K=Keep file date\r\n");
  438.       cprintf ("        -I        Import from FILES.BBS\r\n");
  439.       cprintf ("        -E        Export to FILES.BBS\r\n");
  440.       cprintf ("        -P[K]     Pack (compress) file base\r\n");
  441.       cprintf ("                  K=Purge\r\n");
  442.       cprintf ("        -K<d>     Purge files that are <d> days old\r\n");
  443.       cprintf ("        -L        Create a list of available files\r\n");
  444.       cprintf ("        -C<n>     Multiline descriptions begin at column <n>\r\n");
  445.       cprintf ("        -S<c>     Use <c> as the identifier of a multiline description\r\n");
  446.  
  447.       cprintf ("\r\n * Please refer to the documentation for a more complete command summary\r\n\r\n");
  448.    }
  449.    else {
  450.       for (i = 1; i < argc; i++) {
  451.          if (argv[i][0] == '-' || argv[i][0] == '/') {
  452.             switch (toupper (argv[i][1])) {
  453.                case 'C':
  454.                   Column = (USHORT)(atoi (&argv[i][2]));
  455.                   break;
  456.                case 'I':
  457.                   Import = TRUE;
  458.                   break;
  459.                case 'E':
  460.                   Export = TRUE;
  461.                   break;
  462.                case 'K':
  463.                   Purge = TRUE;
  464.                   DaysOld = (USHORT)atoi (&argv[i][1]);
  465.                   break;
  466.                case 'L':
  467.                   List = TRUE;
  468.                   break;
  469.                case 'P':
  470.                   Pack = TRUE;
  471.                   if (toupper (argv[i][2]) == 'K')
  472.                      Purge = TRUE;
  473.                   break;
  474.                case 'S':
  475.                   Symbol = argv[i][2];
  476.                   break;
  477.                case 'U':
  478.                   Update = TRUE;
  479.                   if (toupper (argv[i][2]) == 'K')
  480.                      KeepDate = TRUE;
  481.                   break;
  482.             }
  483.          }
  484.          else if (Config == NULL)
  485.             Config = argv[i];
  486.       }
  487.  
  488.       if ((Cfg = new TConfig) != NULL) {
  489.          if (Cfg->Load (Config, NULL) == FALSE)
  490.             Cfg->Default ();
  491.       }
  492.  
  493.       if (Update == TRUE)
  494.          UpdateFilebase (KeepDate);
  495.       if (Import == TRUE)
  496.          ImportFilesBBS ();
  497.       if (Purge == TRUE)
  498.          PurgeFiles (DaysOld);
  499.       if (Pack == TRUE)
  500.          PackFilebase ();
  501.       if (Export == TRUE)
  502.          ExportFilesBBS ();
  503.       if (List == TRUE)
  504.          CreateFilesList ();
  505.  
  506.       if (Import == TRUE || Export == TRUE || Purge == TRUE || Pack == TRUE || List == TRUE || Update == TRUE)
  507.          cprintf (" * Done\r\n\r\n");
  508.       else
  509.          cprintf (" * Nothing to do\r\n\r\n");
  510.  
  511.       if (Cfg != NULL)
  512.          delete Cfg;
  513.    }
  514. }
  515.