[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tyndur-devel] [PATCH] bincat: Positionsangabe moeglich (wird auf 1k-Bloecke gerundet)



On Sun, Aug 09, 2009 at 12:09:19AM +0200, Alexander Siol wrote:
> +bincat: Positionsangaben (Start, Länge und Ende)
> *bincat: Umstellung auf getopt
> 
> Signed-off-by: Alexander Siol <alex@xxxxxxxxxx>
> ---
>  trunk/src/modules/c/shell/cmds/bincat.c |   75 +++++++++++++++++++++++++++---
>  1 files changed, 67 insertions(+), 8 deletions(-)
> 
> diff --git a/trunk/src/modules/c/shell/cmds/bincat.c b/trunk/src/modules/c/shell/cmds/bincat.c
> index 8c0372e..0fa8539 100644
> --- a/trunk/src/modules/c/shell/cmds/bincat.c
> +++ b/trunk/src/modules/c/shell/cmds/bincat.c
> @@ -1,8 +1,8 @@
>  /*
> - * Copyright (c) 2007 The tyndur Project. All rights reserved.
> + * Copyright (c) 2007-2009 The tyndur Project. All rights reserved.
>   *
>   * This code is derived from software contributed to the tyndur Project
> - * by Antoine Kaufmann.
> + * by Antoine Kaufmann, Alexander Siol
>   *
>   * Redistribution and use in source and binary forms, with or without
>   * modification, are permitted provided that the following conditions
> @@ -38,35 +38,94 @@
>  #include "stdio.h"
>  #include "unistd.h"
>  #include <lost/config.h>
> +#include <getopt.h>
>  
>  #define BLOCK_SIZE 1024
>  
>  void bincat_display_usage(void);
>  
> +#define min(a,b) (a < b)?(a):(b);
> +
>  #ifdef CONFIG_SHELL_BUILTIN_ONLY
>      int shell_command_bincat(int argc, char* argv[], const char* args)
>  #else
>      int main(int argc, char* argv[])
>  #endif
>  {
> -    if (argc != 2) {
> +    static const struct option long_options[] =
> +    {
> +        { "start", required_argument,  0, 's' },
> +        { "size", required_argument,   0, 'l' },

Wenn -l, dann bitte doch auch --length

> +        { "end", required_argument,    0, 'e' },
> +        { "help", no_argument,         0, 'h' },
> +        { 0, 0, 0, 0 }
> +    };
> +
> +    optind = 0;
> +    int start = 0;
> +    int end = 0;
> +    int length = 0;

Du willst vermutlich uint64_t

> +    char *path = NULL;
> +    int n = 0;
> +    int m = 0;
> +    size_t total_read = 0;
> +
> +
> +    while (optind < argc) {
> +        int result = getopt_long(argc, argv, "s:l:e:h", long_options, NULL);
> +        if (result == -1) {
> +            break;
> +        }
> +        switch (result) {
> +            case 's':
> +                start = atoi(optarg);

Evtl. strtoull mit Basis 0, dann kann man auch in Hex eingeben. Ergebnis prüfen
wäre auch noch eine Idee.

> +                break;
> +            case 'l':
> +                length = atoi(optarg);
> +                break;
> +            case 'e':
> +                end = atoi(optarg);
> +                break;
> +            case 'h':
> +                bincat_display_usage();

bincat_display_usage könnte auch ein Update vertragen

> +                return EXIT_SUCCESS;
> +            default:
> +                break;
> +        }
> +    }
> +
> +    if (length && end) {
> +        printf("--size und --end schließen sich aus. Verwende --size\n");

Normal wäre, einfach abzubrechen

> +    } else if (end) {
> +        length = end - start;
> +    }
> +
> +    while (optind < argc) {
> +        path = argv[optind++];
> +    }

Hm, also nur die letzte Datei wird angezeigt? Ich würde entweder nacheinander
alle anzeigen oder einen Fehler ausgeben, wenn mehr als eine angegeben wird.

> +
> +    if (path == NULL) {
>          bincat_display_usage();
> -        return -1;
> +        return EXIT_SUCCESS;
>      }
>  
> -    char* path = argv[1];
>      FILE* file = fopen(path, "r");
>  
> +    fseek(file, start, SEEK_SET);
> +    m = start / 8;
> +
>      if (file == NULL) {
>          printf("Konnte '%s' nicht zum lesen oeffnen!\n", path);
>          return -1;
>      } else {
>          char buffer[BLOCK_SIZE];
>          size_t size;
> -        int n, m = 0;
> -        while (feof(file) != EOF) {
> +        while (feof(file) != EOF && (total_read <= length || length == 0)) {
> +
>              size = fread(buffer, 1, BLOCK_SIZE, file);
>  
> +            total_read += size;
> +
>              printf("0x%08x: ", m);
>  
>              for (n=0; n < size; n++) {
> @@ -82,7 +141,7 @@ void bincat_display_usage(void);
>  
>          fclose(file);
>      }
> -    
> +    printf("\n");
>      return EXIT_SUCCESS;
>  }
>  
> -- 
> 1.5.6.5
> 
> _______________________________________________
> tyndur-devel mailing list
> tyndur-devel@xxxxxxxxxx
> http://list.tyndur.org/mailman/listinfo/tyndur-devel