home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / UnAce.lha / Src / Uac_crt.c < prev    next >
C/C++ Source or Header  |  1997-12-10  |  4KB  |  141 lines

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