Index: code/sys/sys_unix.c =================================================================== --- code/sys/sys_unix.c (revision 2080) +++ code/sys/sys_unix.c (working copy) @@ -43,6 +43,8 @@ // Used to determine where to store user-specific files static char homePath[ MAX_OSPATH ] = { 0 }; +static qboolean Sys_PathHasCommand(const char *); + /* ================== Sys_DefaultHomePath @@ -438,8 +440,59 @@ return listCopy; } + /* ================== +Sys_PathHasCommand + +Try to find a command (name only) in a directory from the PATH +environment variable. +================== +*/ +static qboolean Sys_PathHasCommand( const char *command ) +{ + char try_path[ MAX_OSPATH ]; + size_t buf_sizeof; + char *buf; + const char *start; + const char *path; + char *p; + qboolean flag = qfalse; + + path = getenv( "PATH" ); + if (path == NULL) { + return qfalse; + } + + buf_sizeof = strlen(path) + 1; + + buf = Z_Malloc(buf_sizeof * sizeof(char)); + Com_sprintf(buf, buf_sizeof, "%s", path); + + for (start = buf; start != NULL && *start != '\0'; ) { + p = strchr(start, ':'); + if (p != NULL) { + *p = '\0'; + } + + Com_sprintf(try_path, sizeof(try_path), "%s/%s", start, command); + if (access(try_path, F_OK | X_OK) == 0) { + flag = qtrue; + break; + } + + if (p == NULL) { + break; + } + start = ++p; + } + + Z_Free(buf); + return flag; +} + +/* +================== Sys_FreeFileList ================== */ @@ -557,9 +610,14 @@ */ static int Sys_ZenityCommand( dialogType_t type, const char *message, const char *title ) { + const char *cmd_name = "zenity"; const char *options = ""; char command[ 1024 ]; + if ( ! Sys_PathHasCommand(cmd_name)) { + return -1; + } + switch( type ) { default: @@ -570,8 +628,8 @@ case DT_OK_CANCEL: options = "--question --ok-label=\"OK\" --cancel-label=\"Cancel\""; break; } - Com_sprintf( command, sizeof( command ), "zenity %s --text=\"%s\" --title=\"%s\"", - options, message, title ); + Com_sprintf( command, sizeof( command ), "%s %s --text=\"%s\" --title=\"%s\"", + cmd_name, options, message, title ); return system( command ); } @@ -583,9 +641,14 @@ */ static int Sys_KdialogCommand( dialogType_t type, const char *message, const char *title ) { + const char *cmd_name = "kdialog"; const char *options = ""; char command[ 1024 ]; + if ( ! Sys_PathHasCommand(cmd_name)) { + return -1; + } + switch( type ) { default: @@ -596,8 +659,8 @@ case DT_OK_CANCEL: options = "--warningcontinuecancel"; break; } - Com_sprintf( command, sizeof( command ), "kdialog %s \"%s\" --title \"%s\"", - options, message, title ); + Com_sprintf( command, sizeof( command ), "%s %s \"%s\" --title \"%s\"", + cmd_name, options, message, title ); return system( command ); } @@ -609,9 +672,14 @@ */ static int Sys_XmessageCommand( dialogType_t type, const char *message, const char *title ) { + const char *cmd_name = "xmessage"; const char *options = ""; char command[ 1024 ]; + if ( ! Sys_PathHasCommand(cmd_name)) { + return -1; + } + switch( type ) { default: options = "-buttons OK"; break; @@ -619,8 +687,8 @@ case DT_OK_CANCEL: options = "-buttons OK:0,Cancel:1"; break; } - Com_sprintf( command, sizeof( command ), "xmessage -center %s \"%s\"", - options, message ); + Com_sprintf( command, sizeof( command ), "%s -center %s \"%s\"", + cmd_name, options, message ); return system( command ); }