Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / wodim / defaults.c
1 /* 
2  * Copyright 2006 Eduard Bloch 
3  *
4  * This code emulates the interface of the original defaults.c file. However,
5  * it improves its behaviour and deals with corner cases: prepended and
6  * trailing spaces on variable and value, no requirement for using TABs
7  * anymore. No requirements to insert dummy values like -1 or "".
8  *
9  */
10 /*
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2
13  * as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; see the file COPYING.  If not, write to the Free Software
22  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  */
24
25 #include <mconfig.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <deflts.h>
29 #include <ctype.h>
30 #include <string.h>
31
32 #define CFGPATH "/etc/wodim.conf"
33 /* The better way would be exporting the meta functions to getnum.h or so */
34 extern int      getnum(char *arg, long *valp);
35
36 void 
37 cdr_defaults(char **p_dev_name, int *p_speed, long *p_fifosize, 
38                                         char **p_drv_opts)
39 {
40         char *t; /* tmp */
41         int wc=0;
42         char loc[256], sSpeed[11], sFs[11], sOpts[81];
43         char *devcand=NULL;
44
45   cfg_open(CFGPATH);
46
47         if(p_dev_name && *p_dev_name)
48                 devcand=*p_dev_name;
49         else if(NULL!=(t=getenv("CDR_DEVICE")))
50                 devcand=t;
51         else if(NULL!=(t=cfg_get("CDR_DEVICE")))
52                 devcand=strdup(t); /* needs to use it as a key later, same stat. memory */
53
54         if(devcand && NULL != (t=cfg_get(devcand))) {
55                 /* extract them now, may be used later */
56                 wc=sscanf(t, "%255s %10s %10s %80s", loc, sSpeed, sFs, sOpts);
57         }
58
59         if(p_dev_name) {
60                 if(wc>0)
61                         *p_dev_name = strdup(loc);
62                 else if(devcand) /* small mem. leak possible, does not matter, checks for that would require more code size than we loose */
63                         *p_dev_name=strdup(devcand);
64         }
65         if(p_speed) { /* sth. to write back */
66                 char *bad;
67                 int cfg_speed=-1;
68
69                 /* that value may be used twice */
70                 if(NULL!=(t=cfg_get("CDR_SPEED"))) {
71                         cfg_speed=strtol(t,&bad,10);
72                         if(*bad || cfg_speed<-1) {
73                                 fprintf(stderr, "Bad default CDR_SPEED setting (%s).\n", t);
74                                 exit(EXIT_FAILURE);
75                         }
76                 }
77
78                 if(*p_speed>0) { 
79                         /* ok, already set by the program arguments */
80                 }
81                 else if(NULL!=(t=getenv("CDR_SPEED"))) {
82                         *p_speed=strtol(t,&bad,10);
83                         if(*bad || *p_speed<-1) {
84                                 fprintf(stderr, "Bad CDR_SPEED environment (%s).\n", t);
85                                 exit(EXIT_FAILURE);
86                         }
87                 }
88                 else if(wc>1 && *sSpeed) {
89                         *p_speed=strtol(sSpeed, &bad, 10);
90                         if(*bad || *p_speed<-1) {
91                                 fprintf(stderr, "Bad speed (%s) in the config, drive description.\n", sSpeed);
92                                 exit(EXIT_FAILURE);
93                         }
94                         if(*p_speed==-1) 
95                                 /* that's autodetect, use the config default as last ressort */
96                                 *p_speed=cfg_speed;
97                 }
98                 else 
99                         *p_speed=cfg_speed;
100         }
101         if(p_fifosize) { /* sth. to write back */
102                 if(*p_fifosize>0) { 
103                         /* ok, already set by the user */
104                 }
105                 else if(NULL!=(t=getenv("CDR_FIFOSIZE"))) {
106                         if(getnum(t, p_fifosize)!=1 || *p_fifosize<-1) {
107                                 fprintf(stderr, "Bad CDR_FIFOSIZE environment (%s).\n", t);
108                                 exit(EXIT_FAILURE);
109                         }
110                 }
111                 else if(wc>2 && *sFs && strcmp("-1", sFs)) {
112                         if(getnum(sFs, p_fifosize)!=1 || *p_fifosize<-1) {
113                                 fprintf(stderr, "Bad fifo size (%s) in the config, device description.\n", sFs);
114                                 exit(EXIT_FAILURE);
115                         }
116                 }
117                 else if(NULL!=(t=cfg_get("CDR_FIFOSIZE"))) {
118                         if(getnum(t, p_fifosize)!=1 || *p_fifosize<-1) {
119                                 fprintf(stderr, "Bad speed default setting (%s).\n", t);
120                                 exit(EXIT_FAILURE);
121                         }
122                 }
123                 if(NULL!=(t=cfg_get("CDR_MAXFIFOSIZE"))) {
124                         long max;
125                         if(getnum(t, &max)!=1 || *p_fifosize<-1) {
126                                 fprintf(stderr, "Bad CDR_MAXFIFOSIZE setting (%s).\n", t);
127                                 exit(EXIT_FAILURE);
128                         }
129                         if(*p_fifosize>max)
130                                 *p_fifosize=max;
131                 }
132         }
133
134         if(p_drv_opts && !*p_drv_opts && wc>3 && strcmp(sOpts, "\"\""))
135                 *p_drv_opts=strdup(sOpts);
136
137   cfg_close();
138
139 }