Back to index

glibc  2.9
Classes | Defines | Functions | Variables
wordexp-test.c File Reference
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wordexp.h>

Go to the source code of this file.

Classes

struct  test_case_struct

Defines

#define IFS   " \n\t"

Functions

static int testit (struct test_case_struct *tc)
static void command_line_test (const char *words)
int main (int argc, char *argv[])

Variables

struct test_case_struct test_case []
static int tests

Class Documentation

struct test_case_struct

Definition at line 31 of file wordexp-test.c.

Class Members
const char * env
int flags
const char * ifs
int retval
size_t wordc
const char * words
const char * wordv

Define Documentation

#define IFS   " \n\t"

Definition at line 29 of file wordexp-test.c.


Function Documentation

static void command_line_test ( const char *  words) [static]

Definition at line 218 of file wordexp-test.c.

{
  wordexp_t we;
  int i;
  int retval = wordexp (words, &we, 0);
  printf ("wordexp returned %d\n", retval);
  for (i = 0; i < we.we_wordc; i++)
    printf ("we_wordv[%d] = \"%s\"\n", i, we.we_wordv[i]);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 229 of file wordexp-test.c.

{
  const char *globfile[] = { "one", "two", "three", NULL };
  char tmpdir[32];
  struct passwd *pw;
  const char *cwd;
  int test;
  int fail = 0;
  int i;
  struct test_case_struct ts;

  if (argc > 1)
    {
      command_line_test (argv[1]);
      return 0;
    }

  cwd = getcwd (NULL, 0);

  /* Set up arena for pathname expansion */
  tmpnam (tmpdir);
  if (mkdir (tmpdir, S_IRWXU) || chdir (tmpdir))
    return -1;
  else
    {
      int fd;

      for (i = 0; globfile[i]; ++i)
       if ((fd = creat (globfile[i], S_IRUSR | S_IWUSR)) == -1
           || close (fd))
         return -1;
    }

  for (test = 0; test_case[test].retval != -1; test++)
    if (testit (&test_case[test]))
      ++fail;

  /* Tilde-expansion tests. */
  pw = getpwnam ("root");
  if (pw != NULL)
    {
      ts.retval = 0;
      ts.env = NULL;
      ts.words = "~root ";
      ts.flags = 0;
      ts.wordc = 1;
      ts.wordv[0] = pw->pw_dir;
      ts.ifs = IFS;

      if (testit (&ts))
       ++fail;

      ts.retval = 0;
      ts.env = pw->pw_dir;
      ts.words = "${var#~root}x";
      ts.flags = 0;
      ts.wordc = 1;
      ts.wordv[0] = "x";
      ts.ifs = IFS;

      if (testit (&ts))
       ++fail;
    }

  /* "~" expands to value of $HOME when HOME is set */

  setenv ("HOME", "/dummy/home", 1);
  ts.retval = 0;
  ts.env = NULL;
  ts.words = "~ ~/foo";
  ts.flags = 0;
  ts.wordc = 2;
  ts.wordv[0] = "/dummy/home";
  ts.wordv[1] = "/dummy/home/foo";
  ts.ifs = IFS;

  if (testit (&ts))
    ++fail;

  /* "~" expands to home dir from passwd file if HOME is not set */

  pw = getpwuid (getuid ());
  if (pw != NULL)
    {
      unsetenv ("HOME");
      ts.retval = 0;
      ts.env = NULL;
      ts.words = "~";
      ts.flags = 0;
      ts.wordc = 1;
      ts.wordv[0] = pw->pw_dir;
      ts.ifs = IFS;

      if (testit (&ts))
       ++fail;
    }

  puts ("tests completed, now cleaning up");

  /* Clean up */
  for (i = 0; globfile[i]; ++i)
    remove (globfile[i]);

  if (cwd == NULL)
    cwd = "..";

  chdir (cwd);
  rmdir (tmpdir);

  printf ("tests failed: %d\n", fail);

  return fail != 0;
}

Here is the call graph for this function:

static int testit ( struct test_case_struct tc) [static]

Definition at line 345 of file wordexp-test.c.

{
  int retval;
  wordexp_t we, sav_we;
  char *dummy;
  int bzzzt = 0;
  int start_offs = 0;
  int i;

  if (tc->env)
    setenv ("var", tc->env, 1);
  else
    unsetenv ("var");

  if (tc->ifs)
    setenv ("IFS", tc->ifs, 1);
  else
    unsetenv ("IFS");

  sav_we.we_wordc = 99;
  sav_we.we_wordv = &dummy;
  sav_we.we_offs = 3;
  we = sav_we;

  printf ("Test %d (%s): ", ++tests, tc->words);

  if (tc->flags & WRDE_APPEND)
    {
      /* initial wordexp() call, to be appended to */
      if (wordexp ("pre1 pre2", &we, tc->flags & ~WRDE_APPEND) != 0)
        {
         printf ("FAILED setup\n");
         return 1;
       }
    }
  retval = wordexp (tc->words, &we, tc->flags);

  if (tc->flags & WRDE_DOOFFS)
      start_offs = sav_we.we_offs;

  if (retval != tc->retval || (retval == 0 && we.we_wordc != tc->wordc))
    bzzzt = 1;
  else if (retval == 0)
    {
      for (i = 0; i < start_offs; ++i)
       if (we.we_wordv[i] != NULL)
         {
           bzzzt = 1;
           break;
         }

      for (i = 0; i < we.we_wordc; ++i)
       if (we.we_wordv[i+start_offs] == NULL ||
           strcmp (tc->wordv[i], we.we_wordv[i+start_offs]) != 0)
         {
           bzzzt = 1;
           break;
         }
    }

  if (bzzzt)
    {
      printf ("FAILED\n");
      printf ("Test words: <%s>, need retval %d, wordc %Zd\n",
             tc->words, tc->retval, tc->wordc);
      if (start_offs != 0)
       printf ("(preceded by %d NULLs)\n", start_offs);
      printf ("Got retval %d, wordc %Zd: ", retval, we.we_wordc);
      if (retval == 0 || retval == WRDE_NOSPACE)
       {
         for (i = 0; i < we.we_wordc + start_offs; ++i)
           if (we.we_wordv[i] == NULL)
             printf ("NULL ");
           else
             printf ("<%s> ", we.we_wordv[i]);
       }
      printf ("\n");
    }
  else if (retval != 0 && retval != WRDE_NOSPACE &&
          (we.we_wordc != sav_we.we_wordc ||
            we.we_wordv != sav_we.we_wordv ||
            we.we_offs != sav_we.we_offs))
    {
      bzzzt = 1;
      printf ("FAILED to restore wordexp_t members\n");
    }
  else
    printf ("OK\n");

  if (retval == 0 || retval == WRDE_NOSPACE)
    wordfree (&we);

  return bzzzt;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int tests [static]

Definition at line 215 of file wordexp-test.c.