/************************************************************************************************** $Header: /pub/cvsroot/yencode/lib/getoptstr.c,v 1.2 2002/03/10 16:22:19 bboy Exp $ Copyright (C) 2002 Don Moore This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at Your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA **************************************************************************************************/ #include "misc.h" /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ GETOPTSTR Given a list of long opts, constructs the opt string automatically, since I always forget to update it. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ char * getoptstr(struct option const longopts[]) { char *optstr, *c; register int n, opts; // Count number of options for (opts = 0; longopts[opts].name; opts++) ; // Allocate optstr optstr = (char *)xmalloc(((opts * 3) + 1) * sizeof(char)); memset(optstr, 0, (opts * 3) + 1); // Build optstr for (c = optstr, n = 0; n < opts; n++) { if (longopts[n].val) { *c++ = longopts[n].val; if (longopts[n].has_arg == required_argument) *c++ = ':'; else if (longopts[n].has_arg == optional_argument) { *c++ = ':'; *c++ = ':'; } } } return (optstr); } /*--- getoptstr() -------------------------------------------------------------------------------*/ /* vi:set ts=3: */