Listing a directory

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
petit.padavoine
Posts: 4
Joined: Sat Apr 07, 2007 2:12 am

Listing a directory

Post by petit.padavoine »

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: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <stdio.h>

PSP_MODULE_INFO&#40;"PSPiPod", 0, 1, 1&#41;;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41; &#123;
          sceKernelExitGame&#40;&#41;;
          return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41; &#123;
          int cbid;

          cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
          sceKernelRegisterExitCallback&#40;cbid&#41;;

          sceKernelSleepThreadCB&#40;&#41;;

          return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41; &#123;
          int thid = 0;

          thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
          if&#40;thid >= 0&#41; &#123;
                    sceKernelStartThread&#40;thid, 0, 0&#41;;
          &#125;

          return thid;
&#125;

int main&#40;&#41;
&#123;
pspDebugScreenInit&#40;&#41;;
SetupCallbacks&#40;&#41;;

SceUID game = sceIoDopen&#40;"ms0&#58;/PSP/GAME"&#41;;
struct SceIoDirent folder;

while&#40;sceIoDread&#40;game, &folder&#41;&#41;&#123;
printf&#40;folder.d_name&#41;;
&#125;

sceIoDclose&#40;game&#41;;
sceKernelSleepThread&#40;&#41;;

return 0;
&#125;
[/code]
petit.padavoine -- [Aspiring] PSP developer
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

memset the folder struct to zero every time you do the sceiodread. someone should put this in the headers...
petit.padavoine
Posts: 4
Joined: Sat Apr 07, 2007 2:12 am

Post by petit.padavoine »

weltall wrote:memset the folder struct to zero every time you do the sceiodread. someone should put this in the headers...
So

Code: Select all

while&#40;sceIoDread&#40;game, &folder&#41;&#41;&#123; 
printf&#40;folder.d_name&#41;; 
memset&#40;&folder, 0, sizeof&#40;SceIoDirent&#41;&#41;;
&#125;
[/code]
petit.padavoine -- [Aspiring] PSP developer
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

yes exactly
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

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.
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

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));
}
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

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.
petit.padavoine
Posts: 4
Joined: Sat Apr 07, 2007 2:12 am

Post by petit.padavoine »

jimparis wrote:
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: Select all

memset&#40;&folder, 0, sizeof&#40;SceIoDiren&#41;&#41;;
while&#40;sceIoDread&#40;game, &folder&#41;&#41;&#123; 
printf&#40;folder.d_name&#41;; 
memset&#40;&folder, 0, sizeof&#40;SceIoDirent&#41;&#41;; 
&#125;
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
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

The warning means you forgot to include stdlib.h

Jim
Insert_witty_name
Posts: 376
Joined: Wed May 10, 2006 11:31 pm

Post by Insert_witty_name »

Jim wrote:The warning means you forgot to include stdlib.h

Jim
That would be string.h ;)
Cy-4AH
Posts: 44
Joined: Wed Jan 31, 2007 9:58 pm
Location: Belarus

Post by Cy-4AH »

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: Select all

        while &#40;true&#41;
        &#123;
            memset&#40;&folder, 0, sizeof&#40;SceIoDirent&#41;&#41;;
            if &#40;sceIoDread&#40;game, &folder&#41; <= 0&#41; break;
            pspDebugScreenPrintf&#40;"%s\n", folder.d_name&#41;; 
        &#125;
Mihawk
Posts: 29
Joined: Tue Apr 03, 2007 2:04 am

Post by Mihawk »

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.
weltall
Posts: 310
Joined: Fri Feb 20, 2004 1:56 am
Contact:

Post by weltall »

jimparis wrote:
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.
Post Reply