gzip on pipe error handling


 
Thread Tools Search this Thread
Top Forums Programming gzip on pipe error handling
# 1  
Old 01-20-2010
gzip on pipe error handling

Hi all...

I have the following code:
Code:
FILE *fp = popen(" gzip -dc /somemount/somefile.gz", "r");
while(fgets(buffer, 1024, fp))
{
      some code....
}

"/somemount" is a mount of some network drive. Sometimes error occurs in while loop - I can see the following "Input/Output error" which gzip throws.

The question is how I can handle this error so I'll be able, for example, to go to the begging of the file and try to read it once again?

Thanks.
# 2  
Old 01-20-2010
I would handle the error in the pclose(), via the exit code returned by gzip. An example is given below (I used the 'ls' command for illustration purposes. Using 'gzip' command should be similar).
Code:
#include <stdio.h>
#include <sys/wait.h> /* WIF.. macro */

int
main()
{
   FILE* stream;
   int   status;
   char  buffer[1024];

   stream=popen("ls DoesNotExist", "r" );
   /* TODO:  handle case stream==NULL */

   while (fgets(buffer, sizeof(buffer), stream)) {
      printf("%s", buffer);
   }

   status=pclose(stream);
   if WIFEXITED(status) {
      int ret=WEXITSTATUS(status);
      if (ret==0)
         printf("ls command was OK\n");
      else
         printf("ls command failed: returned code=%d\n", ret);
   }
   /* handle other case like signaled etc. */
}

Another possibility I see would be to roll on your own popen... But that's more work.

HTH,
Loïc.
# 3  
Old 01-20-2010
Check the return code if you open the pipe:
Code:
FILE *fp;

if (( fp = popen(" gzip -dc /somemount/somefile.gz", "r")) == NULL)
{
  perror("popen");
  exit(1); // or do other stuff here...
}

# 4  
Old 01-20-2010
Quote:
Originally Posted by Franklin52
Check the return code if you open the pipe:
That's something the OP should do anyway. But I don't think that this will solve his initial problem. Indeed the popen() shall succeed, as long as the gzip command gets executed in the shell (and regardless to the eventual error gzip might produce).

Loïc.
# 5  
Old 01-21-2010
Quote:
Originally Posted by Franklin52
Check the return code if you open the pipe:
Code:
FILE *fp;

if (( fp = popen(" gzip -dc /somemount/somefile.gz", "r")) == NULL)
{
  perror("popen");
  exit(1); // or do other stuff here...
}

This won't help, because an error occurs after the pipe was opened.
Somewhere during the reading the file...

---------- Post updated at 01:21 AM ---------- Previous update was at 01:09 AM ----------

Loic Domaigne: Thanks! Will try it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error handling for file

Hi Guys, I got a csv with pipe delimted file and i want to check second column of the file has any alpha character becuase I am expecting only number in that, and if any alpha characters then it should throw an error Thanks in advance (1 Reply)
Discussion started by: Rizzu155
1 Replies

2. Shell Programming and Scripting

Error handling

Hello fellow UNIX gurus :) I have a problem regarding the script below: # Variables used in this shell. power=0 # Stores squared integer total=0 # Sum of all squared integers num=0 # Stores command line arguements # Provides error handling if command line... (5 Replies)
Discussion started by: Learn4Life
5 Replies

3. Shell Programming and Scripting

Error Handling

Below code works for different databases i.e. MYSQL and ORACLE The problem is for MYSQL in Block: if ; $? taking value accordingly but in case of ORACLE $? is always taking this value as zero (0). That is the reason in Oracle it always going in else Block in any case.. :( and in case of ... (4 Replies)
Discussion started by: ambarginni
4 Replies

4. Shell Programming and Scripting

sh: gzip: not found ERROR

I am creating a script to run the SysInfo tool under HPUX servers, this is my script! #!/usr/bin/ksh # # Date: February 29th 2011 # #Definicion de variables PATH_TMP=/home/eponcede > HPUX_SysInfo.log for host in `cat $PATH_TMP/servers/host_hp2_test` do echo... (2 Replies)
Discussion started by: eponcedeleonc
2 Replies

5. UNIX for Advanced & Expert Users

gzip vs pipe gzip: produce different file size

Hi All, I have a random test file: test.txt, size: 146 $ ll test.txt $ 146 test.txt Take 1: $ cat test.txt | gzip > test.txt.gz $ ll test.txt.gz $ 124 test.txt.gz Take 2: $ gzip test.txt $ ll test.txt.gz $ 133 test.txt.gz As you can see, gzipping a file and piping into gzip... (1 Reply)
Discussion started by: hanfresco
1 Replies

6. Shell Programming and Scripting

Error Handling

Helo Experts, I need a help in handling errors in shell script, wants my errors displayed in text file instead of command window.. My shell script is here; cd /cygdrive/s/Files for FILES in ./*.* do temp=`basename $FILES` if cp $FILES /cygdrive/r/CopyFile1/$FILES; then echo "copy... (5 Replies)
Discussion started by: CelvinSaran
5 Replies

7. Shell Programming and Scripting

SFTP Error Handling

Hi , Can any one tell me is there any standard method to track errors during sftp ? using which command i can track sftp errors ? i tried using echo $? . Most of the times i am getting error number 127 ,1, 255. whether it is constant numbers ? Please help me out. Thanks in advance (2 Replies)
Discussion started by: deepusunil
2 Replies

8. UNIX for Dummies Questions & Answers

gunzip error - not in gzip format

Hi, I am getting this error gunzip file1.tar.Z gunzip: file1.tar.Z: not in gzip format Any clues? This goes bad only in some recent installations of ids (5 Replies)
Discussion started by: eagercyber
5 Replies

9. AIX

GZIP ERROR! -- Plesae help! -- Urgent

I have two huge files on AIX Ver 5.0. File size of each file is 6238884375 bytes. There is huge difference in sizes when I zip them by gzip coomand. File1.gz 586147513 File2.gz 547585695 Any idea why it is so? Thanks Sumit (2 Replies)
Discussion started by: sumitc
2 Replies

10. Shell Programming and Scripting

An error with gzip

Have an issue with the following snippet of code, in particular the execution of the `gzip -9 ${ARCHIVE_FILE}`. It is failing with a ReturnCode of 1 - Can anyone lead me to a souce that identifies & describes what RC's there are for gzip, as I've not been able to find any. echo '-- TARing up... (1 Reply)
Discussion started by: Cameron
1 Replies
Login or Register to Ask a Question