C — Directory exploration - c

I want to dynamically populate an array of strings with file name and directory name that are inside the specified directory path.
According to you, is this the fastest implementation for this purpose?
If not, can you suggest an alternative implementation?
int exploreDirectory(const char *dirpath, char ***list, int *numItems) {
DIR *dirstream = NULL;
struct dirent *direntp = NULL;
size_t listSize = 5;
errno = 0;
if (!(dirstream = opendir(dirpath)))
return errno;
if (!((*list) = malloc(sizeof(char *) * listSize))) {
fprintf(stderr, "Error in list allocation for file list: dirpath=%s.\n", dirpath);
exit(EXIT_FAILURE);
}
*numItems = 0;
while(1) {
errno = 0;
if (!(direntp = readdir(dirstream)))
break;
if (*numItems + 1 == listSize) {
listSize *= 2;
if (!((*list) = realloc((*list), sizeof(char *) * listSize))) {
fprintf(stderr, "Error in list reallocation for file list: dirpath=%s.\n", dirpath);
exit(EXIT_FAILURE);
}
}
*numItems += 1;
(*list)[*numItems - 1] = stringDuplication(direntp->d_name);
}
if (errno != 0) {
fprintf(stderr, "Error in readdir for file list: dirpath=%s.\n", dirpath);
exit(EXIT_FAILURE);
}
if (closedir(dirstream) == -1) {
fprintf(stderr, "Error in closedir for file list: dirpath=%s.\n", dirpath);
exit(EXIT_FAILURE);
}
free(direntp);
return 0;
}

This is the implementation based on scandir!
int exploreDirectory(const char *dirpath, char ***list, int *numItems) {
struct dirent **direntList;
int i;
errno = 0;
if ((*numItems = scandir(dirpath, &direntList, NULL, alphasort)) == -1)
return errno;
if (!((*list) = malloc(sizeof(char *) * (*numItems)))) {
fprintf(stderr, "Error in list allocation for file list: dirpath=%s.\n", dirpath);
exit(EXIT_FAILURE);
}
for (i = 0; i < *numItems; i++) {
(*list)[i] = stringDuplication(direntList[i]->d_name);
}
for (i = 0; i < *numItems; i++) {
free(direntList[i]);
}
free(direntList);
return 0;
}

Related

FFMPEG - Encode a .wav file to G711

My question is the following: how to encode a .wav file to G.711/PCM A-law?
I tried to edit this example file, but I got an EINVAL error when tried to call av_frame_get_buffer(frame, 0) with frame->nb_samples = c->frame_size which is 0 (I think this is the problem) because of pcm.c:37 (at least I think because of it).
So with which parameters allocate an audio data buffer by myself if av_frame_get_buffer() doesn't do this for me and how to use it?
Thanks for reply!
EDIT:
My code:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <libavcodec/avcodec.h>
#include <libavutil/channel_layout.h>
#include <libavutil/common.h>
#include <libavutil/frame.h>
#include <libavutil/samplefmt.h>
/* check that a given sample format is supported by the encoder */
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
{
const enum AVSampleFormat *p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
if (*p == sample_fmt)
return 1;
p++;
}
return 0;
}
/* just pick the highest supported samplerate */
static int select_sample_rate(const AVCodec *codec)
{
const int *p;
int best_samplerate = 0;
if (!codec->supported_samplerates)
return 44100;
p = codec->supported_samplerates;
while (*p) {
if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
best_samplerate = *p;
p++;
}
return best_samplerate;
}
/* select layout with the highest channel count */
static int select_channel_layout(const AVCodec *codec)
{
const uint64_t *p;
uint64_t best_ch_layout = 0;
int best_nb_channels = 0;
if (!codec->channel_layouts)
return AV_CH_LAYOUT_STEREO;
p = codec->channel_layouts;
while (*p) {
int nb_channels = av_get_channel_layout_nb_channels(*p);
if (nb_channels > best_nb_channels) {
best_ch_layout = *p;
best_nb_channels = nb_channels;
}
p++;
}
return best_ch_layout;
}
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
FILE *output)
{
int ret;
/* send the frame for encoding */
ret = avcodec_send_frame(ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending the frame to the encoder\n");
exit(1);
}
/* read all the available output packets (in general there may be any
* number of them */
while (ret >= 0) {
ret = avcodec_receive_packet(ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
fwrite(pkt->data, 1, pkt->size, output);
av_packet_unref(pkt);
}
}
int main(int argc, char **argv)
{
const char *filename;
const AVCodec *codec;
AVCodecContext *c = NULL;
AVFrame *frame;
AVPacket *pkt;
int i, j, k, ret;
FILE *f;
uint16_t *samples;
float t, tincr;
if (argc <= 1)
{
fprintf(stderr, "Usage: %s <output file>\n", argv[0]);
return 0;
}
filename = argv[1];
codec = avcodec_find_encoder(AV_CODEC_ID_PCM_ALAW);
if (!codec)
{
fprintf(stderr, "Codec not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c)
{
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
c->bit_rate = 128000;
c->sample_fmt = AV_SAMPLE_FMT_S16;
if (!check_sample_fmt(codec, c->sample_fmt))
{
fprintf(stderr, "Encoder does not support sample format %s",
av_get_sample_fmt_name(c->sample_fmt));
exit(1);
}
c->sample_rate = select_sample_rate(codec);
c->channel_layout = select_channel_layout(codec);
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
if (avcodec_open2(c, codec, NULL) < 0)
{
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "wb");
if (!f)
{
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
pkt = av_packet_alloc();
if (!pkt)
{
fprintf(stderr, "Could not allocate the packet\n");
exit(1);
}
frame = av_frame_alloc();
if (!frame)
{
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
frame->nb_samples = c->frame_size;
frame->format = c->sample_fmt;
frame->channel_layout = c->channel_layout;
// ERROR Here
ret = av_frame_get_buffer(frame, 0);
if (ret < 0)
{
fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
t = 0;
tincr = 2 * M_PI * 440.0 / c->sample_rate;
for (i = 0; i < 200; i++)
{
ret = av_frame_make_writable(frame);
if (ret < 0)
{
exit(1);
}
samples = (uint16_t*)frame->data[0];
for (j = 0; j < c->frame_size; j++)
{
samples[2*j] = (int)(sin(t)*10000);
for (k = 1; k < c->channels; k++)
{
samples[2*j + k] = samples[2*j];
}
t += tincr;
}
encode(c, frame, pkt, f);
}
encode(c, NULL, pkt, f);
fclose(f);
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&c);
return 0;
}

Debugging: Problem with linked list linkage

I have a linked list implementation as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CAPACITY 128
typedef struct blob blob;
struct blob {
char text[CAPACITY];
struct blob* prev;
struct blob* next;
};
static blob* create_blob() {
blob* newBlob = malloc(sizeof(blob));
if (!newBlob) {
fprintf(stderr, "%s\n", "Malloc failed");
return 0;
}
newBlob->prev = 0;
newBlob->next = 0;
memset(newBlob->text, 0, CAPACITY);
return newBlob;
}
int insertAtTail(blob* list[static 1]) {
blob* newBlob = create_blob();
if (!newBlob) {
fprintf(stderr, "%s\n", "Malloc failed");
return 0;
}
if (!*list) {
*list = newBlob;
return 1;
}
blob* lastElement = *list;
while (lastElement->next) {
// Check if linkage until last element is okay
if (lastElement != (lastElement->next)->prev) {
fprintf(stderr, "%s\n", "The next_element->prev does not point to the current element");
} else {
fprintf(stderr, "%s\n", "Okay");
}
lastElement = lastElement->next; // Find last Blob
}
lastElement->next = newBlob;
newBlob->prev = lastElement;
return 1;
}
int reversePrintBlobs(blob const list[static 1]) {
if (!list) {
fprintf(stderr, "%s\n", "list is empty");
return 1;
}
const blob* tmp = list;
size_t blobCounter = 1;
while (tmp->next) {
blobCounter++;
tmp = tmp->next;
}
while (tmp && tmp != list) {
if ((tmp->prev)->next != tmp) {
fprintf(stderr, "%s\n", "error with the linkage");
exit(1);
}
printf("#number = %zu: %s\n", blobCounter--, tmp->text);
tmp = tmp->prev;
}
return 0;
}
When I add some blobs with the following main() function, it seems like the prev member of my struct doesn't point to the previous element. That is, there is a problem with the linkage. Can anyone point out why?
int main() {
FILE* pFile = fopen("example.txt", "r");
if (!pFile) {
perror("fopen failed\n");
return EXIT_FAILURE ;
}
fseek(pFile, 0, SEEK_SET);
blob* list = create_blob();
blob* tmp = list;
while (!feof(pFile)) {
size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
pFile);
if (noBytesRead != CAPACITY && !feof(pFile)) {
fprintf(stderr, "%s\n", "File read failed");
exit(EXIT_FAILURE);
}
// Append a null terminating character in the end
tmp->text[noBytesRead] = '\0';
if (!feof(pFile) && insertAtTail(&list)) {
tmp = tmp->next;
}
}
}
On the other hand, If I use the following main() everything works fine.
int main() {
blob* list = create_blob();
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
insertAtTail(&list);
/* code */
return 0;
}
The bug has been fixed, the new main function is the following:
int main() {
FILE* pFile = fopen("example.txt", "r");
if (!pFile) {
perror("fopen failed\n");
return EXIT_FAILURE ;
}
fseek(pFile, 0, SEEK_SET);
blob* list = create_blob();
blob* tmp = list;
int blobCounter = 1;
while (!feof(pFile)) {
size_t noBytesRead = fread(tmp->text, sizeof(char), CAPACITY,
pFile);
if (noBytesRead != CAPACITY && !feof(pFile)) {
fprintf(stderr, "%s\n", "File read failed");
exit(EXIT_FAILURE);
}
// Append a null terminating character in the end
tmp->text[noBytesRead - 1] = '\0';
if (!feof(pFile) && insertAtTail(&list)) {
blobCounter++;
tmp = tmp->next;
}
}

Reading motion vectors of every 60th frame in video file

I have the following code which reads a video files and extracts motion vectors from each frame and outputs them to stdout:
#include <libavutil/motion_vector.h>
#include <libavformat/avformat.h>
static AVFormatContext *fmt_ctx = NULL;
static AVCodecContext *video_dec_ctx = NULL;
static AVStream *video_stream = NULL;
static const char *src_filename = NULL;
static int video_stream_idx = -1;
static AVFrame *frame = NULL;
static int video_frame_count = 0;
static int decode_packet(const AVPacket *pkt)
{
//fprintf(stderr, "%d\n", video_dec_ctx->width);
int ret = avcodec_send_packet(video_dec_ctx, pkt);
if (ret < 0) {
fprintf(stderr, "Error while sending a packet to the decoder: %s\n", av_err2str(ret));
return ret;
}
while (ret >= 0) {
ret = avcodec_receive_frame(video_dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
fprintf(stderr, "Error while receiving a frame from the decoder: %s\n", av_err2str(ret));
return ret;
}
if (ret >= 0) {
int i;
AVFrameSideData *sd;
video_frame_count++;
sd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
if (sd) {
const AVMotionVector *mvs = (const AVMotionVector *)sd->data;
for (i = 0; i < sd->size / sizeof(*mvs); i++) {
const AVMotionVector *mv = &mvs[i];
printf("%d,%4d,%4d,%4d\n",
video_frame_count,
abs(mv->src_x - mv->dst_x),
abs(mv->src_y - mv->dst_y));
}
}
av_frame_unref(frame);
}
}
return 0;
}
static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
{
int ret;
AVStream *st;
AVCodecContext *dec_ctx = NULL;
AVCodec *dec = NULL;
AVDictionary *opts = NULL;
ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
if (ret < 0) {
fprintf(stderr, "Could not find %s stream in input file '%s'\n",
av_get_media_type_string(type), src_filename);
return ret;
} else {
int stream_idx = ret;
st = fmt_ctx->streams[stream_idx];
dec_ctx = avcodec_alloc_context3(dec);
if (!dec_ctx) {
fprintf(stderr, "Failed to allocate codec\n");
return AVERROR(EINVAL);
}
ret = avcodec_parameters_to_context(dec_ctx, st->codecpar);
if (ret < 0) {
fprintf(stderr, "Failed to copy codec parameters to codec context\n");
return ret;
}
/* Init the video decoder */
av_dict_set(&opts, "flags2", "+export_mvs", 0);
if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) {
fprintf(stderr, "Failed to open %s codec\n",
av_get_media_type_string(type));
return ret;
}
video_stream_idx = stream_idx;
video_stream = fmt_ctx->streams[video_stream_idx];
video_dec_ctx = dec_ctx;
}
return 0;
}
int main(int argc, char **argv)
{
int ret = 0;
AVPacket pkt = { 0 };
if (argc != 2) {
fprintf(stderr, "Usage: %s <video>\n", argv[0]);
exit(1);
}
src_filename = argv[1];
if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
fprintf(stderr, "Could not open source file %s\n", src_filename);
exit(1);
}
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
fprintf(stderr, "Could not find stream information\n");
exit(1);
}
open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO);
av_dump_format(fmt_ctx, 0, src_filename, 0);
if (!video_stream) {
fprintf(stderr, "Could not find video stream in the input, aborting\n");
ret = 1;
goto end;
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate frame\n");
ret = AVERROR(ENOMEM);
goto end;
}
printf("framenum,dx,dy\n");
/* read frames from the file */
while (av_read_frame(fmt_ctx, &pkt) >= 0) {
if (pkt.stream_index == video_stream_idx)
ret = decode_packet(&pkt);
av_packet_unref(&pkt);
if (ret < 0)
break;
}
/* flush cached frames */
decode_packet(NULL);
end:
avcodec_free_context(&video_dec_ctx);
avformat_close_input(&fmt_ctx);
av_frame_free(&frame);
return ret < 0;
}
Instead and outputting every frame, I only want to read one frame every 60 frames. The speed of the program is very important so I want to eliminate as much unnecessary code as possible. The problem is I'm having a hard time understanding how to advance the video stream without reading in new frames. I call av_read_frame(fmt_ctx, &pkt) to get packet info that is decoded by decode_packet and then goes on to read the next, but I can't find a way to only get packet info for one frame out of each 60.
Use av_seek_frame() to read every 60th frame.

minishell malloc error with EXC_BAD_ACCESS

Hi I've recently started learning unix system programming.
I'm trying to create a minishell in c but when I run my code,
I always get:
EXC_BAD_ACCESS (code=EXC_I386_GPFLT
Don't really know what's wrong here. Searched online they say it's something wrong with malloc, but I don't see what's wrong.
Can someone help me with this problem?
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <unistd.h>
#include "minishell.h"
char promptString[] = "mysh>";
struct command_t command;
int enviromentlength;
int commandlength;
char *pathv[MAX_PATHS];
//to display the prompt in the front of every line
void printPrompt()
{
printf("%s", promptString);
}
//get the user's command
void readCommand(char *buffer)
{
gets(buffer);
}
//get the environment variable and store in a pathEnvVar
int parsePath( char* dirs[] )
{
char* pathEnvVar;
char* thePath;
int i;
for(i = 0; i < MAX_ARGS; i++)
{
dirs[i] = NULL;
}
i = 0;
//use system call to get the environment variable
pathEnvVar = (char*) getenv("PATH");
//printf("%s\n", pathEnvVar);
thePath = (char*) malloc(strlen(pathEnvVar) + 1);
strcpy(thePath, pathEnvVar);
//splict the variable and store in the pathv
char *temp = strtok(thePath, ":");
dirs[i] = temp;
while(temp != NULL)
{
i++;
temp = strtok(NULL, ":");
if(temp == NULL)
{
break;
}
else
{
dirs[i] = temp;
}
}
dirs[i+1] = NULL;
return i;
}
//get the user's command and parameters
int parseCommand(char * commandline)
{
int i = 0;
char* temp;
temp = strtok(commandline, " ");
while(temp != NULL)
{
command.argv[i] = temp;
i++;
temp = strtok(NULL, " ");
}
command.argv[i] = NULL;
return i;
}
//input the user's command to
//fix the absolute path of the command
char* lookupPath(char* dir[], char* command[])
{
char* result = NULL;
int i;
//printf("%c\n", *command.argv[0]);
//if the command is already an absolute path
if(*command[0] == '/')
{
result = command[0];
//printf("test\n");
if( access(result, X_OK) == 0)
{
return result;
}
else
{
fprintf(stderr, "%s: command not found\n", result);
return NULL;
}
}
//if the command is not an absolute path
else
{
for(i = 0; i < enviromentlength; i++)
{
char *temp = (char *) malloc (30);
strcpy(temp, dir[i]);
strcat(temp, "/");
strcat(temp, command[0]);
result = temp;
if( access(result, X_OK) == 0)
{
return result;
}
}
fprintf(stderr, "%s: command not found\n", result);
return NULL;
}
}
//to change the directory and
//display the absolute path of the current directory
void do_cd(char* dir[])
{
char currentdirectory[MAX_PATHS];
if(dir[1] == NULL || (strcmp(dir[1], ".") == 0))
{
printf("director does not change\n");
//printf("The current directory is:%s", currentdirectory);
}
else
{
if(chdir(dir[1]) < 0)
{
printf("change director error\n");
}
else
{
printf("change director success\n");
}
}
getcwd(currentdirectory, MAX_PATHS);
printf("The current directory is:%s\n", currentdirectory);
}
//redirection the result to file
void redirection(char* command, char* commandcontent[], int position, pid_t thisChPID)
{
char* content[commandlength - 1];
char* filename = (char *) malloc(MAX_PATH_LEN);
FILE* fid;
int i = 0;
int stat;
strcpy(filename, commandcontent[position + 1]);
//printf("%s\n", commandcontent[position + 1]);
for(i = 0; i < position; i++)
{
content[i] = commandcontent[i];
//printf("content: %s\n", content[i]);
}
content[i + 1] = NULL;
for(i = 0; i< position + 1; i++)
{
printf("%s\n", content[i]);
}
printf("%s\n", command);
if((thisChPID=fork()) < 0)
{
fprintf(stderr, "fork failed\n");
}
else if(thisChPID == 0)
{
fid = open(filename, O_WRONLY || O_CREAT);
close(1);
dup(fid);
close(fid);
execve(command, content, pathv);
}
else
{
wait(&stat);
}
}
//use pipe to run the program
void piperun(char* command, char* commandcontent[], int position, pid_t thisChPID)
{
printf("%s\n%d\n", command, position);
char* firstcommand[position+1];
char* secondcommand[commandlength-position];
char* result = (char *) malloc(MAX_PATH_LEN);
pid_t child;
//the pipe name
int pipeID[2];
int j;
for(j = 0; j< position; j++)
{
firstcommand[j] = commandcontent[j];
printf("%s\n", firstcommand[j]);
}
firstcommand[j] = NULL;
printf("length: %d\n", commandlength-position);
for(j = 0; j < (commandlength-position); j++)
{
secondcommand[j] = commandcontent[position + 1 + j];
printf("second:%s\n",secondcommand[j]);
}
//secondcommand[j+1] = NULL;
result = lookupPath(pathv, secondcommand);
//printf("%s\n", secondcommand[0]);
printf("%s\n", result);
//create pipe "pipeID"
if(pipe(pipeID)==-1)
{
printf("Fail to creat pipe.\n");
}
if((thisChPID=fork())==-1)
{
printf("Fail to creat child process.\n");
}
if(thisChPID==0)
{
printf("in the child\n");
close(1);
dup(pipeID[1]);
close(pipeID[0]);
close(pipeID[1]);
if(execve(command, firstcommand, pathv)==-1)
{
printf("Child process can't exec command %s.\n",firstcommand[0]);
}
}
else
{
child = fork();
if((child=fork())==-1)
{
printf("Fail to creat child process.\n");
}
if(child==0)
{
close(0);
dup(pipeID[0]);
close(pipeID[1]);
close(pipeID[0]);
if(execve(result, secondcommand, pathv)==-1)
{
printf("Child process can't exec command %s.\n",secondcommand[0]);
}
}
else
{
wait(NULL);
}
}
}
int main()
{
char commandLine[LINE_LEN];
int child_pid; //child process id
int stat; //used by parent wait
pid_t thisChPID;
char *arg[MAX_ARGS];
//the flag of redirection, piping and background running
int redirectionsituation = 0;
int pipesituation = 0;
int background = 0;
char * tempchar;
//Command initialization
int i;
for(i = 0; i < MAX_ARGS; i++ )
{
command.argv[i] = (char *) malloc(MAX_ARG_LEN);
}
//get all directories from PATH env var
enviromentlength = parsePath(pathv);
//Main loop
while(TRUE)
{
redirectionsituation = 0;
pipesituation = 0;
background = 0;
//Read the command line
printPrompt();
readCommand(commandLine);
//input nothing
if(commandLine[0] == '\0')
{
continue;
}
//quit the shell?
if((strcmp(commandLine, "exit") == 0) || (strcmp(commandLine, "quit") == 0))
{
break;
}
//if it is background running
if(commandLine[strlen(commandLine) - 1] == '&')
{
printf("backgrond\n");
tempchar = strtok (commandLine, "&");
//strcpy(commandLine, tempchar);
printf("%s\n", tempchar);
background = 1;
}
//Parse the command line
commandlength = parseCommand(commandLine);
//if the command is "cd"
if(strcmp(command.argv[0], "cd") == 0)
{
do_cd(command.argv);
continue;
}
//Get the full path name
command.name = lookupPath(pathv, command.argv);
printf("command name %s\n", command.name);
//report error
if( command.name == NULL)
{
continue; //non-fatal
}
//if redirection is required
for(i = 0; i < commandlength; i++)
{
if(strcmp(command.argv[i], ">") == 0)
{
redirectionsituation = 1;
break;
}
}
if(redirectionsituation == 1)
{
redirection(command.name, command.argv, i, thisChPID);
continue;
}
//if pipe is required
for(i = 0; i < commandlength; i++)
{
if(strcmp(command.argv[i], "|") == 0)
{
pipesituation = 1;
break;
}
}
if(pipesituation == 1)
{ //run pipe
piperun(command.name, command.argv, i, thisChPID);
continue;
}
//normal running
if((thisChPID=fork()) < 0)
{
fprintf(stderr, "fork failed\n");
}
else if(thisChPID == 0)
{
//printf("run again\n");
execve(command.name, command.argv, pathv);
}
else
{
//do not put the process in the background, wait until the child process terminates
if(background == 0)
{
wait(&stat);
}
}
}
return 0;
}
Run it in a debugger and see where you are dereferencing a null.

File compression using bzip2 library doesn't work

I just wrote a C function that compress a file using bzip2 library APIs. The compression is not working fine. When I decompress the output file using an archiving utility, I'm getting some garbage value along with the actual data. I have done everything as per the instructions in the bzip2 library manual. Can someone tell me what went wrong?
The code may not be well structured. This is just an attempt to understand the usage of libzip2 library.
#define BUFSIZE 512
int main(int argc, char *argv[])
{
char file_name[64];
if(argc != 2)
{
printf("usage: compr <file name>");
return -1;
}
strncpy(file_name,argv[1],64);
return file_compress(file_name);
}
int file_compress(char * arg)
{
int input_fd, output_fd;
ssize_t ret_in ;
char buffer[BUFSIZE];
char buffout[BUFSIZE];
struct stat fileStat;
int insize;
int st;
bz_stream strm;
strm.bzalloc= NULL;
strm.bzfree= NULL;
strm.opaque= NULL;
st = BZ2_bzCompressInit (&strm,1,0,30 );
if(st != BZ_OK)
{
return 1;
}
else
{
input_fd = open (arg, O_RDONLY);
if (input_fd < 0)
{
return 1;
}
if(fstat(input_fd,&fileStat) < 0)
return 1;
insize = fileStat.st_size;
printf("File Size: \t%d bytes\n",insize);
strcat(arg,".bz2");
output_fd = open(arg, O_WRONLY | O_CREAT, 0644);
if(output_fd == -1)
{
return 1;
}
strm.avail_in = 0;
while(1)
{
if(insize > 0 && strm.avail_in == 0)
ret_in = read (input_fd, buffer, BUFSIZE);
else
ret_in = 0;
strm.next_in=buffer;
strm.avail_in=ret_in;
strm.next_out=buffout;
strm.avail_out=BUFSIZE;
if(insize == 0)
{
st= BZ2_bzCompress ( &strm,BZ_FINISH);
}
else if(insize <= BUFSIZE)
{
st= BZ2_bzCompress ( &strm,BZ_FINISH);
}
else
{
st=BZ2_bzCompress ( &strm,BZ_RUN);
}
insize -= ret_in;
if(BUFSIZE - strm.avail_out > 0)
{
int ret_out=write (output_fd, buffout, BUFSIZE - strm.avail_out);
printf("retout%d \n",ret_out);
}
if(st == BZ_STREAM_END)
break;
if(st < 0 )
{
return 1;
}
}
close (input_fd);
close (output_fd);
}
st = BZ2_bzCompressEnd (&strm );
if(st != BZ_OK)
{
// perror("ERROR BZ2_bzCompressEnd\n");
return 1;
}
return 0;
}

Resources