~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/process.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    process.cc - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
14
    MediaTomb is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License version 2
 
16
    as published by the Free Software Foundation.
 
17
    
 
18
    MediaTomb is distributed in the hope that it will be useful,
 
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
    GNU General Public License for more details.
 
22
    
 
23
    You should have received a copy of the GNU General Public License
 
24
    version 2 along with MediaTomb; if not, write to the Free Software
 
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
    
 
27
    $Id: process.cc 1294 2007-05-13 16:28:24Z lww $
 
28
*/
 
29
 
 
30
/// \file process.cc
 
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
    #include "autoconfig.h"
 
34
#endif
 
35
 
 
36
#include "process.h"
 
37
 
 
38
#include <stdio.h>
 
39
#include <unistd.h>
 
40
#include <sys/types.h>
 
41
 
 
42
#include <string.h>
 
43
#include <errno.h>
 
44
 
 
45
using namespace zmm;
 
46
 
 
47
#define BUF_SIZE 256
 
48
#define MAX_TEMPLATE 64
 
49
 
 
50
char input_template[MAX_TEMPLATE];
 
51
char output_template[MAX_TEMPLATE];
 
52
 
 
53
void init_process()
 
54
{
 
55
    sprintf(input_template, "/tmp/mediaserver_in_%d_XXXXXX", getuid());
 
56
    sprintf(output_template, "/tmp/mediaserver_out_%d_XXXXXX", getuid());
 
57
}
 
58
 
 
59
String run_process(String prog, String param, String input)
 
60
{
 
61
    FILE *file;
 
62
    int fd;
 
63
 
 
64
    /* creating input file */
 
65
    char input_file[MAX_TEMPLATE];
 
66
    strcpy(input_file, input_template);
 
67
    fd = mkstemp(input_file);
 
68
    int ret = write(fd, input.c_str(), input.length());
 
69
    close(fd);
 
70
    
 
71
    /* touching output file */
 
72
    char output_file[MAX_TEMPLATE];
 
73
    strcpy(output_file, output_template);
 
74
    fd = mkstemp(output_file);
 
75
    close(fd);
 
76
   
 
77
    /* executing script */
 
78
    String command = prog + " " + param + " < " + input_file +
 
79
        " > " + output_file;
 
80
    log_debug("running %s\n", command.c_str());
 
81
    ret = system(command.c_str());
 
82
 
 
83
    /* reading output file */
 
84
    file = fopen(output_file, "r");
 
85
    Ref<StringBuffer> output(new StringBuffer());
 
86
 
 
87
    int bytesRead;
 
88
    char buf[BUF_SIZE];
 
89
    while (1)
 
90
    {
 
91
        bytesRead = fread(buf, 1, BUF_SIZE, file);
 
92
        if(bytesRead > 0)
 
93
            *output << String(buf, bytesRead);
 
94
        else
 
95
            break;
 
96
    }
 
97
    fclose(file);
 
98
 
 
99
    /* removing input and output files */
 
100
    unlink(input_file);
 
101
    unlink(output_file);
 
102
 
 
103
    return output->toString();
 
104
}