Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_getlong.c

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

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2