plover/util.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Jan 30 13:35:28 2012 +0000 (2012-01-30)
changeset 18 cb43820f94ce
parent 3 868db5c1f2d7
child 24 2b9f54d14cc2
permissions -rw-r--r--
Release 0.4.1
     1 /*
     2  * Copyright (C) 2009, 2011  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     4  * This program is free software; you can redistribute it and/or modify
     5  * it under the terms of the GNU General Public License as published by
     6  * the Free Software Foundation; either version 2 of the License, or
     7  * (at your option) any later version.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License along
    15  * with this program; if not, write to the Free Software Foundation, Inc.,
    16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    17  */
    18 
    19 #include <stdlib.h>
    20 #include <stdarg.h>
    21 #include <string.h>
    22 #include <limits.h>
    23 #ifdef WIN32
    24 #include <windows.h>
    25 #include <shlobj.h>
    26 #endif
    27 #include "config.h"
    28 #include "plover.h"
    29 
    30 char *plover_strconcat(const char *string,...)
    31 {
    32     va_list ap,aq;
    33     size_t n;
    34     char *result=NULL,*t;
    35     const char *s;
    36     if (string)
    37     {
    38 	va_start(ap,string);
    39 	va_copy(aq,ap);
    40 	n=strlen(string);
    41 	while((s=va_arg(aq,const char *)))
    42 	    n+=strlen(s);
    43 	va_end(aq);
    44 	result=malloc(n+1);
    45 	if (result)
    46 	{
    47 	    strcpy(result,string);
    48 	    t=result+strlen(result);
    49 	    while((s=va_arg(ap,const char *)))
    50 	    {
    51 		strcpy(t,s);
    52 		t+=strlen(t);
    53 	    }
    54 	}
    55 	va_end(ap);
    56     }
    57     return result;
    58 }
    59 
    60 char *plover_default_prefix_for_vendor(const char *vendor)
    61 {
    62 #ifdef WIN32
    63     /*
    64      * We want to sidestep any redirecting that MS-Windows may
    65      * introduce since this will be based on the architecture
    66      * of the installer whereas the architecture that actually
    67      * matters is of the packages (checked elsewhere).
    68      */
    69     BOOL is_wow64=FALSE;
    70     typedef BOOL (WINAPI *is_wow64_process_t)(HANDLE,PBOOL);
    71     is_wow64_process_t is_wow64_process;
    72     char *program_files=NULL;
    73     char buf[PATH_MAX];
    74     is_wow64_process=(is_wow64_process_t)
    75       GetProcAddress(GetModuleHandleA("kernel32"),"IsWow64Process");
    76     if (is_wow64_process)
    77 	is_wow64_process(GetCurrentProcess(),&is_wow64);
    78     if (is_wow64)
    79 	program_files=getenv("ProgramW6432");
    80     if (!program_files)
    81     {
    82 	SHGetFolderPath(NULL,CSIDL_PROGRAM_FILES|CSIDL_FLAG_DONT_VERIFY,NULL,0,
    83 	  buf);
    84 	program_files=buf;
    85     }
    86     return plover_strconcat(program_files,"\\",vendor?vendor:"Plover",NULL);
    87 #else
    88     return NULL;
    89 #endif
    90 }
    91 
    92 /*
    93  * Get the directory containing the program executable.
    94  */
    95 
    96 char *plover_get_program_directory(const char *argv0)
    97 {
    98     char *s;
    99 #ifdef WIN32
   100     char path[PATH_MAX],*t;
   101     GetModuleFileName(NULL,path,sizeof(path));
   102     s=strrchr(path,'/');
   103     if (s)
   104     {
   105 	t=strrchr(s,'\\');
   106 	if (t)
   107 	    s=t;
   108     }
   109     else
   110 	s=strrchr(path,'\\');
   111     if (s)
   112 	*s='\0';
   113     return strdup(path);
   114 #else
   115     s=argv0?strrchr(argv0,'/'):NULL;
   116     if (s)
   117 	return strndup(argv0,s-argv0);
   118     else
   119 	return strdup(".");
   120 #endif
   121 }