File: utils.c

package info (click to toggle)
freesweep 0.88-4.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 404 kB
  • ctags: 267
  • sloc: ansic: 3,813; sh: 2,460; makefile: 112
file content (97 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**********************************************************************
*  This source code is copyright 1999 by Gus Hartmann & Peter Keller  *
*  It may be distributed under the terms of the GNU General Purpose   *
*  License, version 2 or above; see the file COPYING for more         *
*  information.                                                       *
*                                                                     *
*  $Id: utils.c,v 1.6 1999/08/09 05:25:36 hartmann Exp $
*                                                                     *
**********************************************************************/

#include "sweep.h"

void* xmalloc(size_t num)
{
	void *vec = NULL;

	vec = (void*)malloc(sizeof(unsigned char) * num);

	if (vec == NULL)
	{
		SweepError("Out of Memory. Sorry");
		exit(EXIT_FAILURE);
	}

	return vec;
}

FILE* xfopen(char *name, char *type)
{
	FILE *fp = NULL; 

	fp = fopen(name, type);
	if (fp == NULL)
	{
		SweepError("Could not open file: %s", name);
		exit(EXIT_FAILURE);
	}

	return fp;
}

#ifndef HAVE_STRDUP
char *strdup(char *s)
{
	char *c = NULL;

	c = (char*)xmalloc(strlen(s) + 1);
	
	strcpy(c, s);

	return(c);

}
#endif

char* xgetcwd(char *buf, size_t size)
{
	char *path = NULL;

	path = getcwd(buf, size);

	if (path == NULL)
	{
		SweepError("Could not get current working directory.");
		exit(EXIT_FAILURE);
	}

	return path;
}

DIR* xopendir(const char *path)
{
	DIR *dirent = NULL;

	dirent = opendir(path);

	if (dirent == NULL)
	{
		SweepError("Could not open directory: %s", path);
		exit(EXIT_FAILURE);
	}

	return dirent;
}

/* start and stop the timer */
void StartTimer(void)
{
	signal(SIGALRM, sighandler);
	alarm(1);
}

/* stop the timer */
void StopTimer(void)
{
	signal(SIGALRM, SIG_IGN);
}