Skip to content

Commit 1c7e237

Browse files
📝 Add docstrings to uptime-and-docs
Docstrings generation was requested by @supervoidcoder. * #4 (comment) The following files were modified: * `main.cpp`
1 parent 1497101 commit 1c7e237

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

main.cpp

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ This is kept as a bunch of strings to be easier to call than a dictionary, map,
5151
Less words to type ;)
5252
*/
5353
std::string forkAuthor = ""; // if this is a fork of this project, put your name here! Please be nice and leave my name too :)
54-
std::string version = "v0.1.0"; // Version of this Windows port
54+
std::string version = "v0.1.0"; /**
55+
* @brief Detects whether the current console supports ANSI/VT escape sequences.
56+
*
57+
* @return true if the standard output console has virtual terminal processing enabled, false otherwise.
58+
*/
5559

5660

5761
bool IsVirtualTerminalModeEnabled() {
@@ -68,7 +72,13 @@ bool IsVirtualTerminalModeEnabled() {
6872
// If we tried spitting out escape codes in a terminal that doesn't support it, it would look like unreadable garbage,
6973
// and that's probably not very pleasant to the user. This is very rare and I have yet to encounter a terminal that doesn't support it,
7074
// but I'm sure there's someone out there using some ANCIENT old version of Windows that doesn't support it, and we want to support this for all versions.
71-
// Who knows, I might even test this on windows XP hahahahahaha...
75+
/**
76+
* @brief Enables the SeDebugPrivilege (debug privilege) on the current process token.
77+
*
78+
* Tries to adjust the calling process token to include the SE_DEBUG_NAME privilege so the process can perform debug-level operations on other processes.
79+
*
80+
* @return true if the privilege was successfully enabled, false if any step (opening the token, looking up the privilege, or adjusting token privileges) failed.
81+
*/
7282

7383
bool EnableDebugPrivilege() {
7484
HANDLE hToken;
@@ -133,7 +143,16 @@ BOOL IsProcessElevated()
133143
}
134144
// The above function is taken from https://vimalshekar.github.io/codesamples/Checking-If-Admin , modified to use C++
135145
// style I/O instead of printf like the original code.
136-
// Thanks!
146+
/**
147+
* @brief Converts a UTF-16 wide string to a UTF-8 encoded std::string.
148+
*
149+
* Converts the given Windows-style wide string (UTF-16) into a UTF-8 encoded
150+
* std::string suitable for use with standard C++ and UTF-8 APIs. Returns an
151+
* empty string when the input is empty.
152+
*
153+
* @param wstr Wide string (UTF-16) to convert.
154+
* @return std::string UTF-8 encoded representation of `wstr`.
155+
*/
137156

138157
std::string WideToString(const std::wstring& wstr) {
139158
if (wstr.empty()) return "";
@@ -142,7 +161,15 @@ std::string WideToString(const std::wstring& wstr) {
142161
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
143162
return strTo;
144163
}
145-
// The above stupid function is to convert wide strings (used by Windows API) to normal strings (used by C++ standard library) because cout chokes on wide strings.
164+
/**
165+
* @brief Retrieve a process's creation timestamp as a raw Windows FILETIME value.
166+
*
167+
* Obtains the creation time for the process identified by `pid` and returns it as
168+
* a 64-bit FILETIME value (number of 100-nanosecond intervals since January 1, 1601 UTC).
169+
*
170+
* @param pid Process identifier (PID) of the target process.
171+
* @return ULONGLONG The process creation time as a FILETIME quad-part value, or `0` if the time cannot be obtained (e.g., process cannot be opened or the API call fails).
172+
*/
146173

147174

148175
ULONGLONG GetProcessCreationTime(DWORD pid) {
@@ -166,7 +193,17 @@ ULONGLONG GetProcessCreationTime(DWORD pid) {
166193
// it actually returns a raw FILETIME, meaning it is an unsigned 64-bit integer that
167194
// shows the amount of 100-nanosecond intervals since January 1, 1601 (UTC), which is just straight up not readable, and
168195
// in my opinion INSANELY arbritrary, but I guess Microsoft is Microsoft...
169-
// In short, we need to do some more math on this.
196+
/**
197+
* @brief Produces a human-readable creation time for the given process.
198+
*
199+
* Queries the process creation time and formats it as a relative age (e.g., "2 days ago")
200+
* followed by a parenthesized local timestamp (e.g., "Mon 2025-02-02 11:42:10 +05:30").
201+
*
202+
* @param pid Process identifier to query.
203+
* @return std::string Human-readable creation time in the form
204+
* "X seconds/minutes/hours/days ago (Day YYYY-MM-DD HH:MM:SS ±ZZZZ)". Returns `"N/A"` if
205+
* the creation time cannot be obtained.
206+
*/
170207

171208
std::string GetReadableFileTime(DWORD pid) {
172209
ULONGLONG creationTime = GetProcessCreationTime(pid);
@@ -197,6 +234,17 @@ std::string GetReadableFileTime(DWORD pid) {
197234
}
198235

199236

237+
/**
238+
* @brief Print the ancestry chain for a process to standard output.
239+
*
240+
* Prints each ancestor as "ExecutableName (PID <pid>)" with two-space indentation per
241+
* generation. Recursively walks parent processes and increases indentation for each level.
242+
* If the parent process cannot be verified (not found, has creation time 0, or its creation
243+
* time is newer than the child), a "[Parent Process Exited]" placeholder is printed instead.
244+
*
245+
* @param pid Process identifier whose ancestry will be printed. pid values 0 and 4 are ignored.
246+
* @param depth Indentation depth (number of two-space indents) used for the current line.
247+
*/
200248
void PrintAncestry(DWORD pid, int depth = 0) {
201249

202250
/*
@@ -252,6 +300,16 @@ systemd (pid 1)
252300

253301

254302

303+
/**
304+
* @brief Inspect a process and print basic information to the console.
305+
*
306+
* Prints the target process's executable path (if queryable), its ancestry chain,
307+
* and a human-readable process start time. Errors encountered while opening the
308+
* process or querying its image are written to stderr; output uses ANSI color
309+
* sequences when virtual terminal mode is available.
310+
*
311+
* @param pid The process identifier (PID) of the target process to inspect.
312+
*/
255313
void PIDinspect(DWORD pid) { // ooh guys look i'm in the void
256314
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
257315
if (!hProcess) {
@@ -475,4 +533,4 @@ int main(int argc, char* argv[]) {
475533

476534
}
477535
// I know, I'm gonna go all out with the Ifs statements...
478-
// eh, I can optimize it later.
536+
// eh, I can optimize it later.

0 commit comments

Comments
 (0)