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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
 *  REN.C - rename internal command.
 *
 *
 *  History:
 *
 *
 *    27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
 *        added config.h include
 *
 *    18-Dec-1998 (Eric Kohl)
 *        Added support for quoted long file names with spaces.
 *
 *    20-Jan-1999 (Eric Kohl)
 *        Unicode and redirection safe!
 *
 *    17-Oct-2001 (Eric Kohl)
 *        Implemented basic rename code.
 *
 *    30-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
 *        Remove all hardcoded strings in En.rc
 *
 *    25-Nov-2008 (Victor Martinez <vicmarcal@hotmail.com>)
 *        Patch dedicated to Myrjala because her comprehension and love :D
 *        Fixing following Bugs:
 *             -Wrong behavior with wildcards when Source and Destiny are Paths(FIXED).
 *             -Wrong general behavior (MSDN:"Rename cant move files between subdirectories")(FIXED)
 *             -Wrong behavior when renaming without path in destiny:(i.e) "ren C:\text\as.txt list.txt" it moves as.txt and then rename it(FIXED)
 *              (MSDN: If there is a Path in Source and no Path in Destiny, then Destiny Path is Source Path,because never Ren has to be used to move.)
 *             -Implemented checkings if SourcePath and DestinyPath are differents.
 *
 */

#include "precomp.h"

#ifdef INCLUDE_CMD_RENAME

enum
{
    REN_ATTRIBUTES = 0x001,   /* /A : not implemented */
    REN_ERROR      = 0x002,   /* /E */
    REN_NOTHING    = 0x004,   /* /N */
    REN_PROMPT     = 0x008,   /* /P : not implemented */
    REN_QUIET      = 0x010,   /* /Q */
    REN_SUBDIR     = 0x020,   /* /S */
    REN_TOTAL      = 0x040,   /* /T */
};


/*
 *  file rename internal command.
 */
INT cmd_rename (LPTSTR param)
{
    LPTSTR *arg = NULL;
    INT args = 0;
    INT nSlash = 0;
    INT nEvalArgs = 0; /* number of evaluated arguments */
    DWORD dwFlags = 0;<--- Assignment 'dwFlags=0', assigned value is 0
    DWORD dwFiles = 0; /* number of renamed files */
    INT i;

    LPTSTR srcPattern = NULL; /* Source Argument*/
    TCHAR srcPath[MAX_PATH]; /*Source Path Directories*/
    LPTSTR srcFILE = NULL;  /*Contains the files name(s)*/
    TCHAR srcFinal[MAX_PATH];

    LPTSTR dstPattern = NULL; /*Destiny Argument*/
    TCHAR dstPath[MAX_PATH]; /*Source Path Directories*/
    LPTSTR dstFILE = NULL; /*Contains the files name(s)*/

    TCHAR dstLast[MAX_PATH]; /*It saves the File name after unmasked with wildcards*/
    TCHAR dstFinal[MAX_PATH]; /*It saves the Final destiny Path*/

    BOOL bDstWildcard = FALSE;
    BOOL bPath = FALSE;

    LPTSTR p,q,r;

    HANDLE hFile;
    WIN32_FIND_DATA f;
    /*If the PARAM=/? then show the help*/
    if (!_tcsncmp(param, _T("/?"), 2))
    {
        ConOutResPaging(TRUE,STRING_REN_HELP1);
        return 0;
    }

    nErrorLevel = 0;

    /* Split the argument list.Args will be saved in arg vector*/
    arg = split(param, &args, FALSE, FALSE);

    if (args < 2)
    {
        if (!(dwFlags & REN_ERROR))<--- Condition '!(dwFlags&REN_ERROR)' is always true
            error_req_param_missing();
        freep(arg);
        return 1;
    }

    /* Read options */
    for (i = 0; i < args; i++)
    {
        /* Lets check if we have a special option chosen and set the flag(s)*/
        if (*arg[i] == _T('/'))
        {
            if (_tcslen(arg[i]) >= 2)
            {
                switch (_totupper(arg[i][1]))
                {
                    case _T('E'):
                    dwFlags |= REN_ERROR;
                    break;

                    case _T('N'):
                    dwFlags |= REN_NOTHING;
                    break;

                    case _T('P'):
                    dwFlags |= REN_PROMPT;
                    break;

                    case _T('Q'):
                    dwFlags |= REN_QUIET;
                    break;

                    case _T('S'):
                    dwFlags |= REN_SUBDIR;
                    break;

                    case _T('T'):
                    dwFlags |= REN_TOTAL;
                    break;
                }
            }
            nEvalArgs++;//Save the number of the options.
        }
    }

    /* keep quiet within batch files */
    if (bc != NULL)
        dwFlags |= REN_QUIET;

    /* there are only options on the command line --> error!!! */
    if (args < nEvalArgs + 2)
    {
        if (!(dwFlags & REN_ERROR))
            error_req_param_missing();
        freep(arg);
        return 1;
    }

    /* Get destination pattern and source pattern*/
    for (i = 0; i < args; i++)
    {
        if (*arg[i] == _T('/'))//We have find an Option.Jump it.
            continue;
        dstPattern = arg[i]; //we save the Last argument as dstPattern
        srcPattern = arg[i-1];
    }

    if (_tcschr(srcPattern, _T('\\')))  //Checking if the Source (srcPattern) is a Path to the file
    {
        bPath= TRUE;

        //Splitting srcPath and srcFile.

        srcFILE = _tcschr(srcPattern, _T('\\'));
        nSlash++;
        while(_tcschr(srcFILE, _T('\\')))
        {
            srcFILE++;
            if (*srcFILE==_T('\\')) nSlash++ ;
            if (!_tcschr(srcFILE, _T('\\'))) break;
        }
        _tcsncpy(srcPath,srcPattern,_tcslen(srcPattern)-_tcslen(srcFILE));

        if (_tcschr(dstPattern, _T('\\'))) //Checking if the Destiny (dstPattern)is also a Path.And splitting dstPattern in dstPath and srcPath.
        {
            dstFILE = _tcschr(dstPattern, _T('\\'));
            nSlash=0;
            while(_tcschr(dstFILE, _T('\\')))
            {
                dstFILE++;
                if (*dstFILE==_T('\\')) nSlash++ ;
                if (!_tcschr(dstFILE, _T('\\'))) break;
            }
            _tcsncpy(dstPath,dstPattern,_tcslen(dstPattern)-_tcslen(dstFILE));

            if ((_tcslen(dstPath)!=_tcslen(srcPath))||(_tcsncmp(srcPath,dstPath,_tcslen(srcPath))!=0)) //If it has a Path,then MUST be equal than srcPath
            {
                error_syntax(dstPath);
                freep(arg);
                return 1;
            }
        }
        else
        { //If Destiny hasnt a Path,then (MSDN says) srcPath is its Path.
            _tcscpy(dstPath,srcPath);
            dstFILE=dstPattern;
        }
    }

    if (!_tcschr(srcPattern, _T('\\'))) //If srcPattern isn't a Path but a name:
    {
        srcFILE=srcPattern;
        if (_tcschr(dstPattern, _T('\\')))
        {
            error_syntax(dstPattern);
            freep(arg);
            return 1;
        }
        else
        {
            dstFILE=dstPattern;
        }
    }

    //Checking Wildcards.
    if (_tcschr(dstFILE, _T('*')) || _tcschr(dstFILE, _T('?')))
        bDstWildcard = TRUE;

    TRACE("\n\nSourcePattern: %s SourcePath: %s SourceFile: %s", debugstr_aw(srcPattern),debugstr_aw(srcPath),debugstr_aw(srcFILE));
    TRACE("\n\nDestinationPattern: %s Destination Path:%s Destination File: %s\n", debugstr_aw(dstPattern),debugstr_aw(dstPath),debugstr_aw(dstFILE));

    hFile = FindFirstFile(srcPattern, &f);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        if (!(dwFlags & REN_ERROR)) error_file_not_found();
    }
    do
    {
        /* ignore "." and ".." */
        if (!_tcscmp (f.cFileName, _T(".")) || !_tcscmp (f.cFileName, _T("..")))
            continue;

        /* do not rename hidden or system files */
        if (f.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM))
            continue;

        /* do not rename directories when the destination pattern contains
         * wildcards, unless option /S is used */
        if ((f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
            bDstWildcard && !(dwFlags & REN_SUBDIR))
        {
            continue;
        }

        TRACE("Found source name: %s\n", debugstr_aw(f.cFileName));
        /* So here we have splitted the dstFILE and we have find a f.cFileName(thanks to srcPattern)
         * Now we have to use the mask (dstFILE) (which can have Wildcards) with f.cFileName to find destination file name(dstLast) */
        p = f.cFileName;
        q = dstFILE;
        r = dstLast;
        while(*q != 0)
        {
            if (*q == '*')
            {
                q++;
                while (*p != 0 && *p != *q)
                {
                    *r = *p;
                    p++;
                    r++;
                }
            }
            else if (*q == '?')
            {
                q++;
                if (*p != 0)
                {
                    *r = *p;
                    p++;
                    r++;
                }
            }
            else
            {
                *r = *q;
                if (*p != 0) p++;
                q++;
                r++;
            }
        }
        *r = 0;
        //Well we have splitted the Paths,so now we have to paste them again(if needed),thanks bPath.
        if (bPath != FALSE)
        {
            _tcscpy(srcFinal,srcPath);
            _tcscat(srcFinal,f.cFileName);
            _tcscpy(dstFinal,dstPath);
            _tcscat(dstFinal,dstLast);
        }
        else
        {
            _tcscpy(srcFinal,f.cFileName);
            _tcscpy(dstFinal,dstLast);
        }

        TRACE("DestinationPath: %s\n", debugstr_aw(dstFinal));

        if (!(dwFlags & REN_QUIET) && !(dwFlags & REN_TOTAL))
            ConOutPrintf(_T("%s -> %s\n"),srcFinal , dstFinal);

        /* Rename the file */
        if (!(dwFlags & REN_NOTHING))
        {
            if (MoveFile(srcFinal, dstFinal))
            {
                dwFiles++;
            }
            else
            {
                if (!(dwFlags & REN_ERROR))
                    ConErrResPrintf(STRING_REN_ERROR1, GetLastError());
            }
        }
    }
    while (FindNextFile(hFile, &f));

    //Closing and Printing errors.
    FindClose(hFile);

    if (!(dwFlags & REN_QUIET))
    {
        if (dwFiles == 1)
            ConOutResPrintf(STRING_REN_HELP2, dwFiles);
        else
            ConOutResPrintf(STRING_REN_HELP3, dwFiles);
    }

    freep(arg);
    return 0;
}

#endif

/* EOF */