- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# rpmio/argv.c

Go to the documentation of this file.

    00001
    00005 #include "system.h"
    00006
    00007 #include "rpmio_internal.h"     /* XXX fdGetFILE() */
    00008 #include <argv.h>
    00009
    00010 #include "debug.h"
    00011
    00012 /*@access FD_t @*/              /* XXX fdGetFILE() */
    00013
    00014 void argvPrint(const char * msg, ARGV_t argv, FILE * fp)
    00015 {
    00016     ARGV_t av;
    00017
    00018     if (fp == NULL) fp = stderr;
    00019
    00020     if (msg)
    00021         fprintf(fp, "===================================== %s\n", msg);
    00022
    00023     if (argv)
    00024     for (av = argv; *av; av++)
    00025         fprintf(fp, "%s\n", *av);
    00026
    00027 }
    00028
    00029 ARGI_t argiFree(ARGI_t argi)
    00030 {
    00031     if (argi) {
    00032         argi->nvals = 0;
    00033         argi->vals = _free(argi->vals);
    00034     }
    00035     argi = _free(argi);
    00036     return NULL;
    00037 }
    00038
    00039 ARGV_t argvFree(/*@only@*/ /*@null@*/ ARGV_t argv)
    00040 {
    00041     ARGV_t av;
    00042
    00043     if (argv)
    00044     for (av = argv; *av; av++)
    00045         *av = _free(*av);
    00046     argv = _free(argv);
    00047     return NULL;
    00048 }
    00049
    00050 int argiCount(ARGI_t argi)
    00051 {
    00052     int nvals = 0;
    00053     if (argi)
    00054         nvals = argi->nvals;
    00055     return nvals;
    00056 }
    00057
    00058 ARGint_t argiData(ARGI_t argi)
    00059 {
    00060     ARGint_t vals = NULL;
    00061     if (argi && argi->nvals > 0)
    00062         vals = argi->vals;
    00063     return vals;
    00064 }
    00065
    00066 int argvCount(const ARGV_t argv)
    00067 {
    00068     int argc = 0;
    00069     if (argv)
    00070     while (argv[argc] != NULL)
    00071         argc++;
    00072     return argc;
    00073 }
    00074
    00075 ARGV_t argvData(ARGV_t argv)
    00076 {
    00077 /*@-retalias -temptrans @*/
    00078     return argv;
    00079 /*@=retalias =temptrans @*/
    00080 }
    00081
    00082 int argvCmp(const void * a, const void * b)
    00083 {
    00084     ARGstr_t astr = *(ARGV_t)a;
    00085     ARGstr_t bstr = *(ARGV_t)b;
    00086     return strcmp(astr, bstr);
    00087 }
    00088
    00089 int argvStrcasecmp(const void * a, const void * b)
    00090 {
    00091     ARGstr_t astr = *(ARGV_t)a;
    00092     ARGstr_t bstr = *(ARGV_t)b;
    00093     return xstrcasecmp(astr, bstr);
    00094 }
    00095
    00096 #if defined(RPM_VENDOR_OPENPKG) /* wildcard-matching-arbitrary-tagnames */
    00097 int argvFnmatch(const void * a, const void * b)
    00098 {
    00099     ARGstr_t astr = *(ARGV_t)a;
    00100     ARGstr_t bstr = *(ARGV_t)b;
    00101     return (fnmatch(astr, bstr, 0) == 0 ? 0 : 1);
    00102 }
    00103
    00104 int argvFnmatchCasefold(const void * a, const void * b)
    00105 {
    00106     ARGstr_t astr = *(ARGV_t)a;
    00107     ARGstr_t bstr = *(ARGV_t)b;
    00108     return (fnmatch(astr, bstr, FNM_CASEFOLD) == 0 ? 0 : 1);
    00109 }
    00110 #endif
    00111
    00112 int argvSort(ARGV_t argv, int (*compar)(const void *, const void *))
    00113 {
    00114     if (compar == NULL)
    00115         compar = argvCmp;
    00116     qsort(argv, argvCount(argv), sizeof(*argv), compar);
    00117     return 0;
    00118 }
    00119
    00120 ARGV_t argvSearch(ARGV_t argv, ARGstr_t val,
    00121                 int (*compar)(const void *, const void *))
    00122 {
    00123     if (argv == NULL)
    00124         return NULL;
    00125     if (compar == NULL)
    00126         compar = argvCmp;
    00127     return bsearch(&val, argv, argvCount(argv), sizeof(*argv), compar);
    00128 }
    00129
    00130 #if defined(RPM_VENDOR_OPENPKG) /* wildcard-matching-arbitrary-tagnames */
    00131 ARGV_t argvSearchLinear(ARGV_t argv, ARGstr_t val,
    00132                 int (*compar)(const void *, const void *))
    00133 {
    00134     ARGV_t result;
    00135     ARGV_t av;
    00136     if (argv == NULL)
    00137         return NULL;
    00138     if (compar == NULL)
    00139         compar = argvCmp;
    00140     result = NULL;
    00141     for (av = argv; *av != NULL; av++) {
    00142         if (compar(av, &val) == 0) {
    00143             result = av;
    00144             break;
    00145         }
    00146     }
    00147     return result;
    00148 }
    00149 #endif
    00150
    00151 int argiAdd(/*@out@*/ ARGI_t * argip, int ix, int val)
    00152 {
    00153     ARGI_t argi;
    00154
    00155     if (argip == NULL)
    00156         return -1;
    00157     if (*argip == NULL)
    00158         *argip = xcalloc(1, sizeof(**argip));
    00159     argi = *argip;
    00160     if (ix < 0)
    00161         ix = argi->nvals;
    00162     if (ix >= argi->nvals) {
    00163         argi->vals = xrealloc(argi->vals, (ix + 1) * sizeof(*argi->vals));
    00164         memset(argi->vals + argi->nvals, 0,
    00165                 (ix - argi->nvals) * sizeof(*argi->vals));
    00166         argi->nvals = ix + 1;
    00167     }
    00168     argi->vals[ix] = val;
    00169     return 0;
    00170 }
    00171
    00172 int argvAdd(/*@out@*/ ARGV_t * argvp, ARGstr_t val)
    00173 {
    00174     ARGV_t argv;
    00175     int argc;
    00176
    00177     if (argvp == NULL)
    00178         return -1;
    00179     argc = argvCount(*argvp);
    00180 /*@-unqualifiedtrans@*/
    00181     *argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp));
    00182 /*@=unqualifiedtrans@*/
    00183     argv = *argvp;
    00184     argv[argc++] = xstrdup(val);
    00185     argv[argc  ] = NULL;
    00186     return 0;
    00187 }
    00188
    00189 int argvAppend(/*@out@*/ ARGV_t * argvp, const ARGV_t av)
    00190 {
    00191     ARGV_t argv = *argvp;
    00192     int argc = argvCount(argv);
    00193     int ac = argvCount(av);
    00194     int i;
    00195
    00196     argv = xrealloc(argv, (argc + ac + 1) * sizeof(*argv));
    00197     for (i = 0; i < ac; i++)
    00198         argv[argc + i] = xstrdup(av[i]);
    00199     argv[argc + ac] = NULL;
    00200     *argvp = argv;
    00201     return 0;
    00202 }
    00203
    00204 int argvSplit(ARGV_t * argvp, const char * str, const char * seps)
    00205 {
    00206     static char whitespace[] = " \f\n\r\t\v";
    00207     char * dest = xmalloc(strlen(str) + 1);
    00208     ARGV_t argv;
    00209     int argc = 1;
    00210     const char * s;
    00211     char * t;
    00212     int c;
    00213
    00214     if (seps == NULL)
    00215         seps = whitespace;
    00216
    00217     for (argc = 1, s = str, t = dest; (c = (int) *s); s++, t++) {
    00218         if (strchr(seps, c) && !(s[0] == ':' && s[1] == '/' && s[2] == '/')) {
    00219             argc++;
    00220             c = (int) '\0';
    00221         }
    00222         *t = (char) c;
    00223     }
    00224     *t = '\0';
    00225
    00226     argv = xmalloc( (argc + 1) * sizeof(*argv));
    00227
    00228     for (c = 0, s = dest; s < t; s += strlen(s) + 1) {
    00229         /* XXX Skip repeated seperators (i.e. whitespace). */
    00230         if (seps == whitespace && s[0] == '\0')
    00231             continue;
    00232         argv[c++] = xstrdup(s);
    00233     }
    00234     argv[c] = NULL;
    00235     *argvp = argv;
    00236 /*@-nullstate@*/
    00237     return 0;
    00238 /*@=nullstate@*/
    00239 }
    00240
    00241 char * argvJoin(ARGV_t argv)
    00242 {
    00243     size_t nb = 0;
    00244     int argc;
    00245     char *t, *te;
    00246
    00247     for (argc = 0; argv[argc] != NULL; argc++) {
    00248         if (argc != 0)
    00249             nb++;
    00250         nb += strlen(argv[argc]);
    00251     }
    00252     nb++;
    00253
    00254     te = t = xmalloc(nb);
    00255     *te = '\0';
    00256     for (argc = 0; argv[argc] != NULL; argc++) {
    00257         if (argc != 0)
    00258             *te++ = ' ';
    00259         te = stpcpy(te, argv[argc]);
    00260     }
    00261     *te = '\0';
    00262     return t;
    00263 }
    00264
    00265 int argvFgets(ARGV_t * argvp, void * fd)
    00266 {
    00267     FILE * fp = (fd ? fdGetFILE(fd) : stdin);
    00268     ARGV_t av = NULL;
    00269     char buf[BUFSIZ];
    00270     char * b, * be;
    00271     int rc = 0;
    00272
    00273     if (fp == NULL)
    00274         return -2;
    00275     while (!rc && (b = fgets(buf, (int)sizeof(buf), fp)) != NULL) {
    00276         buf[sizeof(buf)-1] = '\0';
    00277         be = b + strlen(buf) - 1;
    00278         while (strchr("\r\n", *be) != NULL)
    00279             *be-- = '\0';
    00280         rc = argvAdd(&av, b);
    00281     }
    00282
    00283     if (!rc)
    00284         rc = ferror(fp);
    00285     if (!rc)
    00286         rc = (feof(fp) ? 0 : 1);
    00287     if (!rc && argvp)
    00288         *argvp = av;
    00289     else
    00290         av = argvFree(av);
    00291
    00292 /*@-nullstate@*/        /* XXX *argvp may be NULL. */
    00293     return rc;
    00294 /*@=nullstate@*/
    00295 }