2929#include <string.h>
3030#include <strings.h>
3131#include <errno.h>
32- #include <ctype.h>
3332#include <assert.h>
3433#include <unistd.h>
3534#include <getopt.h>
3635
3736#include <xbps.h>
37+
3838#include "../xbps-install/defs.h"
3939
4040static void __attribute__((noreturn ))
8888main (int argc , char * * argv )
8989{
9090 xbps_dictionary_t dict ;
91- struct xbps_handle xh ;
91+ struct xbps_handle xh = { 0 } ;
9292 struct xferstat xfer ;
93- const char * version , * rootdir = NULL , * confdir = NULL ;
93+ const char * version ;
9494 char pkgname [XBPS_NAME_SIZE ], * filename ;
95- int flags = 0 , c , rv = 0 , i = 0 ;
95+ int c , rv = 0 , i = 0 ;
9696 const struct option longopts [] = {
9797 { "config" , required_argument , NULL , 'C' },
9898 { "debug" , no_argument , NULL , 'd' },
@@ -108,17 +108,17 @@ main(int argc, char **argv)
108108 usage (false);
109109 /* NOTREACHED */
110110 case 'C' :
111- confdir = optarg ;
111+ xbps_strlcpy ( xh . confdir , optarg , sizeof ( xh . confdir )) ;
112112 break ;
113113 case 'r' :
114114 /* To specify the root directory */
115- rootdir = optarg ;
115+ xbps_strlcpy ( xh . rootdir , optarg , sizeof ( xh . rootdir )) ;
116116 break ;
117117 case 'd' :
118- flags |= XBPS_FLAG_DEBUG ;
118+ xh . flags |= XBPS_FLAG_DEBUG ;
119119 break ;
120120 case 'v' :
121- flags |= XBPS_FLAG_VERBOSE ;
121+ xh . flags |= XBPS_FLAG_VERBOSE ;
122122 break ;
123123 case 'V' :
124124 printf ("%s\n" , XBPS_RELVER );
@@ -139,8 +139,6 @@ main(int argc, char **argv)
139139 /* NOTREACHED */
140140 }
141141
142- memset (& xh , 0 , sizeof (xh ));
143-
144142 if ((strcmp (argv [0 ], "version" ) == 0 ) ||
145143 (strcmp (argv [0 ], "real-version" ) == 0 ) ||
146144 (strcmp (argv [0 ], "arch" ) == 0 ) ||
@@ -151,11 +149,6 @@ main(int argc, char **argv)
151149 */
152150 xh .fetch_cb = fetch_file_progress_cb ;
153151 xh .fetch_cb_data = & xfer ;
154- xh .flags = flags ;
155- if (rootdir )
156- xbps_strlcpy (xh .rootdir , rootdir , sizeof (xh .rootdir ));
157- if (confdir )
158- xbps_strlcpy (xh .confdir , confdir , sizeof (xh .confdir ));
159152 if ((rv = xbps_init (& xh )) != 0 ) {
160153 xbps_error_printf ("xbps-uhelper: failed to "
161154 "initialize libxbps: %s.\n" , strerror (rv ));
@@ -176,8 +169,10 @@ main(int argc, char **argv)
176169 xbps_error_printf ("Could not find package '%s'\n" , argv [i ]);
177170 rv = 1 ;
178171 } else {
179- xbps_dictionary_get_cstring_nocopy (dict , "pkgver" , & version );
180- printf ("%s\n" , xbps_pkg_version (version ));
172+ const char * pkgver = NULL ;
173+ if (!xbps_dictionary_get_cstring_nocopy (dict , "pkgver" , & pkgver ))
174+ xbps_unreachable ();
175+ printf ("%s\n" , xbps_pkg_version (pkgver ));
181176 }
182177 }
183178 } else if (strcmp (argv [0 ], "real-version" ) == 0 ) {
@@ -356,13 +351,13 @@ main(int argc, char **argv)
356351 }
357352 rv = xbps_pkgpattern_match (argv [1 ], argv [2 ]);
358353 if (rv >= 0 ) {
359- if (flags & XBPS_FLAG_VERBOSE ) {
354+ if (xh . flags & XBPS_FLAG_VERBOSE ) {
360355 fprintf (stderr , "%s %s %s\n" ,
361356 argv [1 ],
362357 (rv == 1 ) ? "matches" : "does not match" ,
363358 argv [2 ]);
364359 }
365- } else if (flags & XBPS_FLAG_VERBOSE ) {
360+ } else if (xh . flags & XBPS_FLAG_VERBOSE ) {
366361 xbps_error_printf ("%s: not a pattern\n" , argv [2 ]);
367362 }
368363 exit (rv );
@@ -374,7 +369,7 @@ main(int argc, char **argv)
374369 }
375370
376371 rv = xbps_cmpver (argv [1 ], argv [2 ]);
377- if (flags & XBPS_FLAG_VERBOSE ) {
372+ if (xh . flags & XBPS_FLAG_VERBOSE ) {
378373 fprintf (stderr , "%s %s %s\n" ,
379374 argv [1 ],
380375 (rv == 1 ) ? ">" : ((rv == 0 ) ? "=" : "<" ),
0 commit comments