@@ -284,7 +284,7 @@ char** save_ps_args(int argc, char** argv)
284
284
* and the init function was called.
285
285
* Otherwise returns NOT_AVAILABLE or NOT_INITIALIZED
286
286
*/
287
- int is_ps_title_available (void )
287
+ ps_title_status is_ps_title_available (void )
288
288
{
289
289
#ifdef PS_USE_NONE
290
290
return PS_TITLE_NOT_AVAILABLE ; /* disabled functionality */
@@ -304,7 +304,7 @@ int is_ps_title_available(void)
304
304
/*
305
305
* Convert error codes into error strings
306
306
*/
307
- const char * ps_title_errno (int rc )
307
+ const char * ps_title_errno (ps_title_status rc )
308
308
{
309
309
switch (rc )
310
310
{
@@ -320,11 +320,16 @@ const char* ps_title_errno(int rc)
320
320
case PS_TITLE_BUFFER_NOT_AVAILABLE :
321
321
return "Buffer not contiguous" ;
322
322
323
- #ifdef PS_USE_WIN32
323
+ case PS_TITLE_TOO_LONG :
324
+ // TODO Indicate max length?
325
+ return "Too long" ;
326
+
324
327
case PS_TITLE_WINDOWS_ERROR :
328
+ #ifdef PS_USE_WIN32
325
329
snprintf (windows_error_details , sizeof (windows_error_details ), "Windows error code: %lu" , GetLastError ());
326
330
return windows_error_details ;
327
331
#endif
332
+ return "Windows error" ;
328
333
}
329
334
330
335
return "Unknown error code" ;
@@ -337,14 +342,19 @@ const char* ps_title_errno(int rc)
337
342
* save_ps_args() was not called.
338
343
* Else returns 0 on success.
339
344
*/
340
- int set_ps_title (const char * title )
345
+ ps_title_status set_ps_title (const char * title , size_t title_len )
341
346
{
342
- int rc = is_ps_title_available ();
347
+ if (title_len >= ps_buffer_size ) {
348
+ return PS_TITLE_TOO_LONG ;
349
+ }
350
+
351
+ ps_title_status rc = is_ps_title_available ();
343
352
if (rc != PS_TITLE_SUCCESS )
344
353
return rc ;
345
354
346
- size_t title_len = strlcpy (ps_buffer , title , ps_buffer_size );
347
- ps_buffer_cur_len = (title_len >= ps_buffer_size ) ? ps_buffer_size - 1 : title_len ;
355
+ /* Include final null byte */
356
+ memcpy (ps_buffer , title , title_len + 1 );
357
+ ps_buffer_cur_len = title_len ;
348
358
349
359
#ifdef PS_USE_SETPROCTITLE
350
360
setproctitle ("%s" , ps_buffer );
@@ -394,9 +404,9 @@ int set_ps_title(const char* title)
394
404
* length into *displen.
395
405
* The return code indicates the error.
396
406
*/
397
- int get_ps_title (size_t * displen , const char * * string )
407
+ ps_title_status get_ps_title (size_t * displen , const char * * string )
398
408
{
399
- int rc = is_ps_title_available ();
409
+ ps_title_status rc = is_ps_title_available ();
400
410
if (rc != PS_TITLE_SUCCESS )
401
411
return rc ;
402
412
0 commit comments