From ff9aced222781506cb41442fb2ee2a175133fb1f Mon Sep 17 00:00:00 2001 From: Bart Massey Date: Thu, 7 Feb 2008 14:03:24 -0800 Subject: [PATCH] changed a bunch of string to number conversions for reliability; moved floats to doube --- xrandr.c | 95 ++++++++++++++++++++++++++++++++++++------------------------- 1 files changed, 56 insertions(+), 39 deletions(-) diff --git a/xrandr.c b/xrandr.c index e2a657f..674dc0b 100644 --- a/xrandr.c +++ b/xrandr.c @@ -263,7 +263,7 @@ struct _output { crtc_t *current_crtc_info; name_t mode; - float refresh; + double refresh; XRRModeInfo *mode_info; name_t addmode; @@ -317,7 +317,7 @@ static int num_crtcs; static XRRScreenResources *res; static int fb_width = 0, fb_height = 0; static int fb_width_mm = 0, fb_height_mm = 0; -static float dpi = 0; +static double dpi = 0; static char *dpi_output = NULL; static Bool dryrun = False; static int minWidth, maxWidth, minHeight, maxHeight; @@ -354,27 +354,27 @@ mode_width (XRRModeInfo *mode_info, Rotation rotation) } /* v refresh frequency in Hz */ -static float +static double mode_refresh (XRRModeInfo *mode_info) { - float rate; + double rate; if (mode_info->hTotal && mode_info->vTotal) - rate = ((float) mode_info->dotClock / - ((float) mode_info->hTotal * (float) mode_info->vTotal)); + rate = ((double) mode_info->dotClock / + ((double) mode_info->hTotal * (double) mode_info->vTotal)); else rate = 0; return rate; } /* h sync frequency in Hz */ -static float +static double mode_hsync (XRRModeInfo *mode_info) { - float rate; + double rate; if (mode_info->hTotal) - rate = (float) mode_info->dotClock / (float) mode_info->hTotal; + rate = (double) mode_info->dotClock / (double) mode_info->hTotal; else rate = 0; return rate; @@ -528,11 +528,11 @@ find_crtc_by_xid (RRCrtc crtc) } static XRRModeInfo * -find_mode (name_t *name, float refresh) +find_mode (name_t *name, double refresh) { int m; XRRModeInfo *best = NULL; - float bestDist = 0; + double bestDist = 0; for (m = 0; m < res->nmode; m++) { @@ -544,7 +544,7 @@ find_mode (name_t *name, float refresh) } if ((name->kind & name_string) && !strcmp (name->string, mode->name)) { - float dist; + double dist; if (refresh) dist = fabs (mode_refresh (mode) - refresh); @@ -587,7 +587,7 @@ find_mode_for_output (output_t *output, name_t *name) XRROutputInfo *output_info = output->output_info; int m; XRRModeInfo *best = NULL; - float bestDist = 0; + double bestDist = 0; for (m = 0; m < output_info->nmode; m++) { @@ -602,7 +602,7 @@ find_mode_for_output (output_t *output, name_t *name) } if ((name->kind & name_string) && !strcmp (name->string, mode->name)) { - float dist; + double dist; if (output->refresh) dist = fabs (mode_refresh (mode) - output->refresh); @@ -1593,6 +1593,26 @@ pick_crtcs (void) } int +check_strtol(char *s) +{ + char *endptr; + int result = strtol(s, &endptr, 10); + if (s == endptr) + usage(); + return result; +} + +int +check_strtod(char *s) +{ + char *endptr; + double result = strtod(s, &endptr); + if (s == endptr) + usage(); + return result; +} + +int main (int argc, char **argv) { XRRScreenSize *sizes; @@ -1610,7 +1630,7 @@ main (int argc, char **argv) int i, j; SizeID current_size; short current_rate; - float rate = -1; + double rate = -1; int size = -1; int dirind = 0; Bool setit = False; @@ -1657,10 +1677,8 @@ main (int argc, char **argv) if (++i>=argc) usage (); if (sscanf (argv[i], "%dx%d", &width, &height) == 2) have_pixel_size = True; - else { - size = atoi (argv[i]); - if (size < 0) usage(); - } + else + size = check_strtol(argv[i]); setit = True; continue; } @@ -1670,8 +1688,7 @@ main (int argc, char **argv) !strcmp ("--refresh", argv[i])) { if (++i>=argc) usage (); - if (sscanf (argv[i], "%f", &rate) != 1) - usage (); + rate = check_strtod(argv[i]); setit = True; #if HAS_RANDR_1_2 if (output) @@ -1701,8 +1718,7 @@ main (int argc, char **argv) } if (!strcmp ("--screen", argv[i])) { if (++i>=argc) usage (); - screen = atoi (argv[i]); - if (screen < 0) usage(); + screen = check_strtol(argv[i]); continue; } if (!strcmp ("-q", argv[i]) || !strcmp ("--query", argv[i])) { @@ -1712,8 +1728,8 @@ main (int argc, char **argv) if (!strcmp ("-o", argv[i]) || !strcmp ("--orientation", argv[i])) { char *endptr; if (++i>=argc) usage (); - dirind = strtol(argv[i], &endptr, 0); - if (*endptr != '\0') { + dirind = check_strtol(argv[i], &endptr, 10); + if (argv[i] == endptr) { for (dirind = 0; dirind < 4; dirind++) { if (strcmp (direction[dirind], argv[i]) == 0) break; } @@ -1876,8 +1892,10 @@ main (int argc, char **argv) continue; } if (!strcmp ("--dpi", argv[i])) { + char *strtod_error; if (++i>=argc) usage (); - if (sscanf (argv[i], "%f", &dpi) != 1) + dpi = strtod(argv[i], &strtod_error); + if (argv[i] == strtod_error) { dpi = 0.0; dpi_output = argv[i]; @@ -1919,25 +1937,24 @@ main (int argc, char **argv) if (!strcmp ("--newmode", argv[i])) { umode_t *m = malloc (sizeof (umode_t)); - float clock; + double clock; ++i; if (i + 9 >= argc) usage (); m->mode.name = argv[i]; m->mode.nameLength = strlen (argv[i]); i++; - if (sscanf (argv[i++], "%f", &clock) != 1) - usage (); + clock = check_strtod(argv[i++]); m->mode.dotClock = clock * 1e6; - if (sscanf (argv[i++], "%d", &m->mode.width) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.hSyncStart) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.hSyncEnd) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.hTotal) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.height) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.vSyncStart) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.vSyncEnd) != 1) usage(); - if (sscanf (argv[i++], "%d", &m->mode.vTotal) != 1) usage(); + m->mode.width = check_strtol(argv[i++]); + m->mode.hSyncStart = check_strtol(argv[i++]); + m->mode.hSyncEnd = check_strtol(argv[i++]); + m->mode.hTotal = check_strtol(argv[i++]); + m->mode.height = check_strtol(argv[i++]); + m->mode.vSyncStart = check_strtol(argv[i++]); + m->mode.vSyncEnd = check_strtol(argv[i++]); + m->mode.vTotal = check_strtol(argv[i++]); m->mode.modeFlags = 0; while (i < argc) { int f; @@ -2423,7 +2440,7 @@ main (int argc, char **argv) printf (" %s (0x%x) %6.1fMHz", mode->name, mode->id, - (float)mode->dotClock / 1000000.0); + (double)mode->dotClock / 1000000.0); for (f = 0; mode_flags[f].flag; f++) if (mode->modeFlags & mode_flags[f].flag) printf (" %s", mode_flags[f].string); @@ -2484,7 +2501,7 @@ main (int argc, char **argv) { printf (" %s (0x%x) %6.1fMHz\n", mode->name, mode->id, - (float)mode->dotClock / 1000000.0); + (double)mode->dotClock / 1000000.0); printf (" h: width %4d start %4d end %4d total %4d skew %4d clock %6.1fKHz\n", mode->width, mode->hSyncStart, mode->hSyncEnd, mode->hTotal, mode->hSkew, mode_hsync (mode) / 1000); -- 1.5.3.4