home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / slfinsta.zip / uac_crt.c < prev    next >
C/C++ Source or Header  |  2000-03-26  |  7KB  |  293 lines

  1. /* $Id: uac_crt.c,v 1.1 2000/03/27 04:52:58 ktk Exp $ */
  2.  
  3. /* ------------------------------------------------------------------------ */
  4. /*                                                                          */
  5. /*      Creates/Replaces files or directories.                              */
  6. /*                                                                          */
  7. /* ------------------------------------------------------------------------ */
  8.  
  9. #include "os.h"
  10.  
  11. #ifdef __EMX__
  12. #include <sys/types.h>
  13. #include <sys/dirent.h>
  14. #include <stdlib.h>
  15. #else
  16. #include <direct.h>
  17. #endif
  18. #include <io.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>     // printf() remove()
  21. #include <string.h>    // strncpy()
  22. #include <sys/types.h>
  23. #include <sys/stat.h>  // struct stat
  24. #define INCL_WIN       /* Window Manager Functions     */
  25. #define INCL_DOS
  26. #define INCL_BASE
  27. #include <os2.h>
  28. #include <time.h>
  29. #include "install.h"
  30. #define OS2_H_INCLUDED
  31.  
  32. #if defined(DOS) || defined(WINNT) || defined(WIN16)
  33.  #include <io.h>       // access()
  34. #if defined(WATCOM_C) || define(__IBMC__)
  35.  #include <direct.h>      // mkdir()
  36. #else
  37.  #include <dir.h>      // mkdir()
  38. #endif
  39. #endif
  40.  
  41. #include "attribs.h"
  42. #include "globals.h"
  43. #include "uac_crt.h"
  44. #include "uac_sys.h"
  45.  
  46. extern FILE *logfile;
  47. extern char installdir[400];
  48.  
  49. /* Undocumented functions */
  50. APIRET APIENTRY DosReplaceModule(PSZ pszOldModule,PSZ pszNewModule,PSZ pszBackupModule);
  51.  
  52.  
  53. /* gets file name from header
  54.  */
  55. CHAR *ace_fname(CHAR * s, thead * head, INT nopath)
  56. {
  57.    INT  i;
  58.    char *cp;
  59.  
  60.    strncpy(s, (CHAR*)(*(tfhead *) head).FNAME, i = (*(tfhead *) head).FNAME_SIZE);
  61.    s[i] = 0;
  62.  
  63.    if (nopath)
  64.    {
  65.       cp=strrchr(s, '\\');
  66.       if (cp)
  67.          memmove(s, cp+1, strlen(cp));
  68.    }
  69. #ifdef __UNIX__
  70.    else
  71.    {                                // by current OS seperator
  72.       cp=s;
  73.       while ((cp=strchr(cp, '\\'))!=NULL)
  74.          *cp++='/';
  75.    }
  76. #endif
  77.  
  78.    return s;
  79. }
  80.  
  81. void check_ext_dir(CHAR * f)        // checks/creates path of file
  82. {
  83.     char d[1024];
  84.     char buffer[1024];
  85.     int z, flag = 0, len = strlen(f);
  86.  
  87.     strcpy(buffer, f);
  88.     for(z=len;z>-1;z--)
  89.     {
  90.         if(buffer[z] == '\\')
  91.         {
  92.             buffer[z+1] = 0;
  93.             flag = 1;
  94.             z = -1;
  95.         }
  96.     }
  97.     if(!flag)
  98.         return;
  99.     for(z=0;z<strlen(buffer);z++)
  100.     {
  101.         if(buffer[z] == '\\')
  102.         {
  103.             if(!(z == 2 && buffer[1] == ':'))
  104.             {
  105.                 strcpy(d, buffer);
  106.                 d[z] = 0;
  107.                 if (!fileexists(d))
  108.                 {
  109. #ifdef __EMX__
  110.                     if (mkdir(d, 0))
  111. #else
  112.                        if (mkdir(d))
  113. #endif
  114.                     {
  115.                         f_err = ERR_WRITE;
  116.                         error("Error while creating directory \"%s\".", d);
  117.                     }
  118.                     else
  119.                     {
  120.                         if(logfile)
  121.                         {
  122.                             if(strlen(d) > 1 && d[1] == ':')
  123.                                 fprintf(logfile, "<NewDir>,%s\r\n", d);
  124.                             else
  125.                             {
  126.                                 if(installdir[strlen(installdir)-1] == '\\')
  127.                                     fprintf(logfile, "<NewDir>,%s%s\r\n", installdir, d);
  128.                                 else
  129.                                     fprintf(logfile, "<NewDir>,%s\\%s\r\n", installdir, d);
  130.                             }
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.  
  137.     }
  138. }
  139.  
  140. INT  ovr_delete(CHAR * n)           // deletes directory or file
  141. {
  142.    if (remove(n) && rmdir(n))
  143.    {
  144.        DosReplaceModule(n, NULL, NULL);
  145.        if (remove(n) && rmdir(n))
  146.        {
  147.            error("Could not delete file or directory: \"%s\" Access denied.", n);
  148.            return (1);
  149.        }
  150.    }
  151.    return (0);
  152. }
  153.  
  154. INT  create_dest_file(CHAR * file, INT a)  // creates file or directory
  155. {
  156.    INT  han,
  157.         i  = 0,
  158.         ex = fileexists(file);
  159.    struct stat st;
  160.    extern int no_update;
  161.  
  162.    check_ext_dir(file);
  163.    if (f_err)
  164.       return (-1);
  165.    if (a & _A_SUBDIR)
  166.    {                                // create dir or file?
  167.        if (ex)
  168.        {
  169.            stat(file, &st);
  170.        }
  171.  
  172. #ifdef __EMX__
  173.       if ((!ex && mkdir(file, 0)) || (ex && (st.st_mode & S_IFDIR)))
  174. #else
  175.       if ((!ex && mkdir(file)) || (ex && (st.st_mode & S_IFDIR)))
  176. #endif
  177.       {
  178.          error("Could not create directory %s.");
  179.          return (-1);
  180.       } 
  181.       else
  182.       {               /* I wonder why it never gets here... :/ BS */
  183.           if(logfile)
  184.               fprintf(logfile, "<NewDir>,%s\\%s\r\n", installdir, file);
  185.       }
  186.  
  187. #ifdef DOS
  188.       _dos_setfileattr(file, a);    // set directory attributes
  189. #endif
  190.       return (-1);
  191.    }
  192.    else
  193.    {
  194.       if (ex)
  195.       {                             // does the file already exist
  196. #ifdef SDDINST
  197.            static int sddall = 0;
  198.            FILESTATUS3 fileinfo;
  199.  
  200.            f_ovrall = 1;
  201.  
  202.            DosQueryPathInfo(file, FIL_STANDARD, &fileinfo, sizeof(FILESTATUS3));
  203.            if(!sddall)
  204.            {
  205.                FDATE fdate;
  206.                FTIME ftime;
  207.                struct tm     tc, tc2;
  208.                time_t         tt, tt2;
  209.  
  210.                *((USHORT*)&fdate) = (USHORT)(fhead.FTIME >> 16);
  211.                *((USHORT*)&ftime) = (USHORT)fhead.FTIME;
  212.  
  213.                tc.tm_year = fileinfo.fdateLastWrite.year + 80;
  214.                tc.tm_mon = fileinfo.fdateLastWrite.month - 1;
  215.                tc.tm_mday = fileinfo.fdateLastWrite.day;
  216.                tc.tm_hour = fileinfo.ftimeLastWrite.hours;
  217.                tc.tm_min = fileinfo.ftimeLastWrite.minutes;
  218.                tc.tm_sec = fileinfo.ftimeLastWrite.twosecs * 2;
  219.  
  220.                tc2.tm_year = fdate.year + 80;
  221.                tc2.tm_mon = fdate.month - 1;
  222.                tc2.tm_mday = fdate.day;
  223.                tc2.tm_hour = ftime.hours;
  224.                tc2.tm_min = ftime.minutes;
  225.                tc2.tm_sec = ftime.twosecs * 2;
  226.  
  227.                if((tt = mktime(&tc)) == -1 || (tt2 = mktime(&tc2)) == -1 || tt > tt2)
  228.                {
  229.  
  230.                    if(file[1] == ':')
  231.                        i = confirm("File \"%s\" has a newer modification time. Overwrite?", file);  // prompt for overwrite
  232.                    else
  233.                    {
  234.                        if(installdir[strlen(installdir)-1] == '\\')
  235.                            i = confirm("File \"%s%s\" has a newer modification time. Overwrite?", installdir, file);  // prompt for overwrite
  236.                        else
  237.                            i = confirm("File \"%s\\%s\" has a newer modification time. Overwrite?", installdir, file);  // prompt for overwrite
  238.                    }
  239.                    if(i == 1)
  240.                    {
  241.                        sddall = 1;
  242.                        i = 0;
  243.                    }
  244.                    if (i == 3)
  245.                        f_err = ERR_USER;
  246.                    if(i)
  247.                        return -1;
  248.                }
  249.            }
  250.            fileinfo.attrFile = FILE_NORMAL;
  251.            DosSetPathInfo(file, FIL_STANDARD, (PVOID)&fileinfo, sizeof(FILESTATUS3), 0);
  252. #endif
  253.          if (!f_ovrall)
  254.          {
  255.              if(installdir[strlen(installdir)-1] == '\\')
  256.                  i = confirm("Overwrite file \"%s%s\"?", installdir, file);  // prompt for overwrite
  257.              else
  258.                  i = confirm("Overwrite file \"%s\\%s\"?", installdir, file);  // prompt for overwrite
  259.              f_ovrall = (i == 1);
  260.              if (i == 3)
  261.                  f_err = ERR_USER;
  262.          }
  263.          if ((i && !f_ovrall) || ovr_delete(file))
  264.             return (-1);            // delete?
  265.       }
  266.       if ((han = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
  267.                             S_IREAD | S_IWRITE | S_IEXEC | S_IDELETE |
  268.                             S_IRGRP | S_IWGRP  | S_IROTH | S_IWOTH )) < 0)
  269.          error("Could not create destination file \"%s\".", file);
  270.       else
  271.       {
  272.           if(logfile)
  273.           {
  274.               if(!no_update)
  275.               {
  276.                   if(strlen(file) > 1 && file[1] == ':')
  277.                       fprintf(logfile, "<FileInst>,%s\r\n", file);
  278.                   else
  279.                   {
  280.                       if(installdir[strlen(installdir)-1] == '\\')
  281.                           fprintf(logfile, "<FileInst>,%s%s\r\n", installdir, file);
  282.                       else
  283.                           fprintf(logfile, "<FileInst>,%s\\%s\r\n", installdir, file);
  284.                   }
  285.               }
  286.           }
  287.       }
  288.  
  289.       return (han);
  290.    }
  291. }
  292.  
  293.