Author: hbelusca Date: Wed Oct 28 23:24:37 2015 New Revision: 69732
URL: http://svn.reactos.org/svn/reactos?rev=69732&view=rev Log: [FATTEN] - Do not always print the usage when an error occurs. Print it only when a syntactic error happens. - FAT volume labels are constituted of 11 chars (not 8!) which should be printable ASCII (in particular, >= 0x20) and in uppercase.
Modified: trunk/reactos/tools/fatten/fatten.c
Modified: trunk/reactos/tools/fatten/fatten.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/tools/fatten/fatten.c?rev=6... ============================================================================== --- trunk/reactos/tools/fatten/fatten.c [iso-8859-1] (original) +++ trunk/reactos/tools/fatten/fatten.c [iso-8859-1] Wed Oct 28 23:24:37 2015 @@ -54,52 +54,17 @@ } }
-int is_command(const char* parg) +void print_help(const char* name) { -#if _WIN32 - return (parg[0] == '/') || (parg[0] == '-'); -#else - return (parg[0] == '-'); -#endif -} - -#define NEED_PARAMS(_min_, _max_) \ - do {\ - if (nargs < _min_) { printf("Too few args for command %s.\n" , argv[-1]); PRINT_HELP_AND_QUIT(); } \ - if (nargs > _max_) { printf("Too many args for command %s.\n", argv[-1]); PRINT_HELP_AND_QUIT(); } \ - } while(0) - -int need_mount(void) -{ - int r; - - if (isMounted) - return FR_OK; - - r = f_mount(&g_Filesystem, "0:", 0); - if (r) - return r; - - isMounted = 1; - return FR_OK; -} - -#define NEED_MOUNT() \ - do { ret = need_mount(); if(ret) \ - {\ - printf("Error: could not mount disk (%d).\n", ret); \ - PRINT_HELP_AND_QUIT(); \ - } } while(0) - -void print_help(char const * const name) -{ + printf("\n"); printf("Syntax: %s image_file [list of commands]\n\n", name); #if _WIN32 printf("Commands: [Note: both '/' and '-' are accepted as command prefixes.]\n"); #else printf("Commands:\n"); #endif - printf(" -format <sectors> [<filesystem>] [<custom header label>]\n" + // printf(" -format <sectors> [<filesystem>] [<custom header label>]\n" + printf(" -format <sectors> [<custom header label>]\n" " Formats the disk image.\n"); printf(" -boot <sector file>\n" " Writes a new boot sector.\n"); @@ -127,6 +92,43 @@ goto exit; \ } while (0)
+int is_command(const char* parg) +{ +#if _WIN32 + return (parg[0] == '/') || (parg[0] == '-'); +#else + return (parg[0] == '-'); +#endif +} + +#define NEED_PARAMS(_min_, _max_) \ + do {\ + if (nargs < _min_) { printf("Too few args for command %s.\n" , argv[-1]); PRINT_HELP_AND_QUIT(); } \ + if (nargs > _max_) { printf("Too many args for command %s.\n", argv[-1]); PRINT_HELP_AND_QUIT(); } \ + } while(0) + +int need_mount(void) +{ + int r; + + if (isMounted) + return FR_OK; + + r = f_mount(&g_Filesystem, "0:", 0); + if (r) + return r; + + isMounted = 1; + return FR_OK; +} + +#define NEED_MOUNT() \ + do { ret = need_mount(); if(ret) \ + {\ + printf("Error: could not mount disk (%d).\n", ret); \ + goto exit; \ + } } while(0) + int main(int oargc, char* oargv[]) { int ret; @@ -141,14 +143,15 @@
if (is_command(argv[0])) { - printf("Error: first parameter must be a filename, found '%s' instead. \n", argv[0]); + printf("Error: first parameter must be a filename, found '%s' instead.\n", argv[0]); PRINT_HELP_AND_QUIT(); }
if (disk_openimage(0, argv[0])) { - printf("Error: could not open image file '%s'. \n", argv[0]); - PRINT_HELP_AND_QUIT(); + printf("Error: could not open image file '%s'.\n", argv[0]); + ret = 1; + goto exit; }
argc--; @@ -156,13 +159,13 @@
while (argc > 0) { - char *parg = *argv; + char* parg = *argv; int nargs = 0; int i = 0;
if (!is_command(parg)) { - printf("Error: Expected a command, found '%s' instead. \n", parg); + printf("Error: Expected a command, found '%s' instead.\n", parg); PRINT_HELP_AND_QUIT(); }
@@ -199,36 +202,38 @@ if (ret) { printf("ERROR: Formatting drive: %d.\n", ret); - PRINT_HELP_AND_QUIT(); + goto exit; }
// Arg 2: custom header label (optional) if (nargs > 1) { - char label[8]; + char label[11]; + char ch;
int i, invalid = 0; int len = strlen(argv[1]);
- if (len <= 8) - { - // Copy and verify each character + if (len <= sizeof(label)) + { + // Verify each character (should be printable ASCII) + // and copy it in uppercase. for (i = 0; i < len; i++) { - char ch = argv[1][i]; - label[i] = ch; - - if (!isupper(ch) && !isspace(ch)) + ch = toupper(argv[1][i]); + if ((ch < 0x20) || !isprint(ch)) { invalid = 1; break; } + + label[i] = ch; }
if (!invalid) { // Pad the label with spaces - while (len < 8) + while (len < sizeof(label)) { label[len++] = ' '; } @@ -241,7 +246,7 @@
if (invalid) { - printf("Error: header label is limited to 8 uppercase letters and spaces."); + printf("Error: header label is limited to 11 printable uppercase ASCII symbols."); ret = 1; goto exit; } @@ -253,14 +258,13 @@ goto exit; }
- if (g_Filesystem.fs_type == FS_FAT32) { - memcpy(buff + 71, label, 8); + memcpy(buff + 71, label, sizeof(label)); } else { - memcpy(buff + 43, label, 8); + memcpy(buff + 43, label, sizeof(label)); }
if (disk_write(0, buff, 0, 1))