Merge mog's ReadFile application (bug #3670)

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@5099 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Mark Spencer
2005-02-27 19:07:46 +00:00
parent 78b9e92af9
commit 85a0e13c73
5 changed files with 155 additions and 1 deletions

34
app.c
View File

@@ -1393,3 +1393,37 @@ int ast_ivr_menu_run(struct ast_channel *chan, struct ast_ivr_menu *menu, void *
res = 0;
return res;
}
char *ast_read_textfile(const char *filename)
{
int fd;
char *output=NULL;
struct stat filesize;
int count=0;
int res;
if(stat(filename,&filesize)== -1){
ast_log(LOG_WARNING,"Error can't stat %s\n", filename);
return NULL;
}
count=filesize.st_size + 1;
fd = open(filename, O_RDONLY);
if (fd < 0) {
ast_log(LOG_WARNING, "Cannot open file '%s' for reading: %s\n", filename, strerror(errno));
return NULL;
}
output=(char *)malloc(count);
if (output) {
res = read(fd, output, count - 1);
if (res == count - 1) {
output[res] = '\0';
} else {
ast_log(LOG_WARNING, "Short read of %s (%d of %d): %s\n", filename, res, count - 1, strerror(errno));
free(output);
output = NULL;
}
} else
ast_log(LOG_WARNING, "Out of memory!\n");
close(fd);
return output;
}