Changeset 5204

Show
Ignore:
Timestamp:
04/24/08 14:34:51 (7 years ago)
Author:
khali
Message:

Move the chip address parsing to a common function for consistency.

Location:
i2c-tools/trunk/tools
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • i2c-tools/trunk/tools/i2cbusses.c

    r5202 r5204  
    345345} 
    346346 
     347/* 
     348 * Parse a CHIP-ADDRESS command line argument and return the corresponding 
     349 * chip address, or a negative value if the address is invalid. 
     350 */ 
     351int parse_i2c_address(const char *address_arg) 
     352{ 
     353        long address; 
     354        char *end; 
     355 
     356        address = strtol(address_arg, &end, 0); 
     357        if (*end || !*address_arg) { 
     358                fprintf(stderr, "Error: Chip address is not a number!\n"); 
     359                return -1; 
     360        } 
     361        if (address < 0x03 || address > 0x77) { 
     362                fprintf(stderr, "Error: Chip address out of range " 
     363                        "(0x03-0x77)!\n"); 
     364                return -2; 
     365        } 
     366 
     367        return address; 
     368} 
     369 
    347370int open_i2c_dev(const int i2cbus, char *filename, const int quiet) 
    348371{ 
  • i2c-tools/trunk/tools/i2cbusses.h

    r5202 r5204  
    3434 
    3535int lookup_i2c_bus(const char *i2cbus_arg); 
     36int parse_i2c_address(const char *address_arg); 
    3637int open_i2c_dev(const int i2cbus, char *filename, const int quiet); 
    3738int set_slave_addr(int file, int address, int force); 
  • i2c-tools/trunk/tools/i2cdump.c

    r5203 r5204  
    9898                exit(1); 
    9999        } 
    100         address = strtol(argv[flags+2], &end, 0); 
    101         if (*end) { 
    102                 fprintf(stderr, "Error: Second argument not a number!\n"); 
    103                 help(); 
    104                 exit(1); 
    105         } 
    106         if (address < 0x03 || address > 0x77) { 
    107                 fprintf(stderr, "Error: Address out of range!\n"); 
     100        address = parse_i2c_address(argv[flags+2]); 
     101        if (address < 0) { 
    108102                help(); 
    109103                exit(1); 
  • i2c-tools/trunk/tools/i2cget.c

    r5203 r5204  
    190190                help(); 
    191191 
    192         address = strtol(argv[flags+2], &end, 0); 
    193         if (*end || address < 0x03 || address > 0x77) { 
    194                 fprintf(stderr, "Error: Chip address invalid!\n"); 
     192        address = parse_i2c_address(argv[flags+2]); 
     193        if (address < 0) 
    195194                help(); 
    196         } 
    197195 
    198196        if (!(flags+3 < argc)) { 
  • i2c-tools/trunk/tools/i2cset.c

    r5203 r5204  
    8484                help(); 
    8585 
    86         address = strtol(argv[flags+2], &end, 0); 
    87         if (*end || address < 0x03 || address > 0x77) { 
    88                 fprintf(stderr, "Error: Chip address invalid!\n"); 
    89                 help(); 
    90         } 
     86        address = parse_i2c_address(argv[flags+2]); 
     87        if (address < 0) 
     88                help(); 
    9189 
    9290        daddress = strtol(argv[flags+3], &end, 0);