1 //------------------------------------------------------------------------------ 2 // Copyright (c) 2001-2002, OpenBeOS 3 // 4 // Permission is hereby granted, free of charge, to any person obtaining a 5 // copy of this software and associated documentation files (the "Software"), 6 // to deal in the Software without restriction, including without limitation 7 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 // and/or sell copies of the Software, and to permit persons to whom the 9 // Software is furnished to do so, subject to the following conditions: 10 // 11 // The above copyright notice and this permission notice shall be included in 12 // all copies or substantial portions of the Software. 13 // 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 // DEALINGS IN THE SOFTWARE. 21 // 22 // File Name: AppMisc.cpp 23 // Author: Ingo Weinhold (bonefish@users.sf.net) 24 // Description: Miscellaneous private functionality. 25 //------------------------------------------------------------------------------ 26 27 #include <string.h> 28 29 #include <AppMisc.h> 30 #include <Entry.h> 31 #include <image.h> 32 #include <OS.h> 33 34 // get_app_path 35 /*! \brief Returns the path to the application's executable. 36 \param buffer A pointer to a pre-allocated character array of at least 37 size B_PATH_NAME_LENGTH + 1 to be filled in by this function. 38 \return 39 - \c B_OK: Everything went fine. 40 - \c B_BAD_VALUE: \c NULL \a buffer. 41 - another error code 42 */ 43 status_t 44 get_app_path(char *buffer) 45 { 46 status_t error = (buffer ? B_OK : B_BAD_VALUE); 47 image_info info; 48 int32 cookie = 0; 49 bool found = false; 50 if (error == B_OK) { 51 while (!found && get_next_image_info(0, &cookie, &info) == B_OK) { 52 if (info.type == B_APP_IMAGE) { 53 strncpy(buffer, info.name, B_PATH_NAME_LENGTH); 54 buffer[B_PATH_NAME_LENGTH] = 0; 55 found = true; 56 } 57 } 58 } 59 if (error == B_OK && !found) 60 error = B_ENTRY_NOT_FOUND; 61 return error; 62 } 63 64 // get_app_ref 65 /*! \brief Returns an entry_ref referring to the application's exectable. 66 \param ref A pointer to a pre-allocated entry_ref to be initialized 67 to an entry_ref referring to the application's executable. 68 \param traverse If \c true, the function traverses symbolic links. 69 \return 70 - \c B_OK: Everything went fine. 71 - \c B_BAD_VALUE: \c NULL \a ref. 72 - another error code 73 */ 74 status_t 75 get_app_ref(entry_ref *ref, bool traverse) 76 { 77 status_t error = (ref ? B_OK : B_BAD_VALUE); 78 char appFilePath[B_PATH_NAME_LENGTH + 1]; 79 if (error == B_OK) 80 error = get_app_path(appFilePath); 81 if (error == B_OK) { 82 BEntry entry(appFilePath, traverse); 83 error = entry.GetRef(ref); 84 } 85 return error; 86 } 87 88 // main_thread_for 89 /*! Returns the ID of the supplied team's main thread. 90 \param team The team. 91 \return 92 - The thread ID of the supplied team's main thread 93 - \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team. 94 - another error code 95 */ 96 thread_id 97 main_thread_for(team_id team) 98 { 99 // For I can't find any trace of how to explicitly get the main thread, 100 // I assume the main thread is the one with the least thread ID. 101 thread_id thread = -1; 102 int32 cookie = 0; 103 thread_info info; 104 while (get_next_thread_info(team, &cookie, &info) == B_OK) { 105 if (thread < 0 || info.thread < thread) 106 thread = info.thread; 107 } 108 return thread; 109 } 110 111