Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / wodim / getnum.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)getnum.c 1.2 04/03/02 Copyright 1984-2002, 2004 J. Schilling */
14 /*
15  *      Number conversion routines to implement 'dd' like options.
16  *
17  *      Copyright (c) 1984-2002, 2004 J. Schilling
18  */
19 /*
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License version 2
22  * as published by the Free Software Foundation.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License along with
30  * this program; see the file COPYING.  If not, write to the Free Software
31  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32  */
33
34 #include <mconfig.h>
35 #include <standard.h>
36 #include <utypes.h>
37 #include <schily.h>
38
39 static  Llong   number(char *arg, int *retp);
40 int     getnum(char *arg, long *valp);
41 int     getllnum(char *arg, Llong *lvalp);
42
43 static Llong
44 number(register char *arg, int *retp)
45 {
46         Llong   val     = 0;
47
48         if (*retp != 1)
49                 return (val);
50         if (*arg == '\0') {
51                 *retp = -1;
52         } else if (*(arg = astoll(arg, &val))) {
53                 if (*arg == 'p' || *arg == 'P') {
54                         val *= (1024*1024);
55                         val *= (1024*1024*1024);
56                         arg++;
57
58                 } else if (*arg == 't' || *arg == 'T') {
59                         val *= (1024*1024);
60                         val *= (1024*1024);
61                         arg++;
62
63                 } else if (*arg == 'g' || *arg == 'G') {
64                         val *= (1024*1024*1024);
65                         arg++;
66
67                 } else if (*arg == 'm' || *arg == 'M') {
68                         val *= (1024*1024);
69                         arg++;
70
71                 } else if (*arg == 'f' || *arg == 'F') {
72                         val *= 2352;
73                         arg++;
74
75                 } else if (*arg == 's' || *arg == 'S') {
76                         val *= 2048;
77                         arg++;
78
79                 } else if (*arg == 'k' || *arg == 'K') {
80                         val *= 1024;
81                         arg++;
82
83                 } else if (*arg == 'b' || *arg == 'B') {
84                         val *= 512;
85                         arg++;
86
87                 } else if (*arg == 'w' || *arg == 'W') {
88                         val *= 2;
89                         arg++;
90                 }
91                 if (*arg == '*' || *arg == 'x')
92                         val *= number(++arg, retp);
93                 else if (*arg != '\0')
94                         *retp = -1;
95         }
96         return (val);
97 }
98
99 int
100 getnum(char *arg, long *valp)
101 {
102         Llong   llval;
103         int     ret = 1;
104
105         llval = number(arg, &ret);
106         *valp = llval;
107         if (*valp != llval) {
108                 errmsgno(EX_BAD,
109                         "Value %lld is too large for data type 'long'.\n",
110                                                                         llval);
111                 ret = -1;
112         }
113         return (ret);
114 }
115
116 int
117 getllnum(char *arg, Llong *lvalp)
118 {
119         int     ret = 1;
120
121         *lvalp = number(arg, &ret);
122         return (ret);
123 }