db_getlong.c

Go to the documentation of this file.
00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996, 1997, 1998, 1999, 2000
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 
00008 #include "config.h"
00009 
00010 #ifndef lint
00011 static const char revid[] = "$Id: db__getlong_8c-source.html,v 1.1 2008/06/08 10:17:40 sebdiaz Exp $";
00012 #endif /* not lint */
00013 
00014 #ifndef NO_SYSTEM_INCLUDES
00015 #include <sys/types.h>
00016 
00017 #include <errno.h>
00018 #include <limits.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 #endif
00022 
00023 #include "db_int.h"
00024 
00025 /*
00026  * CDB___db_getlong --
00027  *      Return a long value inside of basic parameters.
00028  *
00029  * PUBLIC: int CDB___db_getlong
00030  * PUBLIC:     __P((DB *, const char *, char *, long, long, long *));
00031  */
00032 int
00033 CDB___db_getlong(dbp, CDB_progname, p, min, max, storep)
00034         DB *dbp;
00035         const char *CDB_progname;
00036         char *p;
00037         long min, max, *storep;
00038 {
00039         long val;
00040         char *end;
00041 
00042         CDB___os_set_errno(0);
00043         val = strtol(p, &end, 10);
00044         if ((val == LONG_MIN || val == LONG_MAX) &&
00045             CDB___os_get_errno() == ERANGE) {
00046                 if (dbp == NULL) {
00047                         fprintf(stderr,
00048                             "%s: %s: %s\n", CDB_progname, p, strerror(ERANGE));
00049                         exit(1);
00050                 }
00051                 dbp->err(dbp, ERANGE, "%s", p);
00052                 return (1);
00053         }
00054         if (p[0] == '\0' || (end[0] != '\0' && end[0] != '\n')) {
00055                 if (dbp == NULL) {
00056                         fprintf(stderr,
00057                             "%s: %s: Invalid numeric argument\n", CDB_progname, p);
00058                         exit(1);
00059                 }
00060                 dbp->errx(dbp, "%s: Invalid numeric argument", p);
00061                 return (1);
00062         }
00063         if (val < min) {
00064                 if (dbp == NULL) {
00065                         fprintf(stderr,
00066                             "%s: %s: Less than minimum value (%ld)\n",
00067                             CDB_progname, p, min);
00068                         exit(1);
00069                 }
00070                 dbp->errx(dbp, "%s: Less than minimum value (%ld)", p, min);
00071                 return (1);
00072         }
00073         if (val > max) {
00074                 if (dbp == NULL) {
00075                         fprintf(stderr,
00076                             "%s: %s: Greater than maximum value (%ld)\n",
00077                             CDB_progname, p, max);
00078                         exit(1);
00079                 }
00080                 dbp->errx(dbp, "%s: Greater than maximum value (%ld)", p, max);
00081                 exit(1);
00082         }
00083         *storep = val;
00084         return (0);
00085 }
00086 
00087 /*
00088  * CDB___db_getulong --
00089  *      Return an unsigned long value inside of basic parameters.
00090  *
00091  * PUBLIC: int CDB___db_getulong
00092  * PUBLIC:     __P((DB *, const char *, char *, u_long, u_long, u_long *));
00093  */
00094 int
00095 CDB___db_getulong(dbp, CDB_progname, p, min, max, storep)
00096         DB *dbp;
00097         const char *CDB_progname;
00098         char *p;
00099         u_long min, max, *storep;
00100 {
00101 #if !defined(HAVE_STRTOUL)
00102         COMPQUIET(min, 0);
00103 
00104         return (CDB___db_getlong(dbp, CDB_progname, p, 0, max, storep));
00105 #else
00106 
00107         u_long val;
00108         char *end;
00109 
00110         CDB___os_set_errno(0);
00111         val = strtoul(p, &end, 10);
00112         if (val == ULONG_MAX && CDB___os_get_errno() == ERANGE) {
00113                 if (dbp == NULL) {
00114                         fprintf(stderr,
00115                             "%s: %s: %s\n", CDB_progname, p, strerror(ERANGE));
00116                         exit(1);
00117                 }
00118                 dbp->err(dbp, ERANGE, "%s", p);
00119                 return (1);
00120         }
00121         if (p[0] == '\0' || (end[0] != '\0' && end[0] != '\n')) {
00122                 if (dbp == NULL) {
00123                         fprintf(stderr,
00124                             "%s: %s: Invalid numeric argument\n", CDB_progname, p);
00125                         exit(1);
00126                 }
00127                 dbp->errx(dbp, "%s: Invalid numeric argument", p);
00128                 return (1);
00129         }
00130         if (val < min) {
00131                 if (dbp == NULL) {
00132                         fprintf(stderr,
00133                             "%s: %s: Less than minimum value (%ld)\n",
00134                             CDB_progname, p, min);
00135                         exit(1);
00136                 }
00137                 dbp->errx(dbp, "%s: Less than minimum value (%ld)", p, min);
00138                 return (1);
00139         }
00140 
00141         /*
00142          * We allow a 0 to substitute as a max value for ULONG_MAX because
00143          * 1) accepting only a 0 value is unlikely to be necessary, and 2)
00144          * we don't want callers to have to use ULONG_MAX explicitly, as it
00145          * may not exist on all platforms.
00146          */
00147         if (max != 0 && val > max) {
00148                 if (dbp == NULL) {
00149                         fprintf(stderr,
00150                             "%s: %s: Greater than maximum value (%ld)\n",
00151                             CDB_progname, p, max);
00152                         exit(1);
00153                 }
00154                 dbp->errx(dbp, "%s: Greater than maximum value (%ld)", p, max);
00155                 exit(1);
00156         }
00157         *storep = val;
00158         return (0);
00159 #endif  /* !defined(HAVE_STRTOUL) */
00160 }

Generated on Sun Jun 8 10:56:36 2008 for GNUmifluz by  doxygen 1.5.5