xref: /haiku/src/kits/app/AppMisc.cpp (revision e79e4e7c9e432c90415f79809b7160e864f79001)
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 namespace BPrivate {
35 
36 // get_app_path
37 /*!	\brief Returns the path to the application's executable.
38 	\param buffer A pointer to a pre-allocated character array of at least
39 		   size B_PATH_NAME_LENGTH to be filled in by this function.
40 	\return
41 	- \c B_OK: Everything went fine.
42 	- \c B_BAD_VALUE: \c NULL \a buffer.
43 	- another error code
44 */
45 status_t
46 get_app_path(char *buffer)
47 {
48 	// The only way to get the path to the application's executable seems to
49 	// be to get an image_info of its image, which also contains a path.
50 	// Several images may belong to the team (libraries, add-ons), but only
51 	// the one in question should be typed B_APP_IMAGE.
52 	status_t error = (buffer ? B_OK : B_BAD_VALUE);
53 	image_info info;
54 	int32 cookie = 0;
55 	bool found = false;
56 	if (error == B_OK) {
57 		while (!found && get_next_image_info(0, &cookie, &info) == B_OK) {
58 			if (info.type == B_APP_IMAGE) {
59 				strncpy(buffer, info.name, B_PATH_NAME_LENGTH - 1);
60 				buffer[B_PATH_NAME_LENGTH - 1] = 0;
61 				found = true;
62 			}
63 		}
64 	}
65 	if (error == B_OK && !found)
66 		error = B_ENTRY_NOT_FOUND;
67 	return error;
68 }
69 
70 // get_app_ref
71 /*!	\brief Returns an entry_ref referring to the application's exectable.
72 	\param ref A pointer to a pre-allocated entry_ref to be initialized
73 		   to an entry_ref referring to the application's executable.
74 	\param traverse If \c true, the function traverses symbolic links.
75 	\return
76 	- \c B_OK: Everything went fine.
77 	- \c B_BAD_VALUE: \c NULL \a ref.
78 	- another error code
79 */
80 status_t
81 get_app_ref(entry_ref *ref, bool traverse)
82 {
83 	status_t error = (ref ? B_OK : B_BAD_VALUE);
84 	char appFilePath[B_PATH_NAME_LENGTH];
85 	if (error == B_OK)
86 		error = get_app_path(appFilePath);
87 	if (error == B_OK) {
88 		BEntry entry(appFilePath, traverse);
89 		error = entry.GetRef(ref);
90 	}
91 	return error;
92 }
93 
94 // current_team
95 /*!	\brief Returns the ID of the current team.
96 	\return The ID of the current team.
97 */
98 team_id
99 current_team()
100 {
101 	team_id team = -1;
102 	thread_info info;
103 	if (get_thread_info(find_thread(NULL), &info) == B_OK)
104 		team = info.team;
105 	return team;
106 }
107 
108 // main_thread_for
109 /*!	Returns the ID of the supplied team's main thread.
110 	\param team The team.
111 	\return
112 	- The thread ID of the supplied team's main thread
113 	- \c B_BAD_TEAM_ID: The supplied team ID does not identify a running team.
114 	- another error code
115 */
116 thread_id
117 main_thread_for(team_id team)
118 {
119 	// For I can't find any trace of how to explicitly get the main thread,
120 	// I assume the main thread is the one with the least thread ID.
121 	thread_id thread = B_BAD_TEAM_ID;
122 	int32 cookie = 0;
123 	thread_info info;
124 	while (get_next_thread_info(team, &cookie, &info) == B_OK) {
125 		if (thread < 0 || info.thread < thread)
126 			thread = info.thread;
127 	}
128 	return thread;
129 }
130 
131 } // namespace BPrivate
132 
133