No error but not executing

Hi friends
When I compile thic program by gcc filename, it shows no error.
But after that if I execute the program gets stuck.
Can any one find out.

#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
int fd,changes,i,random_spot,kids[2];
struct stat buf;
char *the_file,*starting_string="Finally got it! \n";
size_t length=strlen(the_file);
size_t starting=strlen(starting_string);
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);
}
write(fd,starting_string,starting);
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==(caddr_t)-1)
{
fprintf(stderr,"mmap failure\n");
exit(5);
}
for(i=0;i<length;++i)
{
if(*(the_file+i)>='0' && *(the_file+i)<='9')
*(the_file+i)='*';
sleep(1);
}
printf("Parent done with changes\n");
 printf("The file now contains:\n%s\n",the_file);
exit(0);
}

---------- Post updated at 10:59 AM ---------- Previous update was at 10:47 AM ----------

That program has errors ... I changed it and removed all errors.
Now, there is executing error. Check properly

---------- Post updated at 11:01 AM ---------- Previous update was at 10:59 AM ----------

sorry .. it did not show up.. thats y i posted it again

#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
        int fd,changes,i,random_spot,kids[2];
        struct stat buf;
        char *the_file,*starting_string="Finally got it! \n";
        size_t length=strlen(the_file);

strlen(the_file) will either crash or return garbage when you give it a variable you didn't initialize. the_file could contain anything at that point.

You don't ever set the_file to a string anyway, so strlen() will still return garbage or crash even after you initialize it. Remember, not all arrays are strings! Your file will contain:

Finally got it! \n while a string contains Finally got it! \n\0 The \0 is how strlen() knows where the string ends, the compiler adds it to the end of things in double-quotes for you. strlen() doesn't count it in the length either, so you never write it.

You don't need length anyway. buf.st_size already tells you the exact size of the file.

size_t starting=strlen(starting_string);
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);
}
write(fd,starting_string,starting);
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==(caddr_t)-1)

(caddr_t)-1 should be MAP_FAILED.

{
fprintf(stderr,"mmap failure\n");
exit(5);
}

for(i=0;i<length;++i)
{
if(*(the_file+i)>='0' && *(the_file+i)<='9')
*(the_file+i)='*';
sleep(1);
}

replace 'length' with buf.st_size. strlen() can't tell you how long a file is, but fstat already did.

*(the_file+i) can be written much more clearly as the_file

printf("Parent done with changes\n");
 printf("The file now contains:\n%s\n",the_file);

the_file is still not a string. You can't print it with %s because printf won't know where the text ends. You do know the file size, though, which will let you write it instead. write(STDOUT_FILENO, the_file, buf.st_size);

exit(0);
}
1 Like

But the program shows lot of errors now.
Can you try it????

Thank you very much

What errors? In what code? I can't see the changes you've made to your program from here.

#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
int fd,changes,i,random_spot,kids[2];
struct stat buf;
char *the_file,*starting_string="Finally got it! \n";
size_t starting=strlen(starting_string);
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);
}
write(fd,starting_string,starting);
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED)
{
fprintf(stderr,"mmap failure\n");
exit(5);
}
for(i=0;i<buf.st_size;++i)
{
if(the_file>='0' && the_file<='9')
the_file='*';
// the program takes seventeen seconds to run with this in
// sleep(1);
}
printf("Parent done with changes\n");
 printf("The file now contains:\n");
write(STDOUT_FILENO, the_file, buf.st_size);
printf("\n");
exit(0);
}
#include<stdio.h>
#include<sys/types.h>
#include<sys/mman.h>
#include<stdlib.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>

main(int argc, char *argv[])
{
int fd,changes,i,random_spot,kids[2];
struct stat buf;
char *the_file,*starting_string="Finally got it! \n\0";
if (argc!=2)
{
fprintf(stderr, "Usage %s file_name \n",*argv);
exit(1);
}
if((fd=open(argv[1], O_CREAT | O_RDWR,0666))<0)
{
fprintf(stderr,"open error on file %s\n",*argv);
exit(3);

}
write(fd,starting_string,strlen(starting_string));
if(fstat(fd,&buf)<0)
{
fprintf(stderr,"fstat error on file %s\n",*argv);
exit(4);
}
if((the_file=mmap(0,(size_t)
buf.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0))==MAP_FAILED)
{
fprintf(stderr,"mmap failure\n");
exit(5);
}
for(i=0;i<strlen(the_file);++i)
{
if(*(the_file)>='0' && *(the_file)<='9')
*(the_file)='*';
sleep(1);
}
printf("Parent done with changes\n");
write(STDOUT_FILENO, the_file, buf.st_size);
exit(0);
}

ERRORS:

num.c: In function `main':
num.c:33: warning: comparison between pointer and integer
num.c:40: invalid type argument of `unary *'
num.c:40: invalid type argument of `unary *'
num.c:41: invalid type argument of `unary *'

please use code tags. Thank you

---------- Post updated at 04:32 PM ---------- Previous update was at 04:00 PM ----------

code tag means???

char *the_file,*starting_string="Finally got it! \n\0";

As I said, strings in quotes already have a null terminator. The compiler adds them for you. So putting a \0 here is pointless, it already has one, it's just not being written to the file since strlen() doesn't count the NULL terminator as part of the string.

Which is fine, I think. You should be checking the length of the file, not just blindly reading memory. If you go beyond the end of the file in mmap-ed memory, that's either a segfault or a bus error -- a crash.

So stop using string functions already and just treat the_file like an array of characters.

I repeat: strlen() only works for strings. the_file is not a string. use buf.st_size

{
if(*(the_file)>='0' && *(the_file)<='9')
*(the_file)='*';
sleep(1);
}

Instead of using my syntax or yours, you're trying to use both. either use the_file[i], or *(the_file + i). Note the lack of * in one of the two.

Tags that surround code to make your code readable instead of a mess. If you don't understand how they work, hit the 'quote' button for my post here, I've used lots of them. [ code ] stuff that is code [ /code ] without the extra spaces between [ and ] .

I never call write() but I error check right there, check amount written, and loop if EINTR, EAGAIN if O_NONBLOCK, too, all part of the joys of raw I/O:

char *write_point ;
int char_to_write ;
int ret ;

for ( char_to_write = strlen( starting_string ), write_point = starting_string ; char_to_write ; /* nothing */ ){
  if ( 0 > ( ret = write( fd, write_point, char_to_write )){
    if ( errno == EINTR )
      continue ;
     if ( errno = EAGAIN ){
       poll( 0, 0, 1 ); /* sleep a millisecond */
       continue ;
      }
     perror( "write error" );
     exit( 1 );
   }
  write_point += ret ;
  char_to_write -= ret ;
 }
1 Like