forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Listing a directory

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
petit.padavoine



Joined: 07 Apr 2007
Posts: 4

PostPosted: Thu May 31, 2007 1:44 am    Post subject: Listing a directory Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Thu May 31, 2007 1:57 am    Post subject: Reply with quote

memset the folder struct to zero every time you do the sceiodread. someone should put this in the headers...
Back to top
View user's profile Send private message Visit poster's website
petit.padavoine



Joined: 07 Apr 2007
Posts: 4

PostPosted: Thu May 31, 2007 2:50 am    Post subject: Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Thu May 31, 2007 3:20 am    Post subject: Reply with quote

yes exactly
Back to top
View user's profile Send private message Visit poster's website
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Thu May 31, 2007 4:56 am    Post subject: Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Thu May 31, 2007 5:51 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
jimparis



Joined: 10 Jun 2005
Posts: 1179
Location: Boston

PostPosted: Thu May 31, 2007 6:10 am    Post subject: Reply with quote

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
View user's profile Send private message
petit.padavoine



Joined: 07 Apr 2007
Posts: 4

PostPosted: Thu May 31, 2007 3:52 pm    Post subject: Reply with quote

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
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Thu May 31, 2007 3:53 pm    Post subject: Reply with quote

The warning means you forgot to include stdlib.h

Jim
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Thu May 31, 2007 6:12 pm    Post subject: Reply with quote

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

Jim


That would be string.h ;)
Back to top
View user's profile Send private message
Cy-4AH



Joined: 31 Jan 2007
Posts: 44
Location: Belarus

PostPosted: Fri Jun 01, 2007 12:46 am    Post subject: Reply with quote

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
View user's profile Send private message
Mihawk



Joined: 03 Apr 2007
Posts: 29

PostPosted: Fri Jun 01, 2007 4:14 am    Post subject: Reply with quote

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
View user's profile Send private message
weltall



Joined: 20 Feb 2004
Posts: 310

PostPosted: Fri Jun 01, 2007 3:35 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group