simple file system

G

Greg

I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

Thanks

Greg
 
M

Michael Mair

Greg said:
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

What kind of garbage? Has it to be some pseudorandom stuff or
can it be the ramblings of the most infamous posters of this
newsgroup or some garbage pattern like "Garbage!" or ...?

I would suggest a look at fopen(), fclose(), fwrite() from
the standard library.
OTOH, as you want to give the world a new file system, this may
be exactly what you don't want to do. Fast solutions for your
specific system may necessitate leaving the realms of standard C.

IMO, it would be good if you elaborate what you mean by "best way"
and "garbage".


Cheers
Michael
 
D

dandelion

Greg said:
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

FILE*
open_garbage(const char* filename, size_t size)
{
FILE* fp = fopen(filename, "w");

for(i=0; NULL != fp && i<size; i++)
{
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))
{
fclose(fp);
fp = NULL;
}
}

return fp;
}

My pleasure.
 
N

Neo

Greg said:
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

dd if=/dev/zero of=myfile.dat bs=1024 count=1024
this command will create a 1 MB file. all contents with zero.
U can use /dev/mem as input file for some junk data or any other device u
like...

-Neo
 
J

john blackburn

Greg said:
I am trying to write a simple file system using c. It will consist of
frames of 512 bytes, and will have a superblock etc.

From the outside it will appear as a single file in UNIX. I want to
create the file and fill it with garbage at first before I write the
superblock.

My first question is: what is the best way to create a file and fill
it with garbage?

Thanks

Greg

If you are using Linux you can transfer data from the device /dev/random
which is set up to provide random data for you. Check, but I suspect Unix
has this also.
 
J

Jack Klein

dd if=/dev/zero of=myfile.dat bs=1024 count=1024
this command will create a 1 MB file. all contents with zero.
U can use /dev/mem as input file for some junk data or any other device u
like...

Doesn't work on CP/M. Has nothing to do with the C language. Has
nothing very much to do with the English language, either.
 
M

Merrill & Michele

"dandelion"

FILE*
open_garbage(const char* filename, size_t size)
{
FILE* fp = fopen(filename, "w");

for(i=0; NULL != fp && i<size; i++)
{
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))
{
fclose(fp);
fp = NULL;
}
}

return fp;
}
Would you mind elaborating on what happens within that fputc call? MPJ
 
G

Greg

Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?
 
M

Mark McIntyre

My first question is: what is the best way to create a file and fill
it with garbage?

Install Windows 95?


Ah, sorry, no, that fills your entire filesystem with garbage. My mistake.

(gd&r)
 
M

Mike Wahler

Greg said:
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?

Make sure you specify binary mode when calling 'fopen()'.

-Mike
 
R

Richard Tobin

dandelion said:
if(EOF == fputc(random() * 0x0100 / RAND_MAX, fp))

That doesn't look very random to me... If RAND_MAX is the maximum positive
int, it will produce a lot of zeros.

(And presumably you mean rand() rather than random() for standard C.)

-- Richard
 
J

Joe Wright

Greg said:
Thanks for the quick replies!

Ok when I use the following code:

for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc (random () * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}

It fills the file with garbage, but if I have a number like 1024 in
fs_size, it for some reason creates a file of size 2028. Any ideas?

Strange. When I make a program out of it..

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
int i, fs_size = 1024;
fp = fopen("greg.bin", "wb");
for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc(random() * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}
return 0;
}

...it creates a file of 1024 bytes zero.
 
D

dandelion

Merrill & Michele said:
consist
Would you mind elaborating on what happens within that fputc call? MPJ

The actual garbage (in the form of a call to random()) is generated. The
result of random() is [0..RAND_MAX], so there is a touch of simple
arithmatic to reduce that interval to [0..256] (which should have been
[0..255], come to think of it), which is written to the stream by fputc()
cast to unsigned char.
 
D

dandelion

Joe Wright said:
Strange. When I make a program out of it..

#include <stdio.h>
#include <stdlib.h>

int main(void) {
FILE *fp;
int i, fs_size = 1024;
fp = fopen("greg.bin", "wb");
for (i = 0; NULL != fp && i < fs_size; i++) {
if (EOF == fputc(random() * 0x0100 / RAND_MAX, fp)) {
fclose(fp);
fp = NULL;
}
}
return 0;
}

..it creates a file of 1024 bytes zero.

I *told* you it was garbage... ;-)

You're correct of course. I missed some parenthesis.

(random() * 0x0100) / RAND_MAX.
 
D

dandelion

Richard Tobin said:
That doesn't look very random to me... If RAND_MAX is the maximum positive
int, it will produce a lot of zeros.

(And presumably you mean rand() rather than random() for standard C.)

I stand corrected.
 
R

Richard Tobin

dandelion said:
(random() * 0x0100) / RAND_MAX.

That still produces zero (if random() returns the full range of
positive integers). What are you trying to achieve?

rand() & 0xff will return an 8-bit random number. If you're worried
about the quality of rand(), shifting it right a little helps in some
implementations.

-- Richard
 
D

dandelion

That still produces zero (if random() returns the full range of
positive integers). What are you trying to achieve?

Prooving i'm an idiot, and i'm succeeding at it, seemingly.

Write it down to lots of work and a bit of frustration about [*(&!*@&#!@
GRRRR] hardware faults, a buggy development platform and *(&#*&$@ customers
with vague specs which change from week to week.

Seemingly my mind wasn't on it when I wrote that piece of *(&@#&$@!!!
rand() & 0xff will return an 8-bit random number. If you're worried
about the quality of rand(), shifting it right a little helps in some
implementations.

Right of course.

Reading back the code makes me think: "How the [beeep] could I write
that...."
 
F

Flash Gordon

OP indicated UNIX as platform. Not DOS.

You should still use the correct mode since one day you might need to
port it to some other system.

In any case, what does it cost to use the correct mode which *will* work
rather than the incorrect mode and break it on other systems?
 
D

dandelion

Flash Gordon said:
You should still use the correct mode since one day you might need to
port it to some other system.

I was under the impression that "b" was a DOS-only thing. But some reading
has put me right. ANSI X3.159-1989 (``ANSI C'') does define it, it's just
ignored on most Un*x's.
In any case, what does it cost to use the correct mode which *will* work
rather than the incorrect mode and break it on other systems?

Not much...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top