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

  1. /* $Id: uac_crt.c,v 1.1 2000/03/27 04:53:10 ktk Exp $ */
  2.  
  3. /* ------------------------------------------------------------------------ */
  4. /*                                                                          */
  5. /*      Creates/Replaces files or directories.                              */
  6. /*                                                                          */
  7. /* ------------------------------------------------------------------------ */
  8.  
  9. #include "os.h"
  10.  
  11. #include <stdlib.h>
  12. #include <fcntl.h>     // AMIGA: open()
  13. #include <stdio.h>     // pipeit() remove()
  14. #include <string.h>    // strncpy()
  15. #include <sys/types.h>
  16. #include <sys/stat.h>  // struct stat
  17. #include <string.h>
  18. #include <io.h>
  19.  
  20.  
  21. #if defined(DOS) || defined(WINNT) || defined(WIN16)
  22.  #include <io.h>       // access()
  23. #endif
  24. #if defined(__IBMC__)
  25.  #include <direct.h>
  26. #endif
  27.  
  28. #define DIRSEP     '\\'
  29.  
  30. #include "attribs.h"
  31. #include "globals.h"
  32. #include "uac_crt.h"
  33. #include "uac_sys.h"
  34.  
  35. /* gets file name from header
  36.  */
  37. CHAR *ace_fname(CHAR * s, thead * head, INT nopath)
  38. {
  39.    INT  i;
  40.    char *cp;
  41.  
  42.    strncpy(s, (CHAR *)(*(tfhead *) head).FNAME, i = (*(tfhead *) head).FNAME_SIZE);
  43.    s[i] = 0;
  44.  
  45.    if (nopath)
  46.    {
  47.       cp=strrchr(s, '\\');
  48.       if (cp)
  49.          memmove(s, cp+1, strlen(cp));
  50.    }
  51.  
  52.    return s;
  53. }
  54.  
  55. void check_ext_dir(CHAR * f)        // checks/creates path of file
  56. {
  57.    CHAR *cp,
  58.         d[PATH_MAX];
  59.    INT  i;
  60.  
  61.    d[0] = 0;
  62.  
  63.    for (;;)
  64.    {
  65.       if ((cp = (CHAR *) strchr(&f[strlen(d) + 1], DIRSEP))!=NULL)
  66.       {
  67.          i = cp - f;
  68.          strncpy(d, f, i);
  69.          d[i] = 0;
  70.       }
  71.       else
  72.          return;
  73.  
  74.       if (!fileexists(d))
  75. #ifdef __EMX__
  76.           if (mkdir(d,0))
  77. #else
  78.               if (mkdir(d))
  79. #endif
  80.          {
  81.             f_err = ERR_WRITE;
  82.             pipeit("\n    Error while creating directory.\n");
  83.          }
  84.    }
  85. }
  86.  
  87. INT  ovr_delete(CHAR * n)           // deletes directory or file
  88. {
  89.    if (remove(n) && rmdir(n))
  90.    {
  91.       pipeit("\n    Could not delete file or directory. Access denied.\n");
  92.       return (1);
  93.    }
  94.    return (0);
  95. }
  96.  
  97. INT  create_dest_file(CHAR * file, INT a)  // creates file or directory
  98. {
  99.    INT  han,
  100.         i  = 0,
  101.         ex = fileexists(file);
  102.    struct stat st;
  103.  
  104.    check_ext_dir(file);
  105.    if (f_err)
  106.       return (-1);
  107.    if (a & _A_SUBDIR)
  108.    {                                // create dir or file?
  109.       if (ex) 
  110.           stat(file, &st);
  111. #ifdef __EMX__
  112.       if ((!ex && mkdir(file,0)) || (ex && (st.st_mode & S_IFDIR)))
  113. #else
  114.           if ((!ex && mkdir(file)) || (ex && (st.st_mode & S_IFDIR)))
  115. #endif
  116.       {
  117.          pipeit("\n    Could not create directory.\n");
  118.          return (-1);
  119.       }
  120. #ifdef DOS
  121.       _dos_setfileattr(file, a);    // set directory attributes
  122. #endif
  123.       return (-1);
  124.    }
  125.    else
  126.    {
  127.       if (ex)
  128.       {                             // does the file already exist
  129.          if (!f_ovrall)
  130.          {
  131.             i = wrask("Overwrite existing file?");  // prompt for overwrite
  132.             f_ovrall = (i == 1);
  133.             if (i == 3)
  134.                f_err = ERR_USER;
  135.          }
  136.          if ((i && !f_ovrall) || ovr_delete(file))
  137.             return (-1);            // delete?
  138.       }
  139.       if ((han = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
  140.                             S_IREAD | S_IWRITE | S_IEXEC | S_IDELETE |
  141.                             S_IRGRP | S_IWGRP  | S_IROTH | S_IWOTH )) < 0)
  142.          pipeit("\n    Could not create destination file.\n");
  143.       return (han);
  144.    }
  145. }
  146.  
  147.