diff options
author | Dima Zavin <dima@android.com> | 2010-02-04 19:03:00 -0800 |
---|---|---|
committer | Dima Zavin <dima@android.com> | 2010-02-12 15:32:24 -0800 |
commit | eadd925d248de16fe7f19ac85c542a18f09149ac (patch) | |
tree | ab169e387956ec0412a2a65010baec0dd96fd344 | |
parent | b9a978bab60578e1fbdad4e3ff244f7663f2df94 (diff) | |
download | yaffs2-eadd925d248de16fe7f19ac85c542a18f09149ac.tar.gz |
mkyaffs2image: convert option parsing to use getopt
Signed-off-by: Dima Zavin <dima@android.com>
-rw-r--r-- | yaffs2/utils/mkyaffs2image.c | 82 |
1 files changed, 49 insertions, 33 deletions
diff --git a/yaffs2/utils/mkyaffs2image.c b/yaffs2/utils/mkyaffs2image.c index 250cf11..7d2b6ab 100644 --- a/yaffs2/utils/mkyaffs2image.c +++ b/yaffs2/utils/mkyaffs2image.c @@ -455,75 +455,91 @@ static int process_directory(int parent, const char *path, int fixstats) } +static void usage(void) +{ + fprintf(stderr,"mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n"); + fprintf(stderr,"usage: mkyaffs2image [-f] dir image_file [convert]\n"); + fprintf(stderr," -f fix file stat (mods, user, group) for device\n"); + fprintf(stderr," dir the directory tree to be converted\n"); + fprintf(stderr," image_file the output file to hold the image\n"); + fprintf(stderr," 'convert' produce a big-endian image from a little-endian machine\n"); +} int main(int argc, char *argv[]) { - int fixstats = 0; + int fixstats = 0; struct stat stats; - - if (argc > 1) { - if (strcmp(argv[1], "-f") == 0) { - fixstats = 1; - argc--; - argv++; - } - } + int opt; + char *image; + char *dir; + + while ((opt = getopt(argc, argv, "f")) != -1) { + switch (opt) { + case 'f': + fixstats = 1; + break; + default: + usage(); + exit(1); + } + } - if(argc < 3) - { - fprintf(stderr,"mkyaffs2image: image building tool for YAFFS2 built "__DATE__"\n"); - fprintf(stderr,"usage: mkyaffs2image [-f] dir image_file [convert]\n"); - fprintf(stderr," -f fix file stat (mods, user, group) for device\n"); - fprintf(stderr," dir the directory tree to be converted\n"); - fprintf(stderr," image_file the output file to hold the image\n"); - fprintf(stderr," 'convert' produce a big-endian image from a little-endian machine\n"); + if ((argc - optind < 2) || (argc - optind > 3)) { + usage(); exit(1); } - if ((argc == 4) && (!strncmp(argv[3], "convert", strlen("convert")))) - { - convert_endian = 1; - } - - if(stat(argv[1],&stats) < 0) + dir = argv[optind]; + image = argv[optind + 1]; + + if (optind + 2 < argc) { + if (!strncmp(argv[optind + 2], "convert", strlen("convert"))) + convert_endian = 1; + else { + usage(); + exit(1); + } + } + + if(stat(dir,&stats) < 0) { - fprintf(stderr,"Could not stat %s\n",argv[1]); + fprintf(stderr,"Could not stat %s\n",dir); exit(1); } if(!S_ISDIR(stats.st_mode)) { - fprintf(stderr," %s is not a directory\n",argv[1]); + fprintf(stderr," %s is not a directory\n",dir); exit(1); } - outFile = open(argv[2],O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE); + outFile = open(image,O_CREAT | O_TRUNC | O_WRONLY, S_IREAD | S_IWRITE); if(outFile < 0) { - fprintf(stderr,"Could not open output file %s\n",argv[2]); + fprintf(stderr,"Could not open output file %s\n",image); exit(1); } if (fixstats) { - int len = strlen(argv[1]); + int len = strlen(dir); - if((len >= 4) && (!strcmp(argv[1] + len - 4, "data"))) { + if((len >= 4) && (!strcmp(dir + len - 4, "data"))) { source_path_len = len - 4; - } else if((len >= 7) && (!strcmp(argv[1] + len - 6, "system"))) { + } else if((len >= 7) && (!strcmp(dir + len - 6, "system"))) { source_path_len = len - 6; } else { fprintf(stderr,"Fixstats (-f) option requested but filesystem is not data or android!\n"); exit(1); } - fix_stat(argv[1], &stats); + fix_stat(dir, &stats); } - //printf("Processing directory %s into image file %s\n",argv[1],argv[2]); + //printf("Processing directory %s into image file %s\n",dir,image); error = write_object_header(1, YAFFS_OBJECT_TYPE_DIRECTORY, &stats, 1,"", -1, NULL); if(error) - error = process_directory(YAFFS_OBJECTID_ROOT,argv[1],fixstats); + error = process_directory(YAFFS_OBJECTID_ROOT,dir,fixstats); close(outFile); |