Merge pull request #2 from MFreeze/master
Remove warnings & Address memory leaks
This commit is contained in:
commit
23e04e4810
1 changed files with 43 additions and 2 deletions
45
stt.c
45
stt.c
|
|
@ -1,3 +1,7 @@
|
||||||
|
#ifndef _DEFAULT_SOURCE
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
@ -13,6 +17,10 @@
|
||||||
#define VERSION "dev"
|
#define VERSION "dev"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define free_and_null(x) do { \
|
||||||
|
if (x) { free (x); x = NULL; } \
|
||||||
|
} while (0);
|
||||||
|
|
||||||
char *argv0;
|
char *argv0;
|
||||||
|
|
||||||
static char *timesfile = ".ttimes";
|
static char *timesfile = ".ttimes";
|
||||||
|
|
@ -41,6 +49,7 @@ int timesnode_getbalance(struct timesnode *);
|
||||||
struct timesnode *timesnode_rightrotate(struct timesnode *);
|
struct timesnode *timesnode_rightrotate(struct timesnode *);
|
||||||
struct timesnode *timesnode_leftrotate(struct timesnode *);
|
struct timesnode *timesnode_leftrotate(struct timesnode *);
|
||||||
void timesnode_print(struct timesnode *, time_t);
|
void timesnode_print(struct timesnode *, time_t);
|
||||||
|
void timesnode_free(struct timesnode *);
|
||||||
|
|
||||||
void
|
void
|
||||||
usage()
|
usage()
|
||||||
|
|
@ -82,7 +91,7 @@ opentimesfile()
|
||||||
struct timesnode *
|
struct timesnode *
|
||||||
loadtimes(FILE * fp, struct timesnode * p)
|
loadtimes(FILE * fp, struct timesnode * p)
|
||||||
{
|
{
|
||||||
char *readline, *line, *tmp;
|
char *readline, *line, *tmp, *tmpline;
|
||||||
time_t starttime, endtime;
|
time_t starttime, endtime;
|
||||||
char *task;
|
char *task;
|
||||||
size_t linesize;
|
size_t linesize;
|
||||||
|
|
@ -94,12 +103,15 @@ loadtimes(FILE * fp, struct timesnode * p)
|
||||||
linesize = 0;
|
linesize = 0;
|
||||||
while ((linelen = getline(&readline, &linesize, fp)) != -1) {
|
while ((linelen = getline(&readline, &linesize, fp)) != -1) {
|
||||||
line = strdup(readline);
|
line = strdup(readline);
|
||||||
|
tmpline = line; /* Stores the pointer to allow buffer free */
|
||||||
tmp = strsep(&line, ";");
|
tmp = strsep(&line, ";");
|
||||||
starttime = atoi(tmp);
|
starttime = atoi(tmp);
|
||||||
|
|
||||||
if (starttime == 0) {
|
if (starttime == 0) {
|
||||||
|
free_and_null (tmpline);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp = strsep(&line, ";");
|
tmp = strsep(&line, ";");
|
||||||
endtime = atoi(tmp);
|
endtime = atoi(tmp);
|
||||||
|
|
||||||
|
|
@ -107,8 +119,13 @@ loadtimes(FILE * fp, struct timesnode * p)
|
||||||
task[strlen(task) - 1] = '\0';
|
task[strlen(task) - 1] = '\0';
|
||||||
|
|
||||||
p = timesnode_add(p, task, starttime, endtime);
|
p = timesnode_add(p, task, starttime, endtime);
|
||||||
|
|
||||||
|
free_and_null (task);
|
||||||
|
free_and_null (tmpline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free_and_null (readline);
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,7 +138,7 @@ writetimes(FILE * fp, struct timesnode * p)
|
||||||
if (p->left != NULL) {
|
if (p->left != NULL) {
|
||||||
writetimes(fp, p->left);
|
writetimes(fp, p->left);
|
||||||
}
|
}
|
||||||
fprintf(fp, "%lld;%lld;%s\n", p->starttime, p->endtime, p->task);
|
fprintf(fp, "%ld;%ld;%s\n", p->starttime, p->endtime, p->task);
|
||||||
|
|
||||||
if (p->right != NULL) {
|
if (p->right != NULL) {
|
||||||
writetimes(fp, p->right);
|
writetimes(fp, p->right);
|
||||||
|
|
@ -287,6 +304,23 @@ timesnode_print(struct timesnode * p, time_t aftertime)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
timesnode_free (struct timesnode *p)
|
||||||
|
{
|
||||||
|
if (p->left) {
|
||||||
|
timesnode_free (p->left);
|
||||||
|
p->left = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (p->right) {
|
||||||
|
timesnode_free (p->right);
|
||||||
|
p->right = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
free_and_null (p->task);
|
||||||
|
free_and_null (p);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
|
|
@ -358,5 +392,12 @@ default:
|
||||||
|
|
||||||
timesnode_print(timestree, mktime(&today));
|
timesnode_print(timestree, mktime(&today));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fclose (fp);
|
||||||
|
|
||||||
|
timesnode_free (timestree);
|
||||||
|
|
||||||
|
free_and_null (nowtime);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue