Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #99098 > unrolled thread

C newb - Part III - find a .jhr file in a directory - seg fault

Started byS390 <s390@tampascanner.info>
First post2016-12-12 17:58 +0000
Last post2016-12-20 14:38 -0800
Articles 4 — 3 participants

Back to article view | Back to comp.lang.c


Contents

  C newb - Part III - find a .jhr file in a directory - seg fault S390 <s390@tampascanner.info> - 2016-12-12 17:58 +0000
    Re: C newb - Part III - find a .jhr file in a directory - seg fault Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-12-12 22:18 +0000
      Re: C newb - Part III - find a .jhr file in a directory - seg fault S390 <s390@tampascanner.info> - 2016-12-20 22:12 +0000
        Re: C newb - Part III - find a .jhr file in a directory - seg fault Barry Schwarz <schwarzb@dqel.com> - 2016-12-20 14:38 -0800

#99098 — C newb - Part III - find a .jhr file in a directory - seg fault

FromS390 <s390@tampascanner.info>
Date2016-12-12 17:58 +0000
SubjectC newb - Part III - find a .jhr file in a directory - seg fault
Message-ID<azB3A.89805$dL2.72348@fx12.iad>
In looking to build up the blocks of what I want to do for my program, I 
am starting with one piece of that.

finding all *.jhr in a directory

So looking over some examples....and building from them I the below, but 
when doing the test to find *.jhr files it seg faults... tried on a 
couple versions of GCC

/jamtest $ gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5.1) 4.4.5

@fidodev:~/jamtest$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.

I am lost at this point... why the test for the *.jhr file fails and seg 
faults...


$ cat dirtest.c 

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     printf("String after |%c| is - |%s|\n", ch, ret);    
     
      //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) //
     if (strcmp(ret,".jhr") != 0)
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);

This results in:

@fidodev:~/jamtest$ make dirtest
cc     dirtest.c   -o dirtest
@fidodev:~/jamtest$ ./dirtest 
String after |.| is - |..|
..
String after |.| is - |.jlr|
TESTJAMNNTPD.jlr
String after |.| is - |.jdx|
TESTJAMNNTPD.jdx
String after |.| is - |(null)|
Segmentation fault (core dumped)

BUT

if I remove the test to find the the .jhr file(s).

$ cat dirtest.c 

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     printf("String after |%c| is - |%s|\n", ch, ret);    
     
     if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0) 
     // if (strcmp(ret,".jhr") != 0)//
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);

This results in:

@fidodev:~/jamtest$ make dirtest
cc     dirtest.c   -o dirtest
@fidodev:~/jamtest$ ./dirtest 
String after |.| is - |..|
..
String after |.| is - |.jlr|
TESTJAMNNTPD.jlr
String after |.| is - |.jdx|
TESTJAMNNTPD.jdx
String after |.| is - |(null)|
Segmentation fault (core dumped)

BUT

if I remove the test to find the the .jhr file(s).


$ cat dirtest.c 

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     printf("String after |%c| is - |%s|\n", ch, ret);    
     
     //if (strcmp(dir->d_name,"TESTJAMNNTPD.jhr") == 0)  //
     if (strcmp(ret,".jhr") != 0)
        printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);

$ make dirtest
cc     dirtest.c   -o dirtest
@fidodev:~/jamtest$ ./dirtest
String after |.| is - |..|
String after |.| is - |.jlr|
String after |.| is - |.jdx|
String after |.| is - |(null)|
String after |.| is - |.out|
String after |.| is - |.jdt|
String after |.| is - |.|
String after |.| is - |.jhr|
TESTJAMNNTPD.jhr
String after |.| is - |(null)|
String after |.| is - |.cmhw|
String after |.| is - |(null)|
String after |.| is - |.c|
String after |.| is - |.c|
@fidodev:~/jamtest$ 

Does not matter if the test is to find NOT .jhr or to find .jhr

this is if (strcmp(ret,".jhr") == 0)

$ ./dirtest
String after |.| is - |..|
String after |.| is - |.jlr|
String after |.| is - |.jdx|
String after |.| is - |(null)|
Segmentation fault (core dumped)


directory:
~/jamtest$ ls -la
total 88
drwxrwxr-x  3 me me  4096 Dec 12 12:40 .
drwxr-xr-x 23 me me  4096 Dec 12 12:34 ..
-rwxrwxr-x  1 me me 17148 Dec  2 14:35 a.out
-rwxrwxr-x  1 me me  8837 Dec 12 12:40 dirtest
-rw-r--r--  1 me me   613 Dec 12 12:40 dirtest.c
drwxr-xr-x  2 me me  4096 Dec  2 14:33 jamlib
-rwxrwxr-x  1 me me 17184 Dec  6 21:58 jamtest
-rw-r--r--  1 me me   546 Dec  6 21:58 jamtest.c
-rw-r--r--  1 me me     4 Dec  2 14:33 TESTJAMNNTPD.cmhw
-rw-r--r--  1 me me   268 Dec  2 14:33 TESTJAMNNTPD.jdt
-rw-r--r--  1 me me    24 Dec  2 14:33 TESTJAMNNTPD.jdx
-rw-r--r--  1 me me  1839 Dec  2 14:33 TESTJAMNNTPD.jhr
-rw-r--r--  1 me me     0 Dec  2 14:33 TESTJAMNNTPD.jlr


-- 
I MADE America Great Again! President Trump! I put a stake in that bitch 
for good!

[toc] | [next] | [standalone]


#99142

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2016-12-12 22:18 +0000
Message-ID<87k2b4pzac.fsf@bsb.me.uk>
In reply to#99098
S390 <s390@tampascanner.info> writes:
<snip>
> I am lost at this point... why the test for the *.jhr file fails and seg 
> faults...
>
>
> $ cat dirtest.c 
>
> #include <dirent.h> 
> #include <stdio.h> 
> #include <string.h>
>
> int main(void)
> {
>   char hdrfile;
>   int len;
>   const char ch = '.';
>   char *ret;
>   DIR           *d;
>   struct dirent *dir;
>   d = opendir(".");
>   if (d)
>   {
>     while ((dir = readdir(d)) != NULL)
>     {
>      ret = strchr(dir->d_name, ch);

When the name does not have a '.' in it, the result will be a null
pointer -- a special value that does not point at anything, often used
to indicate failure in some way.  Doing anything with a null pointer is
a Bad Thing, but printf:

>      printf("String after |%c| is - |%s|\n", ch, ret);

tries to help but printing (null), but this test

>      if (strcmp(ret,".jhr") != 0)

Will almost certainly go wrong.

>         printf("%s\n", dir->d_name);
>     }
>
>     closedir(d);
>   }
>
>   return(0);

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#99943

FromS390 <s390@tampascanner.info>
Date2016-12-20 22:12 +0000
Message-ID<11i6A.85165$Cx7.36410@fx11.iad>
In reply to#99142
On Mon, 12 Dec 2016 22:18:03 +0000, Ben Bacarisse wrote:

> When the name does not have a '.' in it, the result will be a null
> pointer -- a special value that does not point at anything, often used
> to indicate failure in some way.  Doing anything with a null pointer is
> a Bad Thing, but printf:

I don't want to go down the rabbit hole on this too much.. but this seems 
to be an C issue with these  "pointers"

I am not interested in a "pointer"

If I do something like this

If x=3.141

or

If x="STupid C Thing" 

I am not asking about "pointers" I am only interested in the value of x 
be it what ever, and if its empty/nothing aka null, then the test would 
fail... as empty/nothing/null is not either of the values I tested for 
above... Seems to be no point to this.

Why??? See below....
 

>>      if (strcmp(ret,".jhr") != 0)
> 
> Will almost certainly go wrong.
> 

This is why its bad!

So this nested if loop is the /*solution?*/

Seems terribly sloppy to me!

#include <dirent.h> 
#include <stdio.h> 
#include <string.h>

int main(void)
{
  char hdrfile;
  int len;
  const char ch = '.';
  char *ret;
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
     ret = strchr(dir->d_name, ch);
     //printf("String after |%c| is - |%s|\n", ch, ret);    
      if (ret)
      {
	if (strcmp(ret,".jhr") == 0)
	{
	printf("ret is: |%s|\n",dir->d_name);
	}
      }
      
     //end while //
     }

    closedir(d);
  }

  return(0);
}


-- 
I MADE America Great Again! President Trump! I put a stake in that bitch 
for good!

[toc] | [prev] | [next] | [standalone]


#99946

FromBarry Schwarz <schwarzb@dqel.com>
Date2016-12-20 14:38 -0800
Message-ID<66cj5cp9700hkumt1ed5nbc3i4jdlph1d0@4ax.com>
In reply to#99943
On Tue, 20 Dec 2016 22:12:13 GMT, S390 <s390@tampascanner.info> wrote:

>On Mon, 12 Dec 2016 22:18:03 +0000, Ben Bacarisse wrote:
>
>> When the name does not have a '.' in it, the result will be a null
>> pointer -- a special value that does not point at anything, often used
>> to indicate failure in some way.  Doing anything with a null pointer is
>> a Bad Thing, but printf:
>
>I don't want to go down the rabbit hole on this too much.. but this seems 
>to be an C issue with these  "pointers"
>
>I am not interested in a "pointer"

Perhaps you should be.

>If I do something like this
>
>If x=3.141
>
>or
>
>If x="STupid C Thing" 
>
>I am not asking about "pointers" I am only interested in the value of x 

Ignoring the mistake of = when you meant ==, your second test is most
certainly asking about pointers.

>be it what ever, and if its empty/nothing aka null, then the test would 
>fail... as empty/nothing/null is not either of the values I tested for 
>above... Seems to be no point to this.
>
>Why??? See below....
> 
>
>>>      if (strcmp(ret,".jhr") != 0)
>> 
>> Will almost certainly go wrong.
>> 
>
>This is why its bad!
>
>So this nested if loop is the /*solution?*/
>
>Seems terribly sloppy to me!

Maybe C is not the right language for your programming style.

>#include <dirent.h> 
>#include <stdio.h> 
>#include <string.h>
>
>int main(void)
>{
>  char hdrfile;
>  int len;
>  const char ch = '.';
>  char *ret;
>  DIR           *d;
>  struct dirent *dir;
>  d = opendir(".");
>  if (d)
>  {
>    while ((dir = readdir(d)) != NULL)
>    {
>     ret = strchr(dir->d_name, ch);
>     //printf("String after |%c| is - |%s|\n", ch, ret);    
>      if (ret)
>      {
>	if (strcmp(ret,".jhr") == 0)
>	{

I don't know why you chose to search for '.' and then check for ".jhr"
when you could have combined everything into a single call to strstr
and searched for ".jhr" directly.  You don't need a sequence of nested
if statements.

>	printf("ret is: |%s|\n",dir->d_name);
>	}
>      }
>      
>     //end while //
>     }
>
>    closedir(d);
>  }
>
>  return(0);
>}

-- 
Remove del for email

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web