xref: /reactos/boot/freeldr/tools/rrmdir.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * COPYRIGHT:             See COPYING in the top level directory
3*c2c66affSColin Finck  * PROGRAMMER:            Rex Jolliff (rex@lvcablemodem.com)
4*c2c66affSColin Finck  *                        Casper S. Hornstrup (chorns@users.sourceforge.net)
5*c2c66affSColin Finck  * PURPOSE:               Platform independent remove directory command
6*c2c66affSColin Finck  */
7*c2c66affSColin Finck 
8*c2c66affSColin Finck #include <dirent.h>
9*c2c66affSColin Finck #include <errno.h>
10*c2c66affSColin Finck #include <limits.h>
11*c2c66affSColin Finck #include <stdio.h>
12*c2c66affSColin Finck #include <string.h>
13*c2c66affSColin Finck #include <stdlib.h>
14*c2c66affSColin Finck #include <unistd.h>
15*c2c66affSColin Finck 
16*c2c66affSColin Finck void
convertPath(char * pathToConvert)17*c2c66affSColin Finck convertPath (char * pathToConvert)
18*c2c66affSColin Finck {
19*c2c66affSColin Finck   while (*pathToConvert != 0)
20*c2c66affSColin Finck   {
21*c2c66affSColin Finck     if (*pathToConvert == '\\')
22*c2c66affSColin Finck     {
23*c2c66affSColin Finck       *pathToConvert = '/';
24*c2c66affSColin Finck     }
25*c2c66affSColin Finck     pathToConvert++;
26*c2c66affSColin Finck   }
27*c2c66affSColin Finck }
28*c2c66affSColin Finck 
29*c2c66affSColin Finck void
getDirectory(const char * filename,char * directorySpec)30*c2c66affSColin Finck getDirectory (const char *filename, char * directorySpec)
31*c2c66affSColin Finck {
32*c2c66affSColin Finck   int  lengthOfDirectory;
33*c2c66affSColin Finck 
34*c2c66affSColin Finck   if (strrchr (filename, '/') != 0)
35*c2c66affSColin Finck   {
36*c2c66affSColin Finck     lengthOfDirectory = strrchr (filename, '/') - filename;
37*c2c66affSColin Finck     strncpy (directorySpec, filename, lengthOfDirectory);
38*c2c66affSColin Finck     directorySpec [lengthOfDirectory] = '\0';
39*c2c66affSColin Finck   }
40*c2c66affSColin Finck   else
41*c2c66affSColin Finck   {
42*c2c66affSColin Finck     strcpy (directorySpec, ".");
43*c2c66affSColin Finck   }
44*c2c66affSColin Finck }
45*c2c66affSColin Finck 
46*c2c66affSColin Finck void
getFilename(const char * filename,char * fileSpec)47*c2c66affSColin Finck getFilename (const char *filename, char * fileSpec)
48*c2c66affSColin Finck {
49*c2c66affSColin Finck   if (strrchr (filename, '/') != 0)
50*c2c66affSColin Finck   {
51*c2c66affSColin Finck     strcpy (fileSpec, strrchr (filename, '/') + 1);
52*c2c66affSColin Finck   }
53*c2c66affSColin Finck   else
54*c2c66affSColin Finck   {
55*c2c66affSColin Finck     strcpy (fileSpec, filename);
56*c2c66affSColin Finck   }
57*c2c66affSColin Finck }
58*c2c66affSColin Finck 
59*c2c66affSColin Finck int
main(int argc,char * argv[])60*c2c66affSColin Finck main (int argc, char* argv[])
61*c2c66affSColin Finck {
62*c2c66affSColin Finck   int  justPrint = 0;
63*c2c66affSColin Finck   int  idx;
64*c2c66affSColin Finck   int  returnCode;
65*c2c66affSColin Finck 
66*c2c66affSColin Finck   for (idx = 1; idx < argc; idx++)
67*c2c66affSColin Finck   {
68*c2c66affSColin Finck     convertPath (argv [idx]);
69*c2c66affSColin Finck 
70*c2c66affSColin Finck     if (justPrint)
71*c2c66affSColin Finck     {
72*c2c66affSColin Finck       printf ("remove %s\n", argv [idx]);
73*c2c66affSColin Finck     }
74*c2c66affSColin Finck     else
75*c2c66affSColin Finck     {
76*c2c66affSColin Finck       returnCode = rmdir (argv [idx]);
77*c2c66affSColin Finck       if (returnCode != 0 && errno != ENOENT)
78*c2c66affSColin Finck       {
79*c2c66affSColin Finck       /* Continue even if there is errors */
80*c2c66affSColin Finck #if 0
81*c2c66affSColin Finck         printf ("Rmdir of %s failed.  Rmdir returned %d.\n",
82*c2c66affSColin Finck                 argv [idx],
83*c2c66affSColin Finck                 returnCode);
84*c2c66affSColin Finck         return  returnCode;
85*c2c66affSColin Finck #endif
86*c2c66affSColin Finck       }
87*c2c66affSColin Finck     }
88*c2c66affSColin Finck   }
89*c2c66affSColin Finck 
90*c2c66affSColin Finck   return  0;
91*c2c66affSColin Finck }
92*c2c66affSColin Finck 
93*c2c66affSColin Finck 
94