Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
This is an archived, read-only copy of the United-TI subforum , including posts and topic from May 2003 to April 2012. If you would like to discuss any of the topics in this forum, you can visit Cemetech's Technology & Calculator Open Topic subforum. Some of these topics may also be directly-linked to active Cemetech topics. If you are a Cemetech member with a linked United-TI account, you can link United-TI topics here with your current Cemetech topics.

This forum is locked: you cannot post, reply to, or edit topics. Community News & Discuss Nspire => Technology & Calculator Open Topic
Author Message
bwang


Member


Joined: 15 Mar 2009
Posts: 128

Posted: 25 Aug 2010 06:50:48 pm    Post subject:

Is there any way to get the size of a file in C on the Nspire? There's no fseek() and no struct stat to use stat () with.
Back to top
bwang


Member


Joined: 15 Mar 2009
Posts: 128

Posted: 25 Aug 2010 08:00:17 pm    Post subject:

w00t!
I figured out.
My code:

Code:

#include <os.h>
#include "utils.h"

asm(".string \"PRG\"\n");

int main(void)
{
  char* s = (char*) malloc(256);
  memset(s, 0x00, 256);
  stat("/documents/Examples/file.tns", s);
  printf("stat struct location: %X\n", s);
  int size = s[20] + 0x100 * s[21] + 0x10000 * s[22] + 0x1000000 * s[23];
  printf("Files Size: %d\n", size);
  printf("%s", "\n");
  return 0;
 }
Back to top
ExtendeD


Advanced Newbie


Joined: 30 Aug 2009
Posts: 91

Posted: 26 Aug 2010 02:06:25 pm    Post subject:

Here is the definition of struct stat in the current development branch of Ndless, that you may temporarily add to your common.h before the next release:


Code:
struct stat {
   unsigned short st_dev;
   unsigned int st_ino; // 0
   unsigned int st_mode; // see S_ macros above
   unsigned short st_nlink; // 1
   unsigned short st_uid; // 0
   unsigned short st_gid; // 0
   unsigned short st_rdev; // = st_dev
   unsigned int st_size;
   unsigned int st_atime;
   unsigned int st_mtime;
   unsigned int st_ctime;
};
Back to top
bsl


Advanced Newbie


Joined: 09 Jan 2010
Posts: 94

Posted: 27 Aug 2010 05:04:51 pm    Post subject:

Here is one more method that uses fseek and ftell:
put in os.h:

Code:

#define fseek        (_oscall(int,  fseek_, FILE * stream , long offset ,int origin ))
#define ftell        (_oscall(long,  ftell_, FILE * stream  ) )
#define fflush       (_oscall(int,  fflush_, FILE * stream  ) )

put in os_1.1.9253.h:

Code:

#define fseek_            0x10185330
#define ftell_            0x10185480 

put in os_cas_1.1.9174.h:

Code:

#define fseek_            0x10185038
#define ftell_            0x10185188


put in common.h:

Code:

#define SEEK_SET 0   
#define SEEK_CUR 1
#define SEEK_END 2


getsize.c:

Code:

#include <os.h>

asm(".string \"PRG\"\n");
int main ()
{
  FILE *handle;
  long size;

  handle = fopen ("/documents/Examples/fseekftell.tns","rb");
  if (handle==NULL) printf("Error opening file\n");
  else
  {
    fseek (handle, 0, SEEK_END);
    size = ftell (handle);
    fclose (handle);
    printf ("Size of /documents/Examples/fseekftell.tns: %ld bytes.\n",size);
  }
  return 0;
}

[s]
I am making a good guess that the entry is correct because the file routines are grouped in one area on boot2
that use one argument thats a pointer:
[/s] Confirmed !!! attachment below

Code:

boot2_1.4.1571.idc.txt:
0X1185625C   "file_undef1");  takes 1 arg - definitely a pointer - FILE* ? - fflush(handle)??
0X1185634C   "fclose");
0X118563C8   "file_undef2");  takes 1 arg - definitely a pointer to a char: ldrb    r3,[r0]
0X11856460   "file_undef3");  takes 3 args
0X11856590   "fopen");
0x11856688       mov r12, sp    <- unknown entry point   takes 3 args
0X118566CC   "fread");
0X11856808   "fseek");
0X11856958   "file_undef6"); takes 1 arg - definitely a pointer- FILE* ?  - ftell(handle) ?? -> confirmed now !!!
0X11856A80   "fwrite");


Put this file in the examples folder :
[attachment=3261:fseekftell.zip]


Last edited by Guest on 30 Aug 2010 09:50:46 pm; edited 1 time in total
Back to top
bsl


Advanced Newbie


Joined: 09 Jan 2010
Posts: 94

Posted: 30 Aug 2010 12:51:12 pm    Post subject:

bwang wrote:


Code:

  int size = s[20] + 0x100 * s[21] + 0x10000 * s[22] + 0x1000000 * s[23];


Another way to do this without defining structures is using read_unaligned_longword
Here you would not need it because 20 is divisible by 4 - so its aligned, but in general without
defining structures read_unaligned_longword will work every time.
I am going to finish off the directory listing code on the other thread using this.
ndless 1.1 was easy to install - I manually editted MSYS for the PATH.
PUT this in os.h:

Code:

#define read_unaligned_longword       (_oscall(unsigned long,  read_unaligned_longword_, void *ptr  ) )

put in os_1.1.9253.h:

Code:

#define read_unaligned_longword_   0x100ec4e4

put in os_cas_1.1.9174.h:

Code:

#define read_unaligned_longword_   0x100ec514

Then:

Code:

  int size = read_unaligned_longword(s[20]);
 UPDATE: int size = read_unaligned_longword(&s[20] or even (void *)&s[20]);


Last edited by Guest on 11 Sep 2010 12:15:48 am; edited 1 time in total
Back to top
calc84maniac


Elite


Joined: 22 Jan 2007
Posts: 770

Posted: 31 Aug 2010 09:06:53 am    Post subject:

bsl wrote:

bwang wrote:


Code:

  int size = s[20] + 0x100 * s[21] + 0x10000 * s[22] + 0x1000000 * s[23];


Another way to do this without defining structures is using read_unaligned_longword
Here you would not need it because 20 is divisible by 4 - so its aligned, but in general without
defining structures read_unaligned_longword will work every time.
I am going to finish off the directory listing code on the other thread using this.
ndless 1.1 was easy to install - I manually editted MSYS for the PATH.
PUT this in os.h:

Code:

#define read_unaligned_longword       (_oscall(unsigned long,  read_unaligned_longword_, void *ptr  ) )

put in os_1.1.9253.h:

Code:

#define read_unaligned_longword_   0x100ec4e4

put in os_cas_1.1.9174.h:

Code:

#define read_unaligned_longword_   0x100ec514

Then:

Code:

  int size = read_unaligned_longword(s[20]);



Shouldn't that be


Code:

  int size = read_unaligned_longword(&s[20]);
Back to top
bsl


Advanced Newbie


Joined: 09 Jan 2010
Posts: 94

Posted: 31 Aug 2010 10:07:44 am    Post subject:

Yes, thanks...
I am wondering if there is :
write_unaligned_word
write_unaligned_longword
Back to top
Goplat


Advanced Newbie


Joined: 26 Jun 2007
Posts: 95

Posted: 03 Sep 2010 10:45:53 am    Post subject:

bwang wrote:

int main(void)
{
char* s = (char*) malloc(256);
...
return 0;
}


Memory allocated by Ndless programs can't be automatically freed on exit like we're used to on both Windows and Unix. You must free every block you allocate, or else that memory becomes unusable until the next reboot.

(Note that for a small temporary object such as this, it would be better to just put it directly on the stack with "char s[256]" anyway.)


Last edited by Guest on 03 Sep 2010 10:46:50 am; edited 1 time in total
Back to top
Display posts from previous:   
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
    »
» View previous topic :: View next topic  
Page 1 of 1 » All times are GMT - 5 Hours

 

Advertisement