home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PCBOARD / PCBM102.ZIP / SOURCES.ZIP / PCBMOVE.C < prev    next >
C/C++ Source or Header  |  1994-01-22  |  37KB  |  1,165 lines

  1. /*
  2.  ┌────────────────────────────────────────────────────────────────────────┐
  3.  │                                                                        │
  4.  │    ▒▒▒▒▒▒▄ ▒▒▒▒▒▒▄ ▒▒▒▒▒▒▒▄ ▒▒▒▒▒▒▒▒▄ ▒▒▒▒▒▒▄ ▒▒▄      ▒▒▄ ▒▒▒▒▒▒▄     │
  5.  │    ▒▒█▀▒▒█ ▒▒█▀▀▀▀  ▒▒█▀▒▒█ ▒▒█▒▒█▒▒█ ▒▒█▀▒▒█  ▒▒█    ▒▒█▀ ▒▒█▀▀▀▀     │
  6.  │    ▒▒▒▒▒▒█ ▒▒█      ▒▒▒▒▒█▀ ▒▒█▒▒█▒▒█ ▒▒█ ▒▒█   ▒▒█  ▒▒█▀  ▒▒▒▒▄       │
  7.  │    ▒▒█▀▀▀▀ ▒▒█      ▒▒█▀▒▒█ ▒▒█ ▀▀▒▒█ ▒▒█ ▒▒█    ▒▒█▒▒█▀   ▒▒█▀▀       │
  8.  │    ▒▒█     ▒▒▒▒▒▒▄ ▒▒▒▒▒▒▒█ ▒▒█   ▒▒█ ▒▒▒▒▒▒█     ▒▒▒█▀    ▒▒▒▒▒▒▄     │
  9.  │     ▀▀      ▀▀▀▀▀▀  ▀▀▀▀▀▀▀  ▀▀    ▀▀  ▀▀▀▀▀▀      ▀▀▀      ▀▀▀▀▀▀     │
  10.  │                                                                        │
  11.  │      Module   :       PCBMOVE.C  - Module Principal -                  │
  12.  │                                                                        │
  13.  │                                                                        │
  14.  └────────────────────────────────────────────────────────────────────────┘
  15. */
  16.  
  17. /* My apologies to everyone, but comments in this code are in french... */
  18. /* However, most variables and functions names are "english", maybe that */
  19. /* will help you much more than comments :-) */
  20.  
  21. /* If something is _really_ uncrackable, just ask me !!! */
  22.  
  23. /* Enjoy */
  24.  
  25.  
  26. #include <ctype.h>
  27. #include <fcntl.h>
  28. #include <io.h>
  29. #include <malloc.h>
  30. #include <memory.h>
  31. #include <share.h>
  32. #include <stdio.h>
  33. #include <stdarg.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/locking.h>
  39. #include "install.h"
  40. #include "pcbmove.h"
  41. #include "mystring.h"
  42. #include "system.h"
  43. #include "strings.h"
  44. #include "pcbmove.ini"
  45.  
  46. /*------------- Seul endroit pour modifier le numéro de version -----*/
  47. /*                                                                   */
  48.                 char *__Version__  =  "1.02";
  49. /*                                                                   */
  50. /*-------------------------------------------------------------------*/
  51.  
  52. /*---------------------------------------------------------------------*/
  53.  
  54. char Program_Name [MAX_PATH];   /* Nom complet de l'executable */
  55. struct InstallType Install;
  56.  
  57. char **_Strings;
  58. char **_months_;
  59. char **_days_;
  60.  
  61. char config_file [MAX_PATH];    /* path du fichier de config passé en argument */
  62. char file_to_move [14];         /* nom du fichier à déplacer */
  63. int area_ori, area_tar;         /* numéro des areas d'origine et destination */
  64. int descript_length;            /* taille en octets de la description */
  65.  
  66. char fullname_ori[MAX_PATH], fullname_tar[MAX_PATH];
  67.                                 /* nom complet (fullpath) du fichier à bouger */
  68. char list_ori[MAX_PATH], list_tar[MAX_PATH];
  69.                                 /* nom complet des 2 file list à modifier */
  70. long offset_list_ori, offset_list_tar;
  71.                                 /* position dans le fichier des filelist ou */
  72.                                 /* doit etre ajouté ou effacé la description */
  73. char FromField[33];             /* Nom utilisateur qui a écrit à PCBMOVE */
  74. char DateField[9];
  75. char HeureField[6];             /* date et heure du message */
  76. int  AreaFound;                 /* Area où a été trouvé le fichier */
  77.  
  78. struct ConfigType Config;       /* structure du fichier de config */
  79. char tampon [99];               /* variable servant à faire des bidouilles */
  80. char tampon1[99];               /* sur des strings */
  81. char far *init;                 /* pointeur pour le malloc */
  82. unsigned bloc;                  /* taille du bloc de caractère lu dans le
  83.                                 /* fichier de description */
  84.                                 /* (c'est un peu merdique, mais ca marche) */
  85. main(int argc,char **argv)
  86. {
  87.    int i;
  88.  
  89.    _Strings = _StringsFRENCH;           /* au début, tout est en francais */
  90.    _days_   = _days_FRENCH;
  91.    _months_ = _months_FRENCH;
  92.  
  93.    if(*argv[1] == '?')                  /* affiche les errors level */
  94.    {
  95.       printf(_Strings[M_ErrorLevel]);
  96.       return(ERROR);
  97.    }
  98.  
  99.    if(_osmajor <3)                      /* si dos inf à 3, on sort */
  100.    {
  101.       printf(_Strings[M_BadDos]);
  102.       return(ERROR);
  103.    }
  104.    strcpy(Program_Name,argv[0]);  /*<<--^^--à cause de ça entre autres... */
  105.  
  106.    if( ReadExe() )           /*  Lit la date & le CRC dans l'exe */
  107.       return (-1 );
  108.  
  109.    printf (_Strings[M_CompilDate], __Version__, frtime(Install.CompilTime));
  110.  
  111.    /* affiche la version et la date de compilation */
  112.  
  113.    if (argc != 5 && argc != 2)  /* 4 arguments si en mode batch */
  114.    {                            /* 1 argument si mode scanmail. */
  115.       printf(_Strings[M_Usage]);
  116.       return(ERROR);
  117.    }
  118.  
  119.    i = argc;
  120.    while( i > 1 )              /* passe tous les caractères en MAJUSCULE */
  121.      strupr (argv[--i] );
  122.  
  123.    strcpy (config_file,   argv[1]); /* fichier de config */
  124.  
  125.    if(access (config_file,04) )     /* si peux pas acceder en lecture        */
  126.    {                                /* c'est qu'il y a un problème de config */
  127.       printf(_Strings[M_NoConfigFile], config_file);
  128.       return(ERROR);
  129.    }
  130.  
  131.    if(charge_config() == ERROR)     /* si peux pas acceder en lecture        */
  132.    {                                /* c'est qu'il y a un problème de config */
  133.       printf (_Strings[M_BadConfigFile], config_file);
  134.       return(ERROR);
  135.    }
  136.  
  137.    if (argc == 2){
  138.         return (scan_mail());                   /* mode scan mail */
  139.    } else {
  140.         strcpy (file_to_move, argv[2]);         /* fichier à déplacer   */
  141.         area_ori = atoi (argv[3]);              /* area d'origine       */
  142.         area_tar = atoi (argv[4]);              /* area de destination  */
  143.         if (area_ori == area_tar)
  144.            return ERROR;
  145.         strcpy (FromField, "LOCAL");
  146.         return (move());                        /* mode batch */
  147.    } /* fin if */
  148.  
  149. }
  150.  
  151. /*-----------------------------------------------------------------------*/
  152.  
  153. int move ()                     /* move proprement dit */
  154. {
  155.    int code_retour;
  156.  
  157.    bloc = 65000;
  158.  
  159.    if ((init = (char far *) malloc (bloc)) == NULL)
  160.    {
  161.      puterrorlog (_Strings[L_Malloc65KError]);
  162.      return ERROR;
  163.    }
  164.  
  165.    if (*Config.DebugFlag == ON)
  166.       printf (_Strings[M_CheckFileToMove]);
  167.  
  168.    code_retour = test_validite ();      /* test pour savoir si le move est */
  169.                                         /* possible et renseigne la */
  170.                                         /* variable offset_list_ori */
  171.    if (code_retour != OK) {free (init); return (code_retour);}
  172.  
  173.    if (*Config.DebugFlag == ON)
  174.       printf (_Strings[M_SeekDescript]);
  175.    where_to_put_tar (list_tar); /* trouve l'endroit où insérer le fichier    */
  176.                                 /* dans la liste et */
  177.                                 /* renseigne la variable offset_list_tar */
  178.    if (*Config.DebugFlag == ON)
  179.        printf (_Strings[M_OffsetsInFileList], offset_list_ori, offset_list_tar);
  180.  
  181.    if (*Config.DebugFlag == ON)
  182.        printf(_Strings[M_MAJinFileList]);
  183.    code_retour = maj_listes();          /* on créé les nouvelles filelist */
  184.  
  185.    if (code_retour != OK) {free (init); return (code_retour);}
  186.  
  187.    if (*Config.DebugFlag == ON)
  188.    {
  189.       printf (_Strings[M_ReplaceFileList], area_ori);
  190.       renbak (list_ori);        /* Si debug, on fait un .BAK de la filelist */
  191.    } else {
  192.       remove (list_ori);        /* sinon on efface l'ancien */
  193.    } /* fin if */
  194.  
  195.    code_retour = move_physique (Config.TempFile1, list_ori);
  196.    if (code_retour != OK) {free (init); return (code_retour);}
  197.  
  198.                         /* on remplace l'ancien filelist par la nouvelle */
  199.  
  200.    if (*Config.DebugFlag == ON)
  201.    {
  202.       printf (_Strings[M_ReplaceFileList], area_tar);
  203.       renbak (list_tar);        /* Si debug, on fait un .BAK de la filelist */
  204.    } else {
  205.       remove (list_tar);        /* sinon on efface l'ancien */
  206.    } /* fin if */
  207.  
  208.    code_retour = move_physique (Config.TempFile2, list_tar);
  209.    if (code_retour != OK) {free (init); return (code_retour);}
  210.  
  211.                         /* on remplace l'ancien filelist par la nouvelle */
  212.  
  213.    if (*Config.DebugFlag == ON)
  214.       printf (_Strings[M_PhysicalMove], file_to_move);
  215.  
  216.    code_retour = move_physique (fullname_ori, fullname_tar);
  217.  
  218.                                         /* move physique du fichier */
  219.    if (code_retour != OK)
  220.       return (code_retour);
  221.  
  222.    putuselog (_Strings[L_MoveOK], file_to_move, area_ori, area_tar, FromField);
  223.  
  224.                                         /* Ajout dans le fichier log */
  225.  
  226.    free (init);
  227.    return OK;
  228. }
  229. /*                       FIN DU PROGRAMME                                 */
  230. /*------------------------------------------------------------------------*/
  231.  
  232. /*------------------------------------------------------------------------*/
  233. /*
  234.  *  CHARGE_CONFIG :     Charge le fichier de configuration et met à jour.
  235.  *                      la structure Config en conséquence
  236.  *                      Retourne ERROR en cas de problème
  237.  *
  238.  *           Remarque : Il est indispensable de trouver TOUS les champs de
  239.  *                      config dans le fichier .cfg. En effet, on utilise la
  240.  *                      même structure pour le nom des champs est son contenu
  241.  *                      C'est pas très clair, mais c'est la raison d'être du
  242.  *                      fichier PCBMOVE.INI qui contient les chaines reconnues
  243.  *                      dans le .cfg
  244.  */
  245.  
  246. int charge_config ()
  247. {
  248.  
  249. FILE *fin;
  250. int  i;
  251. int  compt=0;           /* compte le nombre de ligne lu dans le fichier */
  252.  
  253.     if ((fin = fopen(config_file, "rt")) == NULL)
  254.         return ERROR;
  255.  
  256.     while (fgets (tampon, 90, fin) != NULL)
  257.     {
  258.         _Clean(tampon);                      /* on nettoie la string */
  259.         i = 0;
  260.         if (*tampon == '#')     continue;    /* si commentaire on continue */
  261.         if (strlen(tampon) < 3) continue;    /* Si ligne vide, on continue */
  262.         compt++;
  263.         while (tampon[++i] != '=')           /* on cherche le '='          */
  264.           if (tampon[i] == 0) { fclose (fin); return ERROR; }
  265.                                              /* si pas trouvé, ERROR */
  266.         tampon[i++] = 0 ;       /* on remplace le '=' par 0 et */
  267.                                 /* i pointe juste après ce 0   */
  268.  
  269.         if (!strcmp (tampon, Config.DirList)) /* on compare à tous les champs*/
  270.            strcpy (Config.DirList, tampon+i); /* de la structure Config */
  271.  
  272.         if (!strcmp (tampon, Config.MessageBase))
  273.            strcpy (Config.MessageBase, tampon+i);
  274.  
  275.         if (!strcmp (tampon, Config.UserBase))
  276.            strcpy (Config.UserBase, tampon+i);
  277.  
  278.         if (!strcmp (tampon, Config.UserFileNameError))
  279.            strcpy (Config.UserFileNameError, tampon+i);
  280.  
  281.         if (!strcmp (tampon, Config.UserAreaError))
  282.            strcpy (Config.UserAreaError, tampon+i);
  283.  
  284.         if (!strcmp (tampon, Config.UserOtherAreaError))
  285.            strcpy (Config.UserOtherAreaError, tampon+i);
  286.  
  287.         if (!strcmp (tampon, Config.UserAreaOriForbid))
  288.            strcpy (Config.UserAreaOriForbid, tampon+i);
  289.  
  290.         if (!strcmp (tampon, Config.UserAreaTarForbid))
  291.            strcpy (Config.UserAreaTarForbid, tampon+i);
  292.  
  293.         if (!strcmp (tampon, Config.UserNeedForward))
  294.            strcpy (Config.UserNeedForward, tampon+i);
  295.  
  296.         if (!strcmp (tampon, Config.ErrorLogFile))
  297.            strcpy (Config.ErrorLogFile, tampon+i);
  298.  
  299.         if (!strcmp (tampon, Config.UserLogFile))
  300.            strcpy (Config.UserLogFile, tampon+i);
  301.  
  302.         if (!strcmp (tampon, Config.UseLogFile))
  303.            strcpy (Config.UseLogFile, tampon+i);
  304.  
  305.         if (!strcmp (tampon, Config.TempFile1))
  306.            strcpy (Config.TempFile1, tampon+i);
  307.  
  308.         if (!strcmp (tampon, Config.TempFile2))
  309.            strcpy (Config.TempFile2, tampon+i);
  310.  
  311.         if (!strcmp (tampon, Config.ToField))
  312.            strcpy (Config.ToField, tampon+i);
  313.  
  314.         if (!strcmp (tampon, Config.ForwardUser))
  315.            strcpy (Config.ForwardUser, strupr(tampon+i));
  316.  
  317.         if (!strcmp (tampon, Config.Security))
  318.            strcpy (Config.Security, tampon+i);
  319.  
  320.         if (!strcmp (tampon, Config.NotAreaOri))
  321.            Chaine2int (tampon+i, Config.NotAreaOri);
  322.  
  323.         if (!strcmp (tampon, Config.NotAreaTar))
  324.            Chaine2int (tampon+i, Config.NotAreaTar);
  325.  
  326.         if (!strcmp (tampon, Config.DebugFlag))
  327.            if (!strcmp (strupr(tampon+i), "ON"))
  328.                 *Config.DebugFlag = ON;
  329.            else *Config.DebugFlag = OFF;
  330.  
  331.         if (!strcmp (tampon, Config.Language))
  332.            strcpy (Config.Language, tampon+i);
  333.  
  334.     } /* fin while */
  335.  
  336.     fclose (fin);
  337.  
  338.     if (!strcmp (strupr (Config.Language), "FRENCH"))
  339.     {
  340.        _Strings = _StringsFRENCH;       /* on fait pointer _Strings vers les */
  341.        _days_   = _days_FRENCH;         /* chaines françaises ou anglaises   */
  342.        _months_ = _months_FRENCH;
  343.     } else {
  344.        _Strings = _StringsUS;
  345.        _months_ = _months_US;
  346.        _days_   = _days_US;
  347.     } /* fin if */
  348.  
  349.     if (compt == 21)                    /* Si y'a pas 21 champs dans le */
  350.        return OK;                       /* le fichier de config, c'est qu'il */
  351.     else                                /* y a un problème */
  352.        return ERROR;
  353. }
  354. /*------------------------------------------------------------------------ */
  355. /*
  356.  *  READEXE : Lit l'executable, remplie la structure installe
  357.  *            et vérifie par CRC que rien n'a été patché
  358.  */
  359.  
  360. int ReadExe (void)
  361. {
  362.     int fin;
  363.     long taille;
  364.     int crc;
  365.  
  366.     if((fin = open(Program_Name,O_RDWR|O_BINARY,S_IREAD|S_IWRITE)) == -1)
  367.      {
  368.         printf (_Strings[M_CanNotReadExe]);
  369.         return ERROR;                      /* mais c'est peu probable...*/
  370.      }
  371.     taille= filelength(fin);
  372.  
  373.     lseek(fin,taille - (long)sizeof(Install),SEEK_SET);
  374.                                         /* on se positionne n octets avant */
  375.                                         /* la fin du fichier */
  376.     read(fin,(char *)&Install.CrcTest,sizeof(Install));
  377.     close(fin);
  378.  
  379.     crc = (int) calcrc((char *)&Install.StruVer,sizeof(Install) -2);
  380.  
  381.     if((int) Install.CrcTest != crc )
  382.     {                                   /* on vérifie le CRC */
  383.       printf(_Strings[M_ExeHasBeenPatched], Program_Name);
  384.       return (ERROR);
  385.     }
  386.  
  387.     return (OK);
  388. }
  389.  
  390. /*------------------------------------------------------------------------*/
  391. /*
  392.  *  PUTLOG  : Ecrit un message formatté dans le fichier log.
  393.  *            Retourne un pointeur sur la chaine (statique)
  394.  *            écrite, dans tous les cas.
  395.  *            L'écriture ne doit pas dépasser 1024 octets, mais 56 octets
  396.  *            sera un maximum pour une présentation propre.
  397.  *
  398.  *            La date d'écriture est notée avant le message.
  399.  */
  400.  
  401. void puterrorlog(char *format,...)
  402. {
  403.  va_list ap;                            /* pour les paramètres variables */
  404.  FILE *fin;
  405.  char Buffer[255];
  406.  long t ;
  407.  
  408.   va_start (ap,format);         /* trouve le premier argument variable */
  409.   vsprintf (Buffer,format,ap);  /* met dans buffer la chaine du log    */
  410.   va_end (ap);
  411.  
  412.   time ( &t );
  413.   if(( fin = fopen(Config.ErrorLogFile,"a") ) != NULL )
  414.  
  415.   {                             /* rajoute la date */
  416.  
  417.         fprintf(fin,"■ %s : %-80s ■\n", sfrtime ( t ),Buffer );
  418.         printf("■ %s : %-80s ■\n", sfrtime ( t ),Buffer );
  419.         fclose(fin);
  420.   }
  421.  
  422.  return;
  423. }
  424.  
  425. /*------------------------------------------------------------------------*/
  426. void putuserlog(char *format,...)
  427. {
  428.  va_list ap;
  429.  FILE *fin;
  430.  char Buffer[255];
  431.  long t ;
  432.  
  433.   va_start (ap,format);         /* trouve le premier argument variable */
  434.   vsprintf (Buffer,format,ap);  /* met dans buffer la chaine du log    */
  435.   va_end (ap);
  436.  
  437.   time ( &t );
  438.   if(( fin = fopen(Config.UserLogFile,"a") ) != NULL )
  439.   {
  440.         fprintf(fin,"■ %s : %-80s ■\n", sfrtime ( t ),Buffer );
  441.         if (*Config.DebugFlag == ON)
  442.            printf("■ %s : %-80s ■\n", sfrtime ( t ),Buffer );
  443.         fclose(fin);
  444.   }
  445.  
  446.  return;
  447. }
  448.  
  449. /*------------------------------------------------------------------------*/
  450. void putuselog(char *format,...)
  451. {
  452.  va_list ap;
  453.  FILE *fin;
  454.  char Buffer[255];
  455.  long t ;
  456.  
  457.   va_start (ap,format);         /* trouve le premier argument variable */
  458.   vsprintf (Buffer,format,ap);  /* met dans buffer la chaine du log    */
  459.   va_end (ap);
  460.  
  461.   time ( &t );
  462.   if(( fin = fopen(Config.UseLogFile,"a") ) != NULL )
  463.   {
  464.         fprintf(fin,"■ %s : %-80s ■\n", sfrtime ( t ),Buffer );
  465.         if (*Config.DebugFlag == ON)
  466.            printf("■ %s : %-80s ■\n", sfrtime ( t ),Buffer );
  467.         fclose(fin);
  468.   }
  469.  
  470.  return;
  471. }
  472.  
  473. /*------------------------------------------------------------------------*/
  474. /* TEST_VALIDITE :
  475.  *      . Vérifie que les areas ne sont pas interdites (origine et destination)
  476.  *      . Construit tous les chemins (file lists et fichier à bouger).
  477.  *      . Vérifie l'existence physique du fichier.
  478.  *      . Vérifie l'existence du répertoire de destination.
  479.  *      . Vérifie l'existence des fichiers de description.
  480.  *      . Vérifie l'existence du fichier dans la description de l'area
  481.  */
  482.  
  483. int test_validite (void)
  484. {
  485.   int code_retour;
  486.   unsigned i;
  487.   unsigned max_area;
  488.  
  489.   max_area = getmaxarea ();
  490.  
  491.   for (i=0;i<=max_area ;i++ ){
  492.       if (Config.NotAreaOri[i] == area_ori)
  493.       {
  494.          putuserlog (_Strings[L_FromForbidenMove], area_ori, FromField);
  495.          return ORIFORBIDEN;
  496.       } /* fin if */
  497.       if (Config.NotAreaTar[i] == area_tar)
  498.       {
  499.          putuserlog (_Strings[L_ToForbidenMove], area_tar, FromField);
  500.          return TARFORBIDEN;
  501.       } /* fin if */
  502.   } /* fin for */
  503.  
  504.   code_retour = getfilelist(area_ori);  /* met dans tampon le nom complet */
  505.   if (code_retour != OK)   return code_retour;
  506.  
  507.   strcpy (list_ori, tampon);
  508.  
  509.   code_retour = getfilelist(area_tar);  /* met dans tampon le nom complet */
  510.   if (code_retour != OK)   return code_retour;
  511.  
  512.   strcpy (list_tar, tampon);
  513.  
  514.   code_retour = getfilename (area_ori);      /* retourne le nom complet d' */
  515.   if (code_retour != OK) return code_retour; /* origine du fichier à déplacer*/
  516.  
  517.   sprintf (fullname_ori, "%s%s", tampon, file_to_move);
  518.  
  519.   code_retour = getfilename (area_tar);      /* retourne le nom complet de desti- */
  520.   if (code_retour != OK) return code_retour; /* nation du  fichier à déplacer */
  521.  
  522.   sprintf (fullname_tar, "%s%s", tampon, file_to_move);
  523.  
  524.   strcpy (tampon, fullname_tar);
  525.   *strrchr (tampon, '\\') = 0;
  526.  
  527.   if(access (tampon,00))     /* le répertoire de destination n'existe */
  528.    {                         /* pas. y'a un schmilblic. On arrete là  */
  529.       puterrorlog (_Strings[L_TargetDirNotExist], tampon);
  530.       return ERROR;
  531.    }
  532.  
  533.   if(access (list_ori,06))          /* si peux pas acceder en lecture        */
  534.   {                                 /* et écriture, c'est que la filelist    */
  535.                                     /* d'origine a un problème */
  536.       puterrorlog (_Strings[L_CanNotOpenReadMode], list_ori);
  537.       return ERROR;
  538.   }
  539.  
  540.   if (access (list_tar,06))         /* si peux pas acceder en lecture        */
  541.   {                                 /* et écriture, c'est que la filelist    */
  542.                                     /* de destination a un problème */
  543.       puterrorlog (_Strings[L_CanNotOpenReadMode], list_tar);
  544.       return ERROR;
  545.   }
  546.  
  547.   code_retour = is_file_in_list (list_ori);
  548.  
  549.   if (code_retour == NON) {
  550.      /* le fichier n'est pas dans la liste de l'area spécifiée */
  551.      /* on pourra plus tard, chercher s'il est dans d'autres AREA */
  552.      putuserlog (_Strings[L_FileNotInList], file_to_move, area_ori, FromField);
  553.      return NOTINLIST;
  554.      }
  555.  
  556.   if (code_retour == ERROR) {
  557.      /* y'a un problème sérieux d'acces aux fichiers */
  558.      /* on a mis l'info dans le fichier LOG des erreur */
  559.      return ERROR;
  560.      }
  561.  
  562.   if(access (fullname_ori,06))      /* si peux pas acceder en lecture        */
  563.    {                                /* et écriture, c'est qu'on ne peut pas  */
  564.                                     /* le déplacer */
  565.       puterrorlog (_Strings[L_FileNotPhysicExist], fullname_ori);
  566.       return ERROR;
  567.    }
  568.  
  569.   return OK;
  570.  
  571. }
  572.  
  573. /*------------------------------------------------------------------------*/
  574. /*      GETFILENAME : met dans tampon le dir du fichier à déplacer.
  575.  *                    Le path est construit grace au DIR.LST d'après le numéro
  576.  *                    de l'area passé en argument
  577.  */
  578.  
  579. int getfilename (int area)
  580. {
  581.  
  582.   FILE *fin;
  583.   int i=0;
  584.  
  585.   if ((fin = _fsopen (Config.DirList, "rb", SH_DENYNO )) == NULL)
  586.   {
  587.      puterrorlog (_Strings[L_CanNotOpenReadMode], Config.DirList);
  588.      return ERROR;
  589.   }
  590.   do {
  591.  
  592.     i++;
  593.     if ( fgets (tampon, 97, fin) == NULL)
  594.     {
  595.        putuserlog (_Strings[L_AreaNotExist], area, FromField);
  596.        fclose (fin);
  597.        return AREANOTEXIST;
  598.     }
  599.   } while (i != area  ); /* fin do */
  600.   tampon[60] = 0;
  601.  
  602.   strcpy (tampon1, tampon+30);
  603.   strcpy (tampon, strclean(tampon1));
  604.   _Clean (tampon);
  605.  
  606.   fclose (fin);
  607.   return OK;
  608. }
  609.  
  610. /*------------------------------------------------------------------------*/
  611. /*      GETFILELIST : met dans tampon le nom du fichier de la filelist.
  612.  *                    Le path est construit grace au DIR.LST d'après le numéro
  613.  *                    de l'area passé en argument
  614.  */
  615.  
  616. int getfilelist (int area)
  617. {
  618.  
  619.   FILE *fin;
  620.   int i=0;
  621.  
  622.   if ((fin = _fsopen (Config.DirList, "rt", SH_DENYNO)) == NULL)
  623.   {
  624.      puterrorlog (_Strings[L_CanNotOpenReadMode], Config.DirList);
  625.      return ERROR;
  626.   }
  627.   do {
  628.  
  629.     i++;
  630.     fgets (tampon, 97, fin);
  631.  
  632.   } while (i != area  ); /* fin do */
  633.   tampon[30] = 0;
  634.  
  635.   strclean(tampon);
  636.   _Clean (tampon);
  637.  
  638.   fclose (fin);
  639.   return OK;
  640.  
  641. }
  642.  
  643. /*------------------------------------------------------------------------*/
  644. /*      GETAREANAME : met dans tampon la description de l'area spécifiée
  645.  */
  646.  
  647. int getareaname (int area)
  648. {
  649.  
  650.   FILE *fin;
  651.   int i=0;
  652.  
  653.   if ((fin = _fsopen (Config.DirList, "rb", SH_DENYNO)) == NULL)
  654.   {
  655.      puterrorlog (_Strings[L_CanNotOpenReadMode], Config.DirList);
  656.      return ERROR;
  657.   }
  658.   do {
  659.  
  660.     i++;
  661.     fgets(tampon, 97, fin);
  662.   } while (i != area  ); /* fin do */
  663.   tampon[95] = 0;
  664.  
  665.   strcpy (tampon1, tampon+60);
  666.   strcpy (tampon, strclean(tampon1));
  667.   _Clean (tampon);
  668.  
  669.   fclose (fin);
  670.   return OK;
  671. }
  672. /*------------------------------------------------------------------------*
  673.  * IS_FILE_IN_LIST : Cherche le fichier file_to_move dans le fichier de
  674.  *                   description dont le nom est passé en argument.
  675.  *
  676.  *                   Si trouvé, retourne OUI et renseigne offset_list_ori
  677.  *                   qui servira à enlevé la description
  678.  *                   Si pas trouvé, retourne NON.
  679.  */
  680.  
  681. int is_file_in_list (char *file)
  682. {
  683.   int fin;
  684.   unsigned compteur=0;
  685.   int TROUVE = ERROR;
  686.   unsigned i;
  687.   char far *fptr = init;
  688.   int len_filename;
  689.  
  690.   len_filename = strlen(file_to_move);
  691.   bloc = 65000;
  692.   if ((fin = sopen (file, O_RDONLY | O_BINARY, SH_DENYNO)) == - 1)
  693.   {
  694.      puterrorlog (_Strings[L_CanNotOpenReadMode], file);
  695.      return ERROR;
  696.   }
  697.  
  698.   _fmemset (init, 0, bloc);
  699.  
  700.   do {
  701.  
  702.     if (read(fin, fptr, bloc) == 0) break;
  703.     bloc = 64900;               /* on a lu 65000 car */
  704.     fptr = init;                /* pour prévoir le cas ou le nom du fichier */
  705.                                 /* est coupé au milieu d'un bloc, on ne considère */
  706.                                 /* donc que 64900 caractères . Les 100 restants */
  707.                                 /* permettent de faire le lien */
  708.     for (i=0 ;i < 64915 ;fptr++, i++){
  709.         if (*fptr == 10)        /* si retour chariot, on cherche + loin */
  710.         {
  711.            i++;
  712.            if (*(++fptr) != *file_to_move) continue;    /* si première lettre */
  713.                                                         /* pas bon, on passe */
  714.            *(fptr+len_filename) = 0;
  715.            if (!strcmp (fptr, file_to_move)) {TROUVE = OK; break;}
  716.                                 /* si trouvé, on sort */
  717.         } /* fin if */
  718.     } /* fin for */
  719.  
  720.  
  721.     if (TROUVE == OK){
  722.        offset_list_ori = i + (long)compteur * 64900;    /* on calcule l'offset */
  723.        close (fin);                                     /* du fichier */
  724.        return OUI;
  725.     } /* fin if */
  726.  
  727.     _fmemcpy (init, fptr-15, 100);  /* on enleve 15 au cas ou le nom */
  728.                                     /* du fichier soit coupé en fin */
  729.                                     /* de malloc, et on recopie les 100 */
  730.     compteur++;
  731.     fptr = init+100;
  732.     _fmemset (fptr, 0, (size_t) 64900);
  733.  
  734.   } while (1); /* fin do */
  735.  
  736.   close (fin);
  737.   return NON;
  738.  
  739. }
  740. /*------------------------------------------------------------------------
  741.  * WHERE_TO_PUT_TAR: Cherche l'endroit où il faudra insérer la description.
  742.  *                   Pour cela il renseigne offset_list_tar.
  743.  *
  744.  */
  745. void where_to_put_tar (char *file)
  746. {
  747.  
  748.   int fin;
  749.   int code_retour, avant = -1;
  750.   unsigned compteur=0;
  751.   int TROUVE = ERROR;
  752.   register unsigned i;
  753.   char far *fptr = init;
  754.   int fin_fichier = NON;
  755.  
  756.   bloc = 65000;
  757.  
  758.   fin = sopen (file, O_RDONLY | O_BINARY, SH_DENYNO);
  759.  
  760.   _fmemset (init, 0, bloc);
  761.  
  762.   do {
  763.  
  764.     if (read(fin, fptr, bloc) == 0) break;
  765.     bloc = 64900;
  766.     fptr = init;
  767.  
  768.     for (i=0 ;i < 64915 ;fptr++, i++){
  769.  
  770.         if (*fptr == 10 && !isspace (*(fptr+1))){
  771.  
  772.            i++; fptr++;
  773.            *(fptr+12) = 0;      /* on coupe la string apres le nom */
  774.            code_retour = strcmp (fptr, file_to_move);
  775.            if (code_retour + avant == 0) {TROUVE = OK; break;}
  776.            avant = code_retour;
  777.            if (*(fptr+13) == 0){/* on est arrive au bout fichier */
  778.               fin_fichier = OUI;/* on met le flag de dépassement pour ajout */
  779.               TROUVE = OK;
  780.               break;
  781.            } /* fin if */
  782.  
  783.         } /* fin if */
  784.     } /* fin for */
  785.  
  786.  
  787.     if (TROUVE == OK){
  788.        if (fin_fichier == NON){
  789.            offset_list_tar = i + (long)compteur * 64900;
  790.        } else {
  791.            offset_list_tar = filelength (fin);
  792.        } /* fin if */
  793.        close (fin);
  794.        return;
  795.     } /* fin if */
  796.  
  797.     _fmemcpy (init, fptr-15, 100);  /* on enleve 15 au cas ou  */
  798.                                     /* l'endroit qui nous intéresse */
  799.                                     /* est coupé entre les 2 malloc */
  800.     fptr = init+100;
  801.     _fmemset (fptr, 0, (size_t) 64900);
  802.     compteur++;
  803.  
  804.   } while (1); /* fin do */
  805.  
  806.   close (fin);
  807.   return;
  808. }
  809. /*------------------------------------------------------------------------
  810.  * MAJ_LISTES : Fait les modifs des 2 filelist et sauve le résultat dans
  811.  *              des fichiers temporaires.
  812.  *
  813.  */
  814. int maj_listes (void)
  815. {
  816.  
  817.   int fin;
  818.   int fout;
  819.   char far *init1;
  820.   char far *fptr = init;
  821.   long local_offset = 0;
  822.   int i;
  823.  
  824.   bloc = 65000;
  825.  
  826.   if ((fin = sopen (list_ori, O_RDONLY | O_BINARY, SH_DENYWR )) == - 1)
  827.   {
  828.      puterrorlog (_Strings[L_CanNotOpenReadMode], list_ori);
  829.      return ERROR;
  830.   }
  831.  
  832.   locking (fin, LK_LOCK, filelength (fin));
  833.  
  834.   if ((fout = open (Config.TempFile1, O_BINARY | O_CREAT | O_WRONLY, S_IWRITE))
  835.             == - 1)
  836.   {
  837.      puterrorlog (_Strings[L_CanNotCreateTempo], Config.TempFile1);
  838.      return ERROR;
  839.   }
  840.  
  841.   if ((init1 = (char far *) malloc (2020)) == NULL)
  842.   {                     /* malloc pour stocker la description */
  843.      puterrorlog (_Strings[L_Malloc2KError]);
  844.      close (fin);
  845.      close (fout);
  846.      return ERROR;
  847.   }
  848.  
  849.   do {
  850.  
  851.     if (offset_list_ori <= local_offset+bloc)
  852.         bloc = offset_list_ori - local_offset;
  853.                                 /* si on peut lire toutes les descriptions */
  854.                                 /* d'un coup arriver à celle voulue,       */
  855.                                 /* on diminue le read pour arriver         */
  856.                                 /* "au ras" */
  857.     read(fin, init, bloc);
  858.     write (fout, init, bloc);   /* écrit dans le fichier temporaire */
  859.     local_offset += bloc;       /* local_offset est la position actuelle dans */
  860.                                 /* le fichier */
  861.   } while (offset_list_ori != local_offset); /* fin do */
  862.         /* boucle qui lit toute les descriptions avant celle recherchée */
  863.   read(fin, init, bloc = 65000);
  864.                                         /* on lit un nouveau bloc */
  865.   _fmemcpy (init1, init, 2000);
  866.                                         /* copie 2000K pour garder la description */
  867.   for (i=0;i < 2000 ;i++ )
  868.   {
  869.       if (*(init1+i) == 10 && *(init1+i+1) != ' ')
  870.       {                                 /* on detecte la fin de description */
  871.          descript_length = ++i;         /* dès qu'il n'y a plus un CR suivi */
  872.          break;                         /* d'un blanc */
  873.       }
  874.   }
  875.  
  876.   if (i == 2000)
  877.   {
  878.      puterrorlog (_Strings[L_DescriptTooLong], file_to_move);
  879.      _ffree (init1);
  880.      close (fin);
  881.      close (fout);
  882.      return ERROR;
  883.   }
  884.  
  885.   bloc -= descript_length;              /* shift de la mémoire pour virer */
  886.   local_offset += descript_length;      /* la description qu'on ne veut pas */
  887.   _fmemmove (init, init+descript_length, bloc);
  888.  
  889.   do {
  890.       if (filelength (fin) <= local_offset+bloc)
  891.       {                           /* si c'est le dernier morceau de 65000 */
  892.          if (local_offset < filelength(fin)){
  893.             bloc = (unsigned) (filelength(fin) - local_offset);
  894.             write (fout, init, bloc);
  895.          } /* fin if */
  896.          close (fin);
  897.          close (fout);
  898.          break;
  899.       } /* fin if */
  900.  
  901.       write (fout, init, bloc);
  902.       local_offset += bloc;
  903.       bloc = 65000;
  904.       read (fin, init, bloc);
  905.  
  906.   } while (1); /* fin do */
  907.  
  908. /* ******* 1ere partie ***************
  909. La description a été enlevé du fichier d'origine
  910. ************************************** */
  911.  
  912.   if ((fin = open (list_tar, O_RDONLY | O_BINARY, SH_DENYWR)) == - 1)
  913.   {
  914.      puterrorlog (_Strings[L_CanNotOpenReadMode], list_tar);
  915.      return ERROR;
  916.   }
  917.  
  918.   locking (fin, LK_LOCK, filelength (fin));
  919.  
  920.   if ((fout = open (Config.TempFile2, O_BINARY | O_CREAT | O_WRONLY, S_IWRITE))
  921.             == - 1)
  922.   {
  923.      puterrorlog (_Strings[L_CanNotCreateTempo], Config.TempFile2);
  924.      return ERROR;
  925.   }
  926.  
  927.   local_offset = 0;             /* RAZ Variables */
  928.   bloc = 65000;
  929.  
  930.   do {
  931.  
  932.     if (offset_list_tar <= local_offset+bloc)
  933.         bloc = offset_list_tar - local_offset;
  934.     read(fin, init, bloc);      /* si on peut lire toutes les descriptions */
  935.                                 /* d'un coup arriver à celle voulue,       */
  936.                                 /* on diminue le read pour arriver         */
  937.                                 /* "au ras" */
  938.     write (fout, init, bloc);
  939.     local_offset += bloc;
  940.   } while (offset_list_tar != local_offset); /* fin do */
  941.  
  942.   bloc = 65000;
  943.  
  944.   _fmemcpy (init, init1, descript_length);      /* on ajoute la desciption */
  945.  
  946.   if (filelength (fin) - local_offset < bloc - descript_length) {
  947.                         /* si on a la place de mettre la fin du fichier */
  948.         bloc = filelength (fin) -local_offset;
  949.         read (fin, init+descript_length, bloc);
  950.                                         /* on lit le reste du fichier */
  951.         write (fout, init, bloc + descript_length);
  952.         goto fin;                       /* a ke C pas bo ça :-) */
  953.   } else {
  954.         read (fin, init+descript_length, bloc-descript_length);
  955.  
  956.         local_offset += bloc-descript_length; /* on a pas lu 65000 car, mais 65000-*/
  957.                                               /* la description ajoutée */
  958.         write (fout, init, bloc);
  959.   } /* fin if  */
  960.  
  961.   read (fin, init, bloc);
  962.                         /* on arrive là pour écrire tout la fin du fichier */
  963.   do {
  964.       if (filelength (fin) <= local_offset+bloc)
  965.       {
  966.          bloc = (unsigned) (filelength(fin) - local_offset);
  967.          write (fout, init, bloc);
  968.          break;
  969.       } /* fin if */
  970.  
  971.       write (fout, init, bloc);
  972.       local_offset += bloc;
  973.       bloc = 65000;
  974.       read (fin, init, bloc);
  975.  
  976.   } while (1); /* fin do */
  977.  
  978. /* ********* 2eme partie *************
  979. La description a été ajoutée dans le fichier de destination.
  980.    *********************************** */
  981.  
  982.   fin:                          /* he oui .... ya un goto un peu plus haut */
  983.   close (fin);
  984.   close (fout);
  985.   _ffree (init1);
  986.   return OK;
  987. }
  988. /*------------------------------------------------------------------------
  989.  * MOVE_PHYSIQUE : Déplace un fichier selon 2 méthodes :
  990.  *      . Si c'est sur le même disque, on fait appel au MOVE du dos.
  991.  *      . Si c'est dur des disques différents, on copie le fichier puis
  992.  *        efface l'original.
  993.  */
  994. int move_physique (char *file1, char *file2)
  995. {
  996.   long offset = 0;
  997.   long taille;
  998.   int fin, fout;
  999.   char far *ptr;                 /* pointeur pour le malloc */
  1000.  
  1001.   if (*file1 == *file2)   /* on peut seulement faire un MOVE */
  1002.   {                       /* car le disque est le même       */
  1003.  
  1004.       if (*Config.DebugFlag == ON)
  1005.          printf ("move par rename\n");
  1006.       return (rename (file1, file2));
  1007.   } else {
  1008.  
  1009.       if (*Config.DebugFlag == ON)
  1010.          printf ("move physique\n");
  1011.  
  1012.       if ((fin = sopen (file1, O_RDONLY | O_BINARY, SH_DENYRW)) == - 1)
  1013.       {
  1014.          puterrorlog (_Strings[L_CanNotOpenForMove], file1);
  1015.          return ERROR;
  1016.       }
  1017.  
  1018.       locking (fin, LK_LOCK, filelength (fin));
  1019.  
  1020.       if ((fout = open (file2, O_BINARY | O_CREAT | O_WRONLY, S_IWRITE))
  1021.                 == - 1)
  1022.       {
  1023.          puterrorlog (_Strings[L_CanNotOpenForMove], file2);
  1024.          close (fin);
  1025.          return ERROR;
  1026.       }
  1027.  
  1028.       taille = filelength (fin);
  1029.       bloc = 65000;
  1030.  
  1031.       if ((ptr = (char far *) malloc (bloc)) == NULL)
  1032.       {
  1033.         puterrorlog (_Strings[L_Malloc65KError]);
  1034.         close (fin);
  1035.         close (fout);
  1036.         return ERROR;
  1037.       }
  1038.  
  1039.       do {
  1040.  
  1041.         if (taille < offset+bloc)
  1042.             bloc = taille - offset;
  1043.                                     /* si on arrive au bout, on lit seulement */
  1044.                                     /* jusqu'à la fin. */
  1045.         read (fin, ptr, bloc);
  1046.         if (write (fout, ptr, bloc) == -1)
  1047.         {
  1048.             puterrorlog (_Strings[L_ProblemDuringMove], file1);
  1049.             close (fin);
  1050.             close (fout);
  1051.             free (ptr);
  1052.             return ERROR;
  1053.         }
  1054.         offset += bloc;
  1055.       } while (taille != offset); /* fin do */
  1056.  
  1057.       close (fin);
  1058.       close (fout);
  1059.       free (ptr);
  1060.       remove (file1);
  1061.   } /* fin if */
  1062.  
  1063.   return OK;
  1064. }
  1065. /*------------------------------------------------------------------------
  1066.  * COPY_PHYSIQUE : Copy un fichier
  1067.  */
  1068. int copy_physique (char *file1, char *file2)
  1069. {
  1070.   long offset = 0;
  1071.   long taille;
  1072.   int fin, fout;
  1073.   char far *ptr;                 /* pointeur pour le malloc */
  1074.  
  1075.   if ((fin = sopen (file1, O_RDONLY | O_BINARY, SH_DENYNO )) == - 1)
  1076.      return ERROR;
  1077.  
  1078.   if ((fout = open (file2, O_BINARY | O_CREAT | O_WRONLY, S_IWRITE)) == - 1)
  1079.      return ERROR;
  1080.  
  1081.   taille = filelength (fin);
  1082.   bloc = 65000;
  1083.  
  1084.   if ((ptr = (char far *) malloc (bloc)) == NULL)
  1085.   {
  1086.     puterrorlog (_Strings[L_Malloc65KError]);
  1087.     close (fin);
  1088.     close (fout);
  1089.     return ERROR;
  1090.   }
  1091.  
  1092.   do {
  1093.  
  1094.     if (taille < offset+bloc)
  1095.         bloc = taille - offset;
  1096.                                 /* si on arrive au bout, on lit seulement */
  1097.                                 /* jusqu'à la fin. */
  1098.     read (fin, ptr, bloc);
  1099.     if (write (fout, ptr, bloc) == -1)
  1100.     {
  1101.         close (fin);
  1102.         close (fout);
  1103.         free  (ptr);
  1104.         return ERROR;
  1105.     }
  1106.     offset += bloc;
  1107.   } while (taille != offset); /* fin do */
  1108.  
  1109.   lseek(fin, 0,SEEK_SET);
  1110.   locking (fin, LK_UNLCK, filelength (fin));
  1111.   close (fin);
  1112.   close (fout);
  1113.   free (ptr);
  1114.   return OK;
  1115. }
  1116.  
  1117. /*------------------------------------------------------------------------
  1118.  * RENBAK : Renomme un fichier en .bak
  1119.  */
  1120. int renbak (char *file)
  1121. {
  1122.   char filebak [255];
  1123.   char *pointeur;
  1124.  
  1125.   pointeur = strchr (file, '.');
  1126.  
  1127.   if (pointeur != NULL)
  1128.   {
  1129.      *pointeur = 0;
  1130.      sprintf (filebak, "%s.bak", file);
  1131.      *pointeur = '.';
  1132.   } else {
  1133.      sprintf (filebak, "%s.bak", file);
  1134.   }
  1135.  
  1136.   remove (filebak);
  1137.   return (rename (file, filebak));
  1138. }
  1139. /*------------------------------------------------------------------------
  1140.        GETMAXAREA : retourne le nombre d'area de DIR.LST
  1141.  */
  1142.  
  1143. unsigned getmaxarea (void)
  1144. {
  1145.  
  1146.   FILE *fin;
  1147.   int i=0;
  1148.  
  1149.   if ((fin = _fsopen (Config.DirList, "rb", SH_DENYNO)) == NULL)
  1150.   {
  1151.      puterrorlog (_Strings[L_CanNotOpenReadMode], Config.DirList);
  1152.      return ERROR;
  1153.   }
  1154.   do {
  1155.  
  1156.     i++;
  1157.     if ( fgets (tampon, 97, fin) == NULL) break;
  1158.   } while (1); /* fin do */
  1159.  
  1160.   fclose (fin);
  1161.   return (i);
  1162.  
  1163. }
  1164.  
  1165.