From 8603e2aab2f0bc3f6ee579fb521d7532544bdbec Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Thu, 24 Apr 2008 12:43:10 +0000 Subject: [PATCH] Move the confirmation to a separate function, as is done in i2cget already. git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@5209 7894878c-1315-0410-8ee3-d5d059ff63e0 --- CHANGES | 1 + tools/i2cset.c | 66 ++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/CHANGES b/CHANGES index 782779c..6b6f54a 100644 --- a/CHANGES +++ b/CHANGES @@ -13,6 +13,7 @@ SVN i2cset: Support i2c bus passed by name Shorten the usage message Restrict the chip address to 0x03-0x77 + Split the code into several functions for clarity 3.0.1 (2008-04-04) Drop the trailing .pl from all perl script names diff --git a/tools/i2cset.c b/tools/i2cset.c index a9c1017..d8d400a 100644 --- a/tools/i2cset.c +++ b/tools/i2cset.c @@ -83,6 +83,40 @@ static int check_funcs(int file, int i2cbus, int size, int pec) return 0; } +static int confirm(const char *filename, int address, int size, int daddress, + int value, int vmask, int pec) +{ + int dont = 0; + + fprintf(stderr, "WARNING! This program can confuse your I2C " + "bus, cause data loss and worse!\n"); + + if (address >= 0x50 && address <= 0x57) { + fprintf(stderr, "DANGEROUS! Writing to a serial " + "EEPROM on a memory DIMM\nmay render your " + "memory USELESS and make your system " + "UNBOOTABLE!\n"); + dont++; + } + + fprintf(stderr, "I will write to device file %s, chip address " + "0x%02x, data address\n0x%02x, data 0x%02x%s, mode " + "%s.\n", filename, address, daddress, value, + vmask ? " (masked)" : "", + size == I2C_SMBUS_BYTE_DATA ? "byte" : "word"); + if (pec) + fprintf(stderr, "PEC checking enabled.\n"); + + fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n"); + fflush(stderr); + if (!user_ack(!dont)) { + fprintf(stderr, "Aborting on user request.\n"); + return 0; + } + + return 1; +} + int main(int argc, char *argv[]) { char *end; @@ -170,35 +204,9 @@ int main(int argc, char *argv[]) || set_slave_addr(file, address, force)) exit(1); - if (!yes) { - int dont = 0; - - fprintf(stderr, "WARNING! This program can confuse your I2C " - "bus, cause data loss and worse!\n"); - - if (address >= 0x50 && address <= 0x57) { - fprintf(stderr, "DANGEROUS! Writing to a serial " - "EEPROM on a memory DIMM\nmay render your " - "memory USELESS and make your system " - "UNBOOTABLE!\n"); - dont = 1; - } - - fprintf(stderr, "I will write to device file %s, chip address " - "0x%02x, data address\n0x%02x, data 0x%02x%s, mode " - "%s.\n", filename, address, daddress, value, - vmask ? " (masked)" : "", - size == I2C_SMBUS_BYTE_DATA ? "byte" : "word"); - if (pec) - fprintf(stderr, "PEC checking enabled.\n"); - - fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n"); - fflush(stderr); - if (!user_ack(!dont)) { - fprintf(stderr, "Aborting on user request.\n"); - exit(0); - } - } + if (!yes && !confirm(filename, address, size, daddress, + value, vmask, pec)) + exit(0); if (vmask) { int oldvalue;