mirror of
https://github.com/asterisk/asterisk.git
synced 2025-11-19 00:00:09 +00:00
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:
34
app.c
34
app.c
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user