ff6f0ae1ddeb7c881fbea86d6359bef9a65a8b5a
[tpg/acess2.git] / Tools / DiskTool / src / main.c
1 /*
2  */
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <disktool_common.h>
9
10 // === CODE ===
11 int main(int argc, char *argv[])
12 {
13         // Parse arguments
14         for( int i = 1; i < argc; i ++ )
15         {
16                 if( strcmp("--image", argv[i]) == 0 || strcmp("-i", argv[i]) == 0 ) {
17                         // Mount an image
18                         if( argc - i < 3 ) {
19                                 fprintf(stderr, "--image/-i takes 2 arguments (ident and path)\n");
20                                 exit(-1);
21                         }
22
23                         if( DiskTool_MountImage(argv[i+1], argv[i+2]) ) {
24                                 fprintf(stderr, "Unable to mount '%s' as '%s'\n", argv[i+2], argv[i+1]);
25                                 exit(-1);
26                         }
27
28                         i += 2;
29                         continue ;
30                 }
31                 
32                 if( strcmp("ls", argv[i]) == 0 ) {
33                         if( argc - i < 2 ) {
34                                 fprintf(stderr, "ls 1 argument (path)\n");
35                                 exit(-1);
36                         }
37
38                         DiskTool_ListDirectory(argv[i+1]);
39                         i += 1;
40                         continue ;
41                 }
42                 
43                 if( strcmp("cp", argv[i]) == 0 ) {
44                         
45                         if( argc - i < 3 ) {
46                                 fprintf(stderr, "cp takes 2 arguments (source and destination)\n");
47                                 exit(-1);
48                         }
49
50                         DiskTool_Copy(argv[i+1], argv[i+2]);                    
51
52                         i += 2;
53                         continue ;
54                 }
55         }
56         return 0;
57 }
58
59 // NOTE: This is in a native compiled file because it needs access to the real errno macro
60 int *Threads_GetErrno(void)
61 {
62         return &errno;
63 }
64
65 // TODO: Move into a helper lib?
66 void itoa(char *buf, uint64_t num, int base, int minLength, char pad)
67 {
68         char fmt[] = "%0ll*x";
69         switch(base)
70         {
71         case  8:        fmt[5] = 'o';   break;
72         case 10:        fmt[5] = 'd';   break;
73         case 16:        fmt[5] = 'x';   break;
74         }
75         if(pad != '0') {
76                 fmt[1] = '%';
77                 sprintf(buf, fmt+1, minLength, num);
78         }
79         else {
80                 sprintf(buf, fmt, minLength, num);
81         }
82 }
83
84 int strpos(const char *Str, char Ch)
85 {
86         const char *r = strchr(Str, Ch);
87         if(!r)  return -1;
88         return r - Str;
89 }
90
91 int strucmp(const char *s1, const char *s2)
92 {
93         return strcasecmp(s1, s2);
94 }
95
96 uint64_t DivMod64U(uint64_t value, uint64_t divisor, uint64_t *remainder)
97 {
98         if(remainder)
99                 *remainder = value % divisor;
100         return value / divisor;
101 }
102

UCC git Repository :: git.ucc.asn.au