Sponsored Content
Top Forums Programming Segfault When Parsing Delimiters In C Post 302977509 by Azrael on Tuesday 19th of July 2016 08:30:30 AM
Old 07-19-2016
Segfault When Parsing Delimiters In C

Another project, another bump in the road and another chance to learn. I've been trying to open gzipped files and parse data from them and hit a snag. I have data in gzips with a place followed by an ip or ip range sort of like this:

Code:
Some place:x.x.x.x-x.x.x.x

I was able to modify some code I found that works fine for parsing the data to only show the ips:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (void) {
char str[128];
char *ptr;

strcpy (str, "Some place:x.x.x.x-x.x.x.x");
strtok_r (str, ":", &ptr);

printf ("%s\n", ptr);
return 0;

}

Result:
Code:
$ ./test       
x.x.x.x-x.x.x.x

However, when I add it to the code I have for opening the gzips and reading them I get a segmentation fault. Here is the code I am trying to work from now:

Code:
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(int argc, char *argv[])
{
  const char prefix[] = "zcat ";
  const char *arg;
  char *strip;
  char *range;
  char *cmd;
  FILE *in;
  char buf[4096];

  if (argc != 2) {
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    return 1;
  }

  arg = argv[1];
  cmd = malloc(sizeof(prefix) + strlen(arg) + 1);
  if (!cmd) {
    fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
    return 1;
  }

  sprintf(cmd, "%s%s", prefix, arg);

  in = popen(cmd, "r");
  if (!in) {
    fprintf(stderr, "%s: popen: %s\n", argv[0], strerror(errno));
    return 1;
  }

  while (fscanf(in, "%*s %99[^\n]", buf) == 1){
    strcpy (strip, buf);
    strtok_r (strip, ":", &range);
    printf("%s\n", range);
  }

  if (ferror(in)) {
    fprintf(stderr, "%s: fread: %s\n", argv[0], strerror(errno));
    return 1;
  }
  else if (!feof(in)) {
    fprintf(stderr, "%s: %s: unconsumed input\n", argv[0], argv[1]);
    return 1;
  }

  return 0;
}

I tried to look at this with strace and it seems to die directly after reading the first line. Any thoughts appreciated.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

parsing with multible delimiters

I have data that looks like this aaa!bbb!ccc/ddd/eee It is not fixed format. I need to parse ddd into a var in order to decide if I want to process that row. If I do I need to put ccc and bbb into vars to process it. I need to do this during a while loop one record at a time. Any... (11 Replies)
Discussion started by: gillbates
11 Replies

2. Programming

gnu history library signal segfault

i am trying to use the history functions in a c++ program along with a custom signal handler for SIGINT. the prog works fine catching signals without the line: add_history(*args); but as soon as this line is added, the prog segfaults on SIGINT. does anyone have experience using gnu... (2 Replies)
Discussion started by: a1g0rithm
2 Replies

3. UNIX for Dummies Questions & Answers

[Linux] How Do I Run Until Segfault

Hello, sorry if this has been posted before but i was wondering if there is a way to run a program until a segmentation fault is found. Currently i'm using a simple shell script which runs my program 100 times, sleeps 1 second because srand(time(0)) is dependent on seconds. Is there a possible... (1 Reply)
Discussion started by: aslambilal
1 Replies

4. Solaris

Working around netscape 4.9 segfault on Solaris 8

We have a Solaris 8 server which users login to via VNC to get a desktop. On that desktop these users use Netscape Communicator 4.9 to access a very important mail account. Unfortunately Netscape has started segfaulting regularly. Does anyone have any ideas how I can try to find out what point... (1 Reply)
Discussion started by: aussieos
1 Replies

5. Programming

2 Problems: Segfault on ctrl+c and syslog() prob

1. Even if i have the handles for ctrl+c it gives off a segfault 2. syslog doesn't log LOG_ERR event with log masked specified or non specified, it logs LOG_WARNING however... #include <sys/types.h> /* include this before any other sys headers */ #include <sys/stat.h> #include <fcntl.h>... (2 Replies)
Discussion started by: james2432
2 Replies

6. Programming

id3lib SEGFAULT

Hello everyone, I'm writing a program using the id3lib unfortunately I've encountered with memory issue that cause segmentation fault. I tried to rerun and analyze the program with valgrind but it doesn't point me anywhere. I really stuck on this one. Valgrind output: ==14716== Invalid read of... (2 Replies)
Discussion started by: errb
2 Replies

7. Programming

Is Drive Valid Segfault

I have a program that allows users to specify the debug log file location and name. I have tried using the access() and stat() but they both segfault if the drive say (d:\) is invalid. Both seem to be fine if the drive exists. Could someone please point me in the direction to a function that... (1 Reply)
Discussion started by: robfwauk
1 Replies

8. Programming

segfault in pointer to string program

hello all, my question is not about How code can be rewritten, i just wanna know even though i am not using read only memory of C (i have declared str) why this function gives me segfault :wall:and the other code executes comfortably though both code uses same pointer arithmetic. ... (4 Replies)
Discussion started by: zius_oram
4 Replies

9. Programming

String array iteration causing segfault

I am populating an array of string and print it. But it going in infinite loop and causing segfault. char Name = { "yahoo", "rediff", "facebook", NULL }; main(int argc, char* argv) { int j = 0; ... (7 Replies)
Discussion started by: rupeshkp728
7 Replies

10. Shell Programming and Scripting

--Parsing out strings for repeating delimiters for everyline

Hello: I have some text output, on SunOS 5.11 platform using KSH: I am trying to parse out each string within the () for each line. I tried, as example: perl -lanF"" -e 'print "$F $F $F $F $F $F"' But for some reason, the output gets all garbled after the the first fields.... (8 Replies)
Discussion started by: gilgamesh
8 Replies
ZNEW(1) 						    BSD General Commands Manual 						   ZNEW(1)

NAME
znew -- convert compressed files to gzipped files SYNOPSIS
znew [-ftv9K] file ... DESCRIPTION
The znew utility uncompresses files compressed by compress(1) and recompresses them with gzip(1). The options are as follows: -f Overwrite existing '.gz' files. Unless this option is specified, znew refuses to overwrite existing files. -t Test integrity of the gzipped file before deleting the original file. If the integrity check fails, the original '.Z' file is not removed. -v Print a report specifying the achieved compression ratios. -9 Use the -9 mode of gzip(1), achieving better compression at the cost of slower execution. -K Keep the original '.Z' file if it uses less disk blocks than the gzipped one. A disk block is 1024 bytes. SEE ALSO
gzip(1) CAVEATS
The znew utility tries to maintain the file mode of the original file. If the original file is not writable, it is not able to do that and znew will print a warning. BSD
January 26, 2007 BSD
All times are GMT -4. The time now is 03:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy