Some remarks with respect to the get_base_name() function:
size_tvsint: You correctly start withsize_t arg_len = strlen(arg);but then pass
arg_lentoget_last_backslash_index()which takes anintargument. Depending on the strictness of your compiler this can cause a “Implicit conversion loses integer precision” warning. I suggest to usesize_tconsequently for string length.The cast in
char* carr = (char*)calloc(return_char_array_len, sizeof(char));is not needed and generally not recommended, see for example Do I cast the result of malloc? on Stack Overflow.
sizeof(char)is always equal to one, therefore it can be shortened tochar* carr = malloc(return_char_array_len);Here
carr[return_char_array_len - 1] = NULL;a pointer value is assigned to
char(which is an integral type). The right-hand side should be0or'\0'to avoid compiler warnings.The implementation can be shortened if you take advantage of standardMicrosoft C runtime library functions like
strrchr()strrchr()andstrdup()_strdup():static char* get_base_name(const char* const arg) { const char *backslash_char_ptr = strrchr(arg, '\\'); return strdup_strdup(backslash_char_ptr == NULL ? arg : backslash_char_ptr + 1); }I would perhaps call the function something like
copy_base_name()to make it more obvious that the caller is responsible for releasing the memory.As an alternative you can use existing functions from the Microsoft C runtime like
splitpath, or Shell Path Handling Functions likePathStripPath().
Some more remarks:
- No error message is printed if terminating a process failed.
- The process handle is closed only if terminating the process succeeded.
I would perhaps do it like this:
HANDLE hProcess = OpenProcess(...); if (hProcess == NULL) { // Print error message } else { if (TerminateProcess(hProcess, 0)) { totalProcessesTerminated++; printf("Terminated process ID %d\n", entry.th32ProcessID); } else { // Print error message } if (!CloseHandle(hProcess)) { // Print error message } } In all error situations it would be useful to print the actual reason for the failure (e.g. “access denied”). This can be achieved with GetLastError() and FormatMessage(), see for example How to get the error message from the error code returned by GetLastError()? on Stack Overflow.