Re: Utility functions from gnome-libs



On Sat, Jul 15, 2000 at 08:41:47AM +0200, Tim Janik wrote:
> On Tue, 4 Jul 2000, John Kodis wrote:
> > On Tue, Jul 04, 2000 at 06:52:28AM +0200, Tim Janik wrote:
> > > On 3 Jul 2000, Owen Taylor wrote:
> > >
> > > > /* locate a program in $PATH, or return NULL if not found */
> > > > char *gnome_is_program_in_path (const gchar *program);
> > > > This would be renamed to g_is_program_in_path()
> > > 
> > > rather i'd call it g_find_program_in_path() since it
> > > 1) doesn't return a gboolean but a string
> > > 2) it can take quite some time to do all the path searching
> > 
> > It would also be handy to have a more general version -- something like
> >   char *g_find_file_in_path(const char *filename, const char *path);
> > for finding configuration files and searching MANPATH, MAILPATH, and
> > the like.
> 
> if you supply it, i'm all for it!

Here's what I had in mind.  Since the header file, implementation
file, and a little test program are only 111 lines in total, I've
attached all three files.

Also, on reflection, it might be best to drop the "_find" from both
names in the interest of brevity.  Thus,

    g_find_file_in_path()     becomes  g_file_in_path(), and
    g_find_program_in_path()  becomes  g_program_in_path().

I've Cc'd Owen Taylor, but I don't know what further steps I should
take to petition for getting this included in glib.  Any advice along
those lines, or criticism of the code would be appreciated.

-- John Kodis.
#include <glib.h>
#include <unistd.h>

gchar * g_find_file_in_path    (const gchar *filename, 
                                const gchar *path,
                                gint flags); /* X_OK &c from unistd.h */

gchar * g_find_program_in_path (const gchar *program);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "file-in-path.h"

gchar *
g_find_file_in_path (const gchar *filename, const gchar *path, gint flags)
{
  gchar *file_path, *path_head, *path_entry, *separator;

  if (filename == NULL || *filename == '\0' || path == NULL)
    return NULL;

  path_head = path_entry = g_strdup (path);
  file_path = g_malloc (strlen (filename) + strlen (path) + 3);

  while (path_entry != NULL)
    {
      separator = strchr (path_entry, G_SEARCHPATH_SEPARATOR);
      if (separator != NULL)
	*separator = '\0';

      if (*path_entry == '\0')
	sprintf (file_path, "%s", filename);
      else
	sprintf (file_path, "%s%s%s", path_entry, G_DIR_SEPARATOR_S, filename);

      if (access (file_path, flags) == 0)
	{
	  g_free (path_head);
	  return file_path;
	}

      path_entry = separator ? separator + 1 : NULL;
    }

  g_free (file_path);
  g_free (path_head);
  return NULL;
}

gchar *
g_find_program_in_path (const gchar *program)
{
  if (program == NULL)
    return NULL;

  if (strchr (program, G_DIR_SEPARATOR))
    return g_strdup (program);

  return g_find_file_in_path (program, getenv ("PATH"), X_OK);
}

/*
 * fip.c -- a test program for the g_file_in_path() function.
 *
 * compile using something along the lines of:
 * gcc -o fip fip.c file-in-path.c -g -O -Wall $(glib-config --cflags --libs)
 */

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include "file-in-path.h"

int
main (int argc, char **argv)
{
  int opt, arg;
  int flags = 0;
  int verbose = 0;

  for (arg = 1; arg < argc && *argv[arg] == '-'; arg++)
    for (opt = 1; argv[arg][opt]; opt++)
      switch (tolower (argv[arg][opt]))
	{
	case 'f': flags |= F_OK; break;
	case 'x': flags |= X_OK; break;
	case 'w': flags |= W_OK; break;
	case 'r': flags |= R_OK; break;
	case 'v': verbose = 1; break;
	default: 
	  fprintf (stderr, "fip: invalid option: %c\n", argv[arg][opt]);
	  return EXIT_FAILURE;
	}

  if (arg >= argc)
    {
      fprintf (stderr, "fip: missing filename parameter\n");
      return EXIT_FAILURE;
    }

  if (verbose)
    printf ("fip: flags=%d; file=%s; path=%s\n", flags, argv[arg], argv[arg+1]);

  printf ("%s\n", g_find_file_in_path(argv[arg], argv[arg+1], flags));
  return EXIT_SUCCESS;
}




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]