LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   write to /dev/full is successful !!! what the hell :) (https://www.linuxquestions.org/questions/programming-9/write-to-dev-full-is-successful-what-the-hell-263188/)

miko3k 12-06-2004 07:02 AM

write to /dev/full is successful !!! what the hell :)
 
t have written a simple program which writes 100 bytes to /dev/full and it works!!! what the hell ? see below how i figured it out...

Code:

miko@miko:~/tmp$ cat > a.c
#include <stdio.h>

int main(void)
{
  char str[100];
  FILE *f;

  if(!(f=fopen("/dev/full","w"))) {
    printf("fuck fuck fuck\n");
    return 1;
  }

  printf("%d\n",fwrite(str,1,100,f));
  return 0;
}
miko@miko:~/tmp$ gcc a.c
miko@miko:~/tmp$ ./a.out
100
miko@miko:~/tmp$ cat a.c  > /dev/full
cat: write error: No space left on device

what's wrong with my code ? please help me!!

jim mcnamara 12-06-2004 09:11 AM

Do you mean /dev/null?

itsme86 12-06-2004 10:46 AM

I get the same result. It must be writing successfully to the "standard I/O buffer" instead of the actual device. The low level I/O functions don't seem to have the same shortcoming.
Code:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(void)
{
  char str[100];
  int fd;

  if((fd = open("/dev/full", O_WRONLY)) < 0)
  {
    printf("fsck\n");
    return 1;
  }

  if(write(fd, str, 100) < 0)
    perror("write()");
  else
    puts("Data written successfully!");

  return 0;
}

Quote:

itsme@dreams:~/C$ ./full2
write(): No space left on device

itsme86 12-06-2004 10:51 AM

Ahh...I see. It doesn't fail until the standard I/O buffer if flushed:
Code:

#include <stdio.h>
#include <errno.h>

int main(void)
{
  char str[100];
  FILE *f;

  if(!(f = fopen("/dev/full", "w")))
  {
    printf("fsck\n");
    return 1;
  }

  printf("%d\n", fwrite(str, 1, 100, f));
  if(fflush(f))
    perror("fwrite()");
  return 0;
}

Quote:

itsme@dreams:~/C$ ./full
100
fwrite(): No space left on device

miko3k 12-07-2004 07:39 AM

yeah... thanks :)


All times are GMT -5. The time now is 12:36 PM.