| View previous topic :: View next topic |
| Author |
Message |
petit.padavoine
Joined: 07 Apr 2007 Posts: 4
|
Posted: Thu May 31, 2007 1:44 am Post subject: Listing a directory |
|
|
Hello.
I'm trying to print out a list of the contents of a directory.
The code compiles correctly but when I execute it on the PSP, it freezes and turns off automatically after a few seconds.
| Code: |
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <stdio.h>
PSP_MODULE_INFO("PSPiPod", 0, 1, 1);
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
int main()
{
pspDebugScreenInit();
SetupCallbacks();
SceUID game = sceIoDopen("ms0:/PSP/GAME");
struct SceIoDirent folder;
while(sceIoDread(game, &folder)){
printf(folder.d_name);
}
sceIoDclose(game);
sceKernelSleepThread();
return 0;
}
| [/code] _________________ petit.padavoine -- [Aspiring] PSP developer |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Thu May 31, 2007 1:57 am Post subject: |
|
|
| memset the folder struct to zero every time you do the sceiodread. someone should put this in the headers... |
|
| Back to top |
|
 |
petit.padavoine
Joined: 07 Apr 2007 Posts: 4
|
Posted: Thu May 31, 2007 2:50 am Post subject: |
|
|
| weltall wrote: | | memset the folder struct to zero every time you do the sceiodread. someone should put this in the headers... |
So
| Code: | while(sceIoDread(game, &folder)){
printf(folder.d_name);
memset(&folder, 0, sizeof(SceIoDirent));
}
| [/code] _________________ petit.padavoine -- [Aspiring] PSP developer |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Thu May 31, 2007 3:20 am Post subject: |
|
|
| yes exactly |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Thu May 31, 2007 4:56 am Post subject: |
|
|
No, you want to zero it before you call sceiodread, not after.
Just use opendir/readdir/closedir in newlib, we've already taken the trouble to work around issues like this. |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Thu May 31, 2007 5:51 am Post subject: |
|
|
| jimparis wrote: | No, you want to zero it before you call sceiodread, not after.
Just use opendir/readdir/closedir in newlib, we've already taken the trouble to work around issues like this. |
and it's what happens in this code
at the start folder should be empty (and the first time sceiodread is called it always work even without work arounds)
then it printfs, memset to 0 and then sceiodread is called again so the code is correct :P
while(sceIoDread(game, &folder)){
printf(folder.d_name);
memset(&folder, 0, sizeof(SceIoDirent));
} |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Thu May 31, 2007 6:10 am Post subject: |
|
|
| Quote: | | at the start folder should be empty | "folder" is allocated on the stack and so it will be filled with uninitialized data initially. If that works then you're just getting lucky. |
|
| Back to top |
|
 |
petit.padavoine
Joined: 07 Apr 2007 Posts: 4
|
Posted: Thu May 31, 2007 3:52 pm Post subject: |
|
|
| jimparis wrote: | | Quote: | | at the start folder should be empty | "folder" is allocated on the stack and so it will be filled with uninitialized data initially. If that works then you're just getting lucky. |
Thanks a lot for all your answers, guys.
So,
| Code: |
memset(&folder, 0, sizeof(SceIoDiren));
while(sceIoDread(game, &folder)){
printf(folder.d_name);
memset(&folder, 0, sizeof(SceIoDirent));
}
|
The compiler says
"main.c: In function 'main':
main.c:115: warning: implicit declaration of function 'memset'
main.c:115: warning: incompatible implicit declaration of built-in function 'memset'".
...And the code works ! Thanks a lot everybody :D . _________________ petit.padavoine -- [Aspiring] PSP developer |
|
| Back to top |
|
 |
Jim

Joined: 02 Jul 2005 Posts: 487 Location: Sydney
|
Posted: Thu May 31, 2007 3:53 pm Post subject: |
|
|
The warning means you forgot to include stdlib.h
Jim _________________ http://www.dbfinteractive.com |
|
| Back to top |
|
 |
Insert_witty_name
Joined: 10 May 2006 Posts: 376
|
Posted: Thu May 31, 2007 6:12 pm Post subject: |
|
|
| Jim wrote: | The warning means you forgot to include stdlib.h
Jim |
That would be string.h ;) |
|
| Back to top |
|
 |
Cy-4AH
Joined: 31 Jan 2007 Posts: 44 Location: Belarus
|
Posted: Fri Jun 01, 2007 12:46 am Post subject: |
|
|
Don't you mind if I post my variant?
I was sure that weltall's words "memset the folder struct to zero every time you do the sceiodread. someone should put this in the headers..." was enough, but you need some code, so, there is mine:
| Code: | while (true)
{
memset(&folder, 0, sizeof(SceIoDirent));
if (sceIoDread(game, &folder) <= 0) break;
pspDebugScreenPrintf("%s\n", folder.d_name);
}
|
|
|
| Back to top |
|
 |
Mihawk
Joined: 03 Apr 2007 Posts: 29
|
Posted: Fri Jun 01, 2007 4:14 am Post subject: |
|
|
IIRC you don't need the memset before every sceIoDread call just before you call it the first time (thats what I saw in samples, and what worked for me).
So it would be a waste of cycles if you do it inside the while loop,
once before the loop is enough. |
|
| Back to top |
|
 |
weltall
Joined: 20 Feb 2004 Posts: 310
|
Posted: Fri Jun 01, 2007 3:35 pm Post subject: |
|
|
| jimparis wrote: | | Quote: | | at the start folder should be empty | "folder" is allocated on the stack and so it will be filled with uninitialized data initially. If that works then you're just getting lucky. |
well maybe it's so. thinking about it you are right it could be unsafe. |
|
| Back to top |
|
 |
|