File: uac_crt.c

package info (click to toggle)
unace 1.2b-12
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 408 kB
  • ctags: 420
  • sloc: ansic: 1,762; sh: 113; makefile: 93
file content (200 lines) | stat: -rw-r--r-- 4,656 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* ------------------------------------------------------------------------ */
/*                                                                          */
/*      Creates/Replaces files or directories.                              */
/*                                                                          */
/* ------------------------------------------------------------------------ */
/*  ML - 01/2004: changed licence to GPL                                    */
/* ------------------------------------------------------------------------ */

#include "os.h"

#include <fcntl.h>     // AMIGA: open()
#include <stdio.h>     // printf() remove()
#include <string.h>    // strncpy()
#include <sys/stat.h>  // struct stat

#ifdef UNIX
#include <unistd.h>
#else
 #if !defined(VA_CPP)
  #include <dos.h>     // AMIGA: mkdir() rmdir() DOS: _dos_*()
 #endif
#endif
#if defined(DOS) || defined(WINNT) || defined(WIN16) || defined(VA_CPP)
 #include <io.h>       // access()
#if defined(WATCOM_C) || defined(VA_CPP)
 #include <direct.h>   // mkdir()
#else
 #include <dir.h>      // mkdir()
#endif
#endif

#include "globals.h"
#include "uac_crt.h"
#include "uac_sys.h"

/* gets file name from header
 */
CHAR *ace_fname(CHAR * s, thead * head, INT nopath, unsigned int size)
{
   unsigned int i;
   char *cp;

   i = (*(tfhead *) head).FNAME_SIZE;
   if (i > (size - 1))
     i = size - 1;
   strncpy(s, (*(tfhead *) head).FNAME, i);
   s[i] = 0;

   if (nopath)
   {
      cp=strrchr(s, '\\');
      if (cp)
         memmove(s, cp+1, strlen(cp));
   }
#if (DIRSEP!='\\')                  // replace msdos directory seperator
   else
   {                                // by current OS seperator
      cp=s;
      while ((cp=strchr(cp, '\\'))!=NULL)
         *cp++=DIRSEP;
   }
#endif

   cp = s;
   while (*cp == '/') cp++;
   if (cp != s)
     memmove(s, cp, strlen(cp) + 1);

   return s;
}

int is_directory_traversal(char *str)
{
  unsigned int mode, countdots;
  /* mode 0 = fresh, 1 = just dots, 2 = not just dots */

  mode = countdots = 0;

  while (*str)
  {
    char ch = *str++;

    if ((ch == '/') && (mode == 1) && (countdots > 1))
      return 1;

    if (ch == '/')
    {
       mode = countdots = 0;
       continue;
    }

    if (ch == '.')
    {
      if (mode == 0)
        mode = 1;

      countdots++;
    }
    else
      mode = 2;
  }

  if ((mode == 1) && (countdots > 1))
    return 1;

  return 0;
}

void check_ext_dir(CHAR * f)        // checks/creates path of file
{
   CHAR *cp,
        d[PATH_MAX];
   unsigned int i;

   d[0] = 0;

   if (is_directory_traversal(f))
   {
      f_err = ERR_WRITE;
      printf("\n    Directory traversal attempt:  %s\n", f);
      return;
   }

   for (;;)
   {
      if ((cp = (CHAR *) strchr(&f[strlen(d) + 1], DIRSEP))!=NULL)
      {
         i = cp - f;
         if (i > (PATH_MAX - 1))
           i = PATH_MAX - 1;
         strncpy(d, f, i);
         d[i] = 0;
      }
      else
         return;

      if (!fileexists(d))
         if (mkdir(d))
         {
            f_err = ERR_WRITE;
            printf("\n    Error while creating directory.\n");
         }
   }
}

INT  ovr_delete(CHAR * n)           // deletes directory or file
{
   if (remove(n) && rmdir(n))
   {
      printf("\n    Could not delete file or directory. Access denied.\n");
      return (1);
   }
   return (0);
}

INT  create_dest_file(CHAR * file, INT a)  // creates file or directory
{
   INT  han,
        i  = 0,
        ex = fileexists(file);
   struct stat st;

   check_ext_dir(file);
   if (f_err)
      return (-1);
   if (a & _A_SUBDIR)
   {                                // create dir or file?
      if (ex) stat(file, &st);
      if (ex ? (st.st_mode & S_IFDIR) : mkdir(file))
      {
         printf("\n    Could not create directory.\n");
         return (-1);
      }
#ifdef DOS
      _dos_setfileattr(file, a);    // set directory attributes
#endif
      return (-1);
   }
   else
   {
      if (ex)
      {                             // does the file already exist
         if (!f_ovrall)
         {
            i = wrask("Overwrite existing file?");  // prompt for overwrite
            f_ovrall = (i == 1);
            if (i == 3)
               f_err = ERR_USER;
         }
         if ((i && !f_ovrall) || ovr_delete(file))
            return (-1);            // delete?
      }
      if ((han = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY,
                            S_IREAD | S_IWRITE | S_IEXEC | S_IDELETE |
                            S_IRGRP | S_IWGRP  | S_IROTH | S_IWOTH )) < 0)
         printf("\n    Could not create destination file.\n");
      return (han);
   }
}