• Main Page
  • Files
  • File List
  • File Members

/home/bifh6/cs2009q3-i386/work/applauncherd-0.30.5+rq730927+0m6/src/invoker/search.c

Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (directui@nokia.com)
00006 **
00007 ** This file is part of applauncherd
00008 **
00009 ** If you have questions regarding the use of this file, please contact
00010 ** Nokia at directui@nokia.com.
00011 **
00012 ** This library is free software; you can redistribute it and/or
00013 ** modify it under the terms of the GNU Lesser General Public
00014 ** License version 2.1 as published by the Free Software Foundation
00015 ** and appearing in the file LICENSE.LGPL included in the packaging
00016 ** of this file.
00017 **
00018 ****************************************************************************/
00019 
00020 #define _GNU_SOURCE
00021 
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <unistd.h>
00025 #include <string.h>
00026 
00027 #include "report.h"
00028 #include "search.h"
00029 
00030 static char* merge_paths(const char *base_path, const char *rel_path)
00031 {
00032     char *path;
00033 
00034     if (asprintf(&path, "%s%s%s", base_path,
00035                  (base_path[strlen(base_path) - 1] == '/' ? "" : "/"),
00036                  rel_path) < 0)
00037     {
00038         die(1, "allocating merge path buffer");
00039     }
00040     return path;
00041 }
00042 
00043 char* search_program(const char *progname)
00044 {
00045     char *launch = NULL;
00046     char *cwd;
00047 
00048     if (progname[0] == '/')
00049     {
00050         launch = strdup(progname);
00051         if (!launch)
00052         {
00053             die(1, "allocating program name buffer");
00054         }
00055     }
00056     else if (strchr(progname, '/') != NULL)
00057     {
00058         cwd = get_current_dir_name();
00059         launch = merge_paths(cwd, progname);
00060         free(cwd);
00061     }
00062     else
00063     {
00064         char *path = getenv("PATH");
00065         char *saveptr = NULL;
00066         char *token;
00067 
00068         if (path == NULL)
00069         {
00070             die(1, "could not get PATH environment variable");
00071         }
00072         path = strdup(path);
00073 
00074         for (token = strtok_r(path, ":", &saveptr); token != NULL; token = strtok_r(NULL, ":", &saveptr))
00075         {
00076             launch = merge_paths(token, progname);
00077 
00078             if (access(launch, X_OK) == 0)
00079                 break;
00080 
00081             free(launch);
00082             launch = NULL;
00083         }
00084 
00085         free(path);
00086 
00087         if (launch == NULL)
00088         {
00089             die(1, "could not locate program \"%s\" to launch \n", progname);
00090         }
00091 
00092         if (launch[0] != '/')
00093         {
00094             char *relative = launch;
00095 
00096             cwd = get_current_dir_name();
00097             launch = merge_paths(cwd, relative);
00098 
00099             free(cwd);
00100             free(relative);
00101         }
00102     }
00103     return launch;
00104 }
00105 

Generated on Mon Jul 4 2011 14:23:01 for applauncherd by  doxygen 1.7.1