Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
htop
Commits
1de8762e
Commit
1de8762e
authored
Apr 22, 2014
by
Hisham Muhammad
Browse files
Fix display of open files.
Cleanup the code to prevent this bug from happening again.
parent
d28b2811
Changes
1
Hide whitespace changes
Inline
Side-by-side
OpenFilesScreen.c
View file @
1de8762e
...
...
@@ -28,14 +28,18 @@ in the source distribution for its full text.
#include "Panel.h"
#include "FunctionBar.h"
typedef struct OpenFiles_Data_ {
char* data[256];
} OpenFiles_Data;
typedef struct OpenFiles_ProcessData_ {
struct
OpenFiles_
FileData_* files
;
OpenFiles_
Data data
;
int error;
char* data[256]
;
struct OpenFiles_FileData_* files
;
} OpenFiles_ProcessData;
typedef struct OpenFiles_FileData_ {
char* data[256]
;
OpenFiles_Data data
;
struct OpenFiles_FileData_* next;
} OpenFiles_FileData;
...
...
@@ -85,17 +89,15 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
FILE
*
fd
=
popen
(
command
,
"r"
);
OpenFiles_ProcessData
*
pdata
=
calloc
(
1
,
sizeof
(
OpenFiles_ProcessData
));
OpenFiles_FileData
*
fdata
=
NULL
;
OpenFiles_ProcessData
*
item
=
pdata
;
bool
anyRead
=
false
;
OpenFiles_Data
*
item
=
&
(
pdata
->
data
);
if
(
!
fd
)
{
pdata
->
error
=
127
;
return
pdata
;
}
while
(
!
feof
(
fd
))
{
int
cmd
=
fgetc
(
fd
);
if
(
cmd
==
EOF
&&
!
anyRead
)
if
(
cmd
==
EOF
)
break
;
anyRead
=
true
;
char
*
entry
=
malloc
(
1024
);
if
(
!
fgets
(
entry
,
1024
,
fd
))
{
free
(
entry
);
...
...
@@ -104,14 +106,14 @@ static OpenFiles_ProcessData* OpenFilesScreen_getProcessData(pid_t pid) {
char
*
newline
=
strrchr
(
entry
,
'\n'
);
*
newline
=
'\0'
;
if
(
cmd
==
'f'
)
{
OpenFiles_FileData
*
nextFile
=
calloc
(
1
,
sizeof
(
OpenFiles_
Process
Data
));
OpenFiles_FileData
*
nextFile
=
calloc
(
1
,
sizeof
(
OpenFiles_
File
Data
));
if
(
fdata
==
NULL
)
{
pdata
->
files
=
nextFile
;
}
else
{
fdata
->
next
=
nextFile
;
}
fdata
=
nextFile
;
item
=
(
OpenFiles_ProcessData
*
)
f
data
;
item
=
&
(
fdata
->
data
)
;
}
item
->
data
[
cmd
]
=
entry
;
}
...
...
@@ -125,6 +127,12 @@ static inline void addLine(const char* line, Vector* lines, Panel* panel, const
Panel_add
(
panel
,
(
Object
*
)
Vector_get
(
lines
,
Vector_size
(
lines
)
-
1
));
}
static
inline
void
OpenFiles_Data_clear
(
OpenFiles_Data
*
data
)
{
for
(
int
i
=
0
;
i
<
255
;
i
++
)
if
(
data
->
data
[
i
])
free
(
data
->
data
[
i
]);
}
static
void
OpenFilesScreen_scan
(
OpenFilesScreen
*
this
,
Vector
*
lines
,
IncSet
*
inc
)
{
Panel
*
panel
=
this
->
display
;
int
idx
=
Panel_getSelectedIndex
(
panel
);
...
...
@@ -138,24 +146,21 @@ static void OpenFilesScreen_scan(OpenFilesScreen* this, Vector* lines, IncSet* i
OpenFiles_FileData
*
fdata
=
pdata
->
files
;
while
(
fdata
)
{
char
entry
[
1024
];
char
**
data
=
fdata
->
data
.
data
;
sprintf
(
entry
,
"%5s %4s %10s %10s %10s %s"
,
fdata
->
data
[
'f'
]
?
fdata
->
data
[
'f'
]
:
""
,
fdata
->
data
[
't'
]
?
fdata
->
data
[
't'
]
:
""
,
fdata
->
data
[
'D'
]
?
fdata
->
data
[
'D'
]
:
""
,
fdata
->
data
[
's'
]
?
fdata
->
data
[
's'
]
:
""
,
fdata
->
data
[
'i'
]
?
fdata
->
data
[
'i'
]
:
""
,
fdata
->
data
[
'n'
]
?
fdata
->
data
[
'n'
]
:
""
);
data
[
'f'
]
?
data
[
'f'
]
:
""
,
data
[
't'
]
?
data
[
't'
]
:
""
,
data
[
'D'
]
?
data
[
'D'
]
:
""
,
data
[
's'
]
?
data
[
's'
]
:
""
,
data
[
'i'
]
?
data
[
'i'
]
:
""
,
data
[
'n'
]
?
data
[
'n'
]
:
""
);
addLine
(
entry
,
lines
,
panel
,
IncSet_filter
(
inc
));
for
(
int
i
=
0
;
i
<
255
;
i
++
)
if
(
fdata
->
data
[
i
])
free
(
fdata
->
data
[
i
]);
OpenFiles_Data_clear
(
&
fdata
->
data
);
OpenFiles_FileData
*
old
=
fdata
;
fdata
=
fdata
->
next
;
free
(
old
);
}
for
(
int
i
=
0
;
i
<
255
;
i
++
)
if
(
pdata
->
data
[
i
])
free
(
pdata
->
data
[
i
]);
OpenFiles_Data_clear
(
&
pdata
->
data
);
}
free
(
pdata
);
Vector_insertionSort
(
lines
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment