Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**************************************************************************
  2.  *
  3.  * Copyright 2013 VMware, Inc.
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the
  8.  * "Software"), to deal in the Software without restriction, including
  9.  * without limitation the rights to use, copy, modify, merge, publish,
  10.  * distribute, sub license, and/or sell copies of the Software, and to
  11.  * permit persons to whom the Software is furnished to do so, subject to
  12.  * the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice (including the
  15.  * next paragraph) shall be included in all copies or substantial portions
  16.  * of the Software.
  17.  *
  18.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19.  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  21.  * IN NO EVENT SHALL THE AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
  22.  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  23.  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24.  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  *
  26.  **************************************************************************/
  27.  
  28.  
  29. #include "pipe/p_config.h"
  30. #include "os/os_process.h"
  31. #include "util/u_memory.h"
  32.  
  33. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  34. #  include <windows.h>
  35. #elif defined(__GLIBC__)
  36. #  include <errno.h>
  37. #elif defined(PIPE_OS_BSD) || defined(PIPE_OS_APPLE)
  38. #  include <stdlib.h>
  39. #else
  40. #warning unexpected platform in os_process.c
  41. #endif
  42.  
  43.  
  44. /**
  45.  * Return the name of the current process.
  46.  * \param procname  returns the process name
  47.  * \param size  size of the procname buffer
  48.  * \return  TRUE or FALSE for success, failure
  49.  */
  50. boolean
  51. os_get_process_name(char *procname, size_t size)
  52. {
  53.    const char *name;
  54. #if defined(PIPE_SUBSYSTEM_WINDOWS_USER)
  55.    char szProcessPath[MAX_PATH];
  56.    char *lpProcessName;
  57.    char *lpProcessExt;
  58.  
  59.    GetModuleFileNameA(NULL, szProcessPath, Elements(szProcessPath));
  60.  
  61.    lpProcessName = strrchr(szProcessPath, '\\');
  62.    lpProcessName = lpProcessName ? lpProcessName + 1 : szProcessPath;
  63.  
  64.    lpProcessExt = strrchr(lpProcessName, '.');
  65.    if (lpProcessExt) {
  66.       *lpProcessExt = '\0';
  67.    }
  68.  
  69.    name = lpProcessName;
  70.  
  71. #elif defined(__GLIBC__)
  72.    name = program_invocation_short_name;
  73. #elif defined(PIPE_OS_BSD) || defined(PIPE_OS_APPLE)
  74.    /* *BSD and OS X */
  75.    name = getprogname();
  76. #else
  77. #warning unexpected platform in os_process.c
  78.    return FALSE;
  79. #endif
  80.  
  81.    assert(size > 0);
  82.    assert(procname);
  83.  
  84.    if (name && procname && size > 0) {
  85.       strncpy(procname, name, size);
  86.       procname[size - 1] = '\0';
  87.       return TRUE;
  88.    }
  89.    else {
  90.       return FALSE;
  91.    }
  92. }
  93.