Commit b4bde28d authored by Tomaz  Canabrava's avatar Tomaz Canabrava

Simpe C updates to ksysguardd code

Summary:
Use C 11, it has better defaults
Simplify if - chain
Reduce variable scope
Don't check before free

Reviewers: apol

Reviewed By: apol

Subscribers: apol, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D18576
parent 91b1f711
......@@ -45,7 +45,7 @@ endif()
PWUIDCache.c )
add_executable(ksysguardd ${ksysguardd_SRCS})
set_property(TARGET ksysguardd PROPERTY C_STANDARD 11)
target_link_libraries(ksysguardd libksysguardd)
if( ${CMAKE_SYSTEM_NAME} MATCHES "NetBSD" )
......
......@@ -51,12 +51,10 @@ void command_cleanup( void* v )
{
if ( v ) {
Command* c = v;
if ( c->command )
free ( c->command );
if ( c->type )
free ( c->type );
free ( v );
free ( c->command );
free ( c->type );
}
free ( v );
}
/*
......@@ -152,8 +150,7 @@ void removeCommand( const char* command )
if ( cmd->command && strcmp( cmd->command, command ) == 0 ) {
remove_ctnr( CommandList );
free( cmd->command );
if ( cmd->type )
free( cmd->type );
free( cmd->type );
free( cmd );
}
}
......
......@@ -51,13 +51,11 @@ static int uidCmp( void* p1, void* p2 )
void PWUIDCache_cleanup( void* c )
{
if ( c ) {
if ( ((CachedPWUID*)c)->uName )
free ( ((CachedPWUID*)c)->uName );
free ( c );
}
if ( c )
free ( ((CachedPWUID*)c)->uName );
free ( c );
}
void initPWUIDCache()
{
UIDCache = new_ctnr();
......@@ -100,10 +98,7 @@ const char* getCachedPWUID( uid_t uid )
entry->uid = uid;
pwent = getpwuid( uid );
if ( pwent )
entry->uName = strdup( pwent->pw_name );
else
entry->uName = strdup( "?" );
entry->uName = strdup( pwent ? pwent->pw_name : "?" );
push_ctnr( UIDCache, entry );
bsort_ctnr( UIDCache, uidCmp );
......
......@@ -38,11 +38,9 @@ void freeConfigFile( void );
void LogFileList_cleanup( void *ptr )
{
if ( ptr ) {
if ( ((ConfigLogFile*)ptr)->name )
free( ((ConfigLogFile*)ptr)->name );
free( ptr );
}
free( ptr );
}
void freeConfigFile( void )
......
......@@ -268,9 +268,7 @@ static int readCommand( int fd, char* cmdBuf, size_t len )
void resetClientList( void )
{
int i;
for ( i = 0; i < MAX_CLIENTS; i++ ) {
for (int i = 0; i < MAX_CLIENTS; i++ ) {
ClientList[ i ].socket = -1;
ClientList[ i ].out = 0;
}
......@@ -281,10 +279,9 @@ void resetClientList( void )
*/
int addClient( int client )
{
int i;
FILE* out;
for ( i = 0; i < MAX_CLIENTS; i++ ) {
for (int i = 0; i < MAX_CLIENTS; i++ ) {
if ( ClientList[ i ].socket == -1 ) {
ClientList[ i ].socket = client;
if ( ( out = fdopen( client, "w+" ) ) == NULL ) {
......@@ -310,9 +307,7 @@ int addClient( int client )
*/
int delClient( int client )
{
int i;
for ( i = 0; i < MAX_CLIENTS; i++ ) {
for (int i = 0; i < MAX_CLIENTS; i++ ) {
if ( ClientList[i].socket == client ) {
fclose( ClientList[ i ].out );
ClientList[ i ].out = 0;
......@@ -356,10 +351,7 @@ int createServerSocket()
memset( &s_in, 0, sizeof( struct sockaddr_in ) );
s_in.sin_family = AF_INET;
if ( BindToAllInterfaces )
s_in.sin_addr.s_addr = htonl( INADDR_ANY );
else
s_in.sin_addr.s_addr = htonl( INADDR_LOOPBACK );
s_in.sin_addr.s_addr = htonl( BindToAllInterfaces ? INADDR_ANY : INADDR_LOOPBACK );
s_in.sin_port = htons( SocketPort );
if ( bind( newSocket, (struct sockaddr*)&s_in, sizeof( s_in ) ) < 0 ) {
......@@ -417,8 +409,6 @@ static void handleSocketTraffic( int socketNo, const fd_set* fds )
char cmdBuf[ CMDBUFSIZE ];
if ( RunAsDaemon ) {
int i;
if ( FD_ISSET( socketNo, fds ) ) {
int clientsocket;
struct sockaddr addr;
......@@ -432,7 +422,7 @@ static void handleSocketTraffic( int socketNo, const fd_set* fds )
addClient( clientsocket );
}
for ( i = 0; i < MAX_CLIENTS; i++ ) {
for (int i = 0; i < MAX_CLIENTS; i++ ) {
if ( ClientList[ i ].socket != -1 ) {
CurrentSocket = ClientList[ i ].socket;
if ( FD_ISSET( ClientList[ i ].socket, fds ) ) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment