Back to index

tetex-bin  3.0
Functions | Variables
tilde.h File Reference
#include "info.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

char * tilde_expand (char *string)
char * tilde_expand_word (char *filename)

Variables

CFunctiontilde_expansion_failure_hook
char ** tilde_additional_prefixes
char ** tilde_additional_suffixes

Function Documentation

char* tilde_expand ( char *  string)

Definition at line 117 of file tilde.c.

{
  char *result;
  int result_size, result_index;

  result_size = result_index = 0;
  result = (char *)NULL;

  /* Scan through STRING expanding tildes as we come to them. */
  while (1)
    {
      register int start, end;
      char *tilde_word, *expansion;
      int len;

      /* Make START point to the tilde which starts the expansion. */
      start = tilde_find_prefix (string, &len);

      /* Copy the skipped text into the result. */
      if ((result_index + start + 1) > result_size)
        result = (char *)xrealloc (result, 1 + (result_size += (start + 20)));

      strncpy (result + result_index, string, start);
      result_index += start;

      /* Advance STRING to the starting tilde. */
      string += start;

      /* Make END be the index of one after the last character of the
         username. */
      end = tilde_find_suffix (string);

      /* If both START and END are zero, we are all done. */
      if (!start && !end)
        break;

      /* Expand the entire tilde word, and copy it into RESULT. */
      tilde_word = (char *)xmalloc (1 + end);
      strncpy (tilde_word, string, end);
      tilde_word[end] = '\0';
      string += end;

      expansion = tilde_expand_word (tilde_word);
      free (tilde_word);

      len = strlen (expansion);
      if ((result_index + len + 1) > result_size)
        result = (char *)xrealloc (result, 1 + (result_size += (len + 20)));

      strcpy (result + result_index, expansion);
      result_index += len;
      free (expansion);
    }

  result[result_index] = '\0';

  return (result);
}

Here is the call graph for this function:

char* tilde_expand_word ( char *  filename)

Definition at line 179 of file tilde.c.

{
  char *dirname = filename ? xstrdup (filename) : NULL;

  if (dirname && *dirname == '~')
    {
      char *temp_name;
      if (!dirname[1] || IS_SLASH (dirname[1]))
        {
          /* Prepend $HOME to the rest of the string. */
          char *temp_home = getenv ("HOME");

          /* If there is no HOME variable, look up the directory in
             the password database. */
          if (!temp_home)
            {
              struct passwd *entry;

              entry = (struct passwd *) getpwuid (getuid ());
              if (entry)
                temp_home = entry->pw_dir;
            }

          temp_name = xmalloc (1 + strlen (&dirname[1])
                               + (temp_home ? strlen (temp_home) : 0));
          if (temp_home)
            strcpy (temp_name, temp_home);
          else
            temp_name[0] = 0;
          strcat (temp_name, &dirname[1]);
          free (dirname);
          dirname = xstrdup (temp_name);
          free (temp_name);
        }
      else
        {
          struct passwd *user_entry;
          char *username = xmalloc (257);
          int i, c;

          for (i = 1; (c = dirname[i]); i++)
            {
              if (IS_SLASH (c))
                break;
              else
                username[i - 1] = c;
            }
          username[i - 1] = 0;

          if (!(user_entry = (struct passwd *) getpwnam (username)))
            {
              /* If the calling program has a special syntax for
                 expanding tildes, and we couldn't find a standard
                 expansion, then let them try. */
              if (tilde_expansion_failure_hook)
                {
                  char *expansion = (*tilde_expansion_failure_hook) (username);

                  if (expansion)
                    {
                      temp_name = xmalloc (1 + strlen (expansion)
                                           + strlen (&dirname[i])); 
                      strcpy (temp_name, expansion);
                      strcat (temp_name, &dirname[i]);
                      free (expansion);
                      goto return_name;
                    }
                }
              /* We shouldn't report errors. */
            }
          else
            {
              temp_name = xmalloc (1 + strlen (user_entry->pw_dir)
                                   + strlen (&dirname[i])); 
              strcpy (temp_name, user_entry->pw_dir);
              strcat (temp_name, &dirname[i]);

            return_name:
              free (dirname);
              dirname = xstrdup (temp_name);
              free (temp_name);
            }

          endpwent ();
          free (username);
        }
    }
  return dirname;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 52 of file tilde.c.

Definition at line 57 of file tilde.c.

Definition at line 47 of file tilde.c.