Back to index

glibc  2.9
globtest.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <getopt.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <glob.h>
00024 
00025 int
00026 main (int argc, char *argv[])
00027 {
00028   int i, j;
00029   int glob_flags = 0;
00030   glob_t g;
00031   int quotes = 1;
00032 
00033   g.gl_offs = 0;
00034 
00035   while ((i = getopt (argc, argv, "bcdeEgmopqstT")) != -1)
00036     switch(i)
00037       {
00038       case 'b':
00039        glob_flags |= GLOB_BRACE;
00040        break;
00041       case 'c':
00042        glob_flags |= GLOB_NOCHECK;
00043        break;
00044       case 'd':
00045        glob_flags |= GLOB_ONLYDIR;
00046        break;
00047       case 'e':
00048        glob_flags |= GLOB_NOESCAPE;
00049        break;
00050       case 'E':
00051        glob_flags |= GLOB_ERR;
00052        break;
00053       case 'g':
00054        glob_flags |= GLOB_NOMAGIC;
00055        break;
00056       case 'm':
00057        glob_flags |= GLOB_MARK;
00058        break;
00059       case 'o':
00060        glob_flags |= GLOB_DOOFFS;
00061        g.gl_offs = 1;
00062        break;
00063       case 'p':
00064        glob_flags |= GLOB_PERIOD;
00065        break;
00066       case 'q':
00067        quotes = 0;
00068        break;
00069       case 's':
00070        glob_flags |= GLOB_NOSORT;
00071        break;
00072       case 't':
00073        glob_flags |= GLOB_TILDE;
00074        break;
00075       case 'T':
00076        glob_flags |= GLOB_TILDE_CHECK;
00077        break;
00078       default:
00079        exit (-1);
00080       }
00081 
00082   if (optind >= argc || chdir (argv[optind]))
00083     exit(1);
00084 
00085   j = optind + 1;
00086   if (optind + 1 >= argc)
00087     exit (1);
00088 
00089   /* Do a glob on each remaining argument.  */
00090   for (j = optind + 1; j < argc; j++) {
00091     i = glob (argv[j], glob_flags, NULL, &g);
00092     if (i != 0)
00093       break;
00094     glob_flags |= GLOB_APPEND;
00095   }
00096 
00097   /* Was there an error? */
00098   if (i == GLOB_NOSPACE)
00099     puts ("GLOB_NOSPACE");
00100   else if (i == GLOB_ABORTED)
00101     puts ("GLOB_ABORTED");
00102   else if (i == GLOB_NOMATCH)
00103     puts ("GLOB_NOMATCH");
00104 
00105   /* If we set an offset, fill in the first field.
00106      (Unless glob() has filled it in already - which is an error) */
00107   if ((glob_flags & GLOB_DOOFFS) && g.gl_pathv[0] == NULL)
00108     g.gl_pathv[0] = (char *) "abc";
00109 
00110   /* Print out the names.  Unless otherwise specified, qoute them.  */
00111   if (g.gl_pathv)
00112     {
00113       for (i = 0; i < g.gl_offs + g.gl_pathc; ++i)
00114         printf ("%s%s%s\n", quotes ? "`" : "",
00115               g.gl_pathv[i] ? g.gl_pathv[i] : "(null)",
00116               quotes ? "'" : "");
00117     }
00118   return 0;
00119 }