~gnomefreak/firefox-extensions/firegpg.ubuntu

« back to all changes in this revision

Viewing changes to FireGPGCall/FireGPGCallOLD.cpp

  • Committer: John Vivirito
  • Date: 2008-08-12 11:47:33 UTC
  • Revision ID: gnomefreak@ubuntu.com-20080812114733-hn73tjxi26ylibrf
* import of upstream source version 0.5.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "FireGPGCall.h"
 
2
#include "nsMemory.h"
 
3
#include "nsCOMPtr.h"
 
4
#include "plstr.h"
 
5
#include <fcntl.h>
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <string.h>
 
9
#include <unistd.h>
 
10
#include <sys/socket.h>
 
11
#include <sys/wait.h>
 
12
 
 
13
char** split(char* chaine,const char* delim,int vide) {
 
14
 
 
15
    char** tab = NULL; //tableau de chaine, tableau resultat
 
16
    char *ptr; //pointeur sur une partie de
 
17
    int sizeStr; //taille de la chaine à recupérer
 
18
    int sizeTab = 0; //taille du tableau de chaine
 
19
    char* largestring; //chaine à traiter
 
20
 
 
21
    int sizeDelim=strlen(delim); //taille du delimiteur
 
22
 
 
23
    largestring = chaine; //comme ca on ne modifie pas le pointeur d'origine
 
24
 
 
25
        while( (ptr=strstr(largestring, delim))!=NULL ){
 
26
            sizeStr=ptr-largestring;
 
27
 
 
28
            //si la chaine trouvé n'est pas vide ou si on accepte les chaine vide
 
29
            if (vide == 1 || sizeStr != 0) {
 
30
                //on alloue une case en plus au tableau de chaines
 
31
                sizeTab++;
 
32
 
 
33
                tab= (char**) realloc(tab,sizeof(char*)*sizeTab);
 
34
 
 
35
                //on alloue la chaine du tableau
 
36
                tab[sizeTab-  1]=(char*) malloc( sizeof(char)*(sizeStr + 1) );
 
37
                strncpy(tab[sizeTab-1],largestring,sizeStr);
 
38
                tab[sizeTab-1][sizeStr] = '\0';
 
39
            }
 
40
 
 
41
            //on decale le pointeur largestring pour continuer la boucle apres le premier elément traiter
 
42
            ptr = ptr + sizeDelim;
 
43
            largestring = ptr;
 
44
        }
 
45
 
 
46
    //si la chaine n'est pas vide, on recupere le dernier "morceau"
 
47
    if (strlen(largestring) != 0) {
 
48
 
 
49
        sizeStr=strlen(largestring);
 
50
        sizeTab++;
 
51
        tab = (char**) realloc(tab,sizeof(char*)*sizeTab);
 
52
        tab[sizeTab-1]=(char*) malloc( sizeof(char)*(sizeStr+1) );
 
53
        strncpy(tab[sizeTab-1],largestring,sizeStr);
 
54
        tab[sizeTab-1][sizeStr] = '\0';
 
55
    }
 
56
    else if (vide == 1) { //si on fini sur un delimiteur et si on accepte les mots vides,on ajoute un mot vide
 
57
        sizeTab++;
 
58
        tab = (char**) realloc(tab,sizeof(char*)*sizeTab);
 
59
        tab[sizeTab-1] = (char*) malloc( sizeof(char)*1 );
 
60
        tab[sizeTab-1][0] = '\0';
 
61
 
 
62
    }
 
63
 
 
64
    //on ajoute une case à null pour finir le tableau
 
65
    sizeTab++;
 
66
    tab = (char**) realloc(tab,sizeof(char*)*sizeTab);
 
67
    tab[sizeTab-1] = NULL;
 
68
 
 
69
    return tab;
 
70
}
 
71
 
 
72
NS_IMPL_ISUPPORTS1(FireGPGCall, IFireGPGCall)
 
73
 
 
74
FireGPGCall::FireGPGCall() {
 
75
  /* member initializers and constructor code */
 
76
}
 
77
 
 
78
FireGPGCall::~FireGPGCall() {
 
79
  /* destructor code */
 
80
}
 
81
 
 
82
/* long Add (in long a, in long b); */
 
83
NS_IMETHODIMP FireGPGCall::Call(const char *path, const char *parameters, const char *sdin, char **_retval) {
 
84
 
 
85
    char c[1];  /* declare a char array */
 
86
    int n = 0;
 
87
 
 
88
    char buffer[10240];
 
89
 
 
90
    pid_t child;
 
91
    int fd[2];
 
92
    int rc;
 
93
 
 
94
    //We create a pair of socket (input and ouput)
 
95
    rc = socketpair( AF_UNIX, SOCK_STREAM, 0, fd );
 
96
    if ( rc < 0 ) {
 
97
        perror("Cannot open socketpair");
 
98
        exit(0);
 
99
    }
 
100
 
 
101
    //We create a child of ourself
 
102
    child = fork();
 
103
    if (child < 0) {
 
104
        perror("Cannot fork");
 
105
        exit(0);
 
106
    }
 
107
 
 
108
    if (child == 0) { /* child - it uses fd[1] */
 
109
        close (fd[0]);
 
110
        if (fd[1] != STDIN_FILENO) { /*Redirect standard input to socketpair*/
 
111
            if (dup2(fd[1], STDIN_FILENO) != STDIN_FILENO) {
 
112
                perror("Cannot dup2 stdin");
 
113
                exit(0);
 
114
            }
 
115
        }
 
116
 
 
117
        if (fd[1] != STDOUT_FILENO) { /*Redirect standard output to socketpair*/
 
118
            if (dup2(fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
 
119
                perror("Cannot dup2 stdout");
 
120
                exit(0);
 
121
            }
 
122
        }
 
123
 
 
124
        //We call the process
 
125
        char  *const *argv = split((char*)parameters," ",0);
 
126
 
 
127
        if (execvp(path, argv) < 0) {
 
128
            perror("Cannot exec");
 
129
            exit(0);
 
130
        }
 
131
 
 
132
        exit(0);
 
133
    }
 
134
 
 
135
    //We write data
 
136
    write(fd[0], sdin, strlen(sdin));
 
137
 
 
138
    //We wait for the end of the subprocess
 
139
    int status = 0;
 
140
    waitpid( child, &status, 0 );
 
141
 
 
142
    fcntl(fd[0], F_SETFL, O_NONBLOCK | fcntl(fd[0], F_GETFL));
 
143
 
 
144
    //We read byte by byte the output
 
145
    while(1) {
 
146
        read(fd[0],c,1);
 
147
 
 
148
        if(c[0]!=-74 && n < 10240) {
 
149
            buffer[n] = c[0];
 
150
            n++;
 
151
       }
 
152
        else {  //If it's the end of the buffer is full
 
153
            break;
 
154
       }
 
155
    }
 
156
 
 
157
    //End of the string
 
158
    buffer[n] = (char)'\0';
 
159
 
 
160
    //We copy the ouput to the return variable
 
161
    * _retval = (char*) nsMemory::Alloc(PL_strlen(buffer) + 1);
 
162
    if (! *_retval)
 
163
        return NS_ERROR_NULL_POINTER;
 
164
 
 
165
    PL_strcpy(*_retval, buffer);
 
166
 
 
167
    //We close sockets
 
168
    close(fd[0]);
 
169
    close(fd[1]);
 
170
 
 
171
        return NS_OK;
 
172
}