A friend has forwarded this to me :
Hello,
first i'd like to thank you for taking the time to write this great software,
it is really nice to extract xb isos on unix (i was using some windows
app before... what a pain)
Short story:
I tried to use xbiso on OpenBSD, the extracting part works perfectly,
but i've had some problems with options handling:
getopt() from OpenBSD stops reading argv when it encounters a
non-option argument
so the options are never read on this platform since the filename has
to come first.
From what i can see in the man pages, freebsd and netbsd also acts like this.
(getopt() probably didn't change much since the 4.4BSD fork...)
http://www.freebsd.org/cgi/man.cgi?query=getopt&apropos=0&sektion=3&manpath=FreeBSD+6.2-RELEASE&format=html
http://netbsd.gw.com/cgi-bin/man-cgi?getopt+3+NetBSD-current
http://www.openbsd.org/cgi-bin/man.cgi?query=getopt&apropos=0&sektion=3&manpath=OpenBSD+Current&arch=i386&format=html
"[...] When all options have been processed (i.e., up to the first
non-option argument), getopt() returns -1."
I've been wondering why xbiso requires the filename to be the first argument,
since almost all unix programs i've used usually get the options
first, then the filename
(ls comes to mind, grep is also a good example)
So there I was, thinking about this and looking at the code.
I figured it would be easy to change it to get the options first, then
the filename.
Here's the resulting diff:
---------------8<--------8<----[snip]------8<------8<--------
--- xbiso.c 2005-06-13 07:53:20.000000000 -0400
+++ ../xbiso-0.6.1-diff/xbiso.c 2007-04-11 20:57:19.000000000 -0400
@@ -95,7 +95,7 @@
int main(int argc, char *argv[]) {
- char *dbuf,*fname;
+ char *dbuf=NULL,*fname;
long dtable, dtablesize;
int diri=0,ret;
OFFT cpos;
@@ -121,14 +121,8 @@
xbftell = ftello64;
#endif
- if(argv[1] == NULL) { help=1; err(argv[0]); }
+ if(argc < 2) { help=1; err(argv[0]); }
- fname = malloc(strlen(argv[1])+1);
- memset(fname,0,strlen(argv[1])+1);
- dbuf = malloc(1);
- memset(dbuf, 0, 1);
- strcpy(fname,argv[1]);
-
#ifdef USE_FTP
while((ret = getopt(argc,argv,"h:u:p:i:fxvd:")) != -1)
#else
@@ -166,8 +160,7 @@
break;
#endif
case 'd':
- dbuf = realloc(dbuf, (size_t)(strlen(optarg))+1);
- memset(dbuf, 0, (size_t)(strlen(optarg))+1);
+ dbuf = calloc(1, (size_t)(strlen(optarg))+1);
snprintf(dbuf,(size_t)(strlen(optarg))+1,"%s",optarg);
break;
case '?':
@@ -177,10 +170,16 @@
}
}
+ argc -= optind;
+ if(!argc){help=1; err(argv[0]); }
+
+ argv += optind;
+ fname = calloc(1,strlen(argv[0])+1);
+ strcpy(fname,argv[0]);
+
//set the dirname to the filename - ext if blank
- if(strcmp(dbuf,"")==0) {
- dbuf = realloc(dbuf, (size_t)(strlen((fname))));
- memset(dbuf, 0, (size_t)(strlen((fname))));
+ if(dbuf==NULL) {
+ dbuf = calloc(1, (size_t)(strlen((fname))));
snprintf(dbuf,(size_t)(strlen(fname))-3,"%s",fname);
}
@@ -385,7 +384,7 @@
License for more details. \n \
\n\n",version,platform);
- printf("%s filename [options]\n",error);
+ printf("%s [options] filename\n",error);
printf(" -v verbose\n");
printf(" -x enable xiso <=1.10 compat\n");
printf(" -d (dirname) directory to extract to\n");
---------------8<--------8<----[snip]------8<------8<--------
(hopefully gmail hasn't mangled that diff too much, i've attached it,
just in case)
As you can see, i've changed the initialisation of the dbuf variable
since i moved the filename handling further down and it had to be
initialised in the getopt loop.
I've also replaced (re)malloc() + memset(0) combos by calloc,
this is probably just a programming reflex of mine, i don't like
reallocs.. but that's just me.
i've changed the argument detection line:
if(argv[1] == NULL) { help=1; err(argv[0]); }
xbiso would segfault if started like this:
xbiso ""
I was about to write that this solved the problem, it would be wrong...
it works on linux, but not on openbsd
snprintf(dbuf,(size_t)(strlen(fname))-3,"%s",fname);
this line causes the sigsegv.
The argv +=optind and argv -= optind are just a little trick to find
out if there's a remaning argument (the filename)
only the first remaning argument is used
That's about it, what do you think?
Thanks again for writing this software.
--
Jean
Patch