created read_socket function
This commit is contained in:
parent
e859087763
commit
edbe534a1f
@ -19,6 +19,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
#include <why2/chat/common.h>
|
#include <why2/chat/common.h>
|
||||||
|
|
||||||
void die(char *exit_message);
|
void die(char *exit_message);
|
||||||
|
char *read_socket(int socket);
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
@ -60,3 +61,25 @@ void die(char *exit_msg)
|
|||||||
|
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *read_socket(int socket)
|
||||||
|
{
|
||||||
|
FILE *opened = why2_fdopen(socket, "r"); //OPEN socket
|
||||||
|
long content_size;
|
||||||
|
char *content;
|
||||||
|
|
||||||
|
//COUNT content_size
|
||||||
|
fseek(opened, 0, SEEK_END);
|
||||||
|
content_size = ftell(opened);
|
||||||
|
rewind(opened); //REWIND
|
||||||
|
|
||||||
|
//ALLOCATE
|
||||||
|
content = why2_calloc(content_size, sizeof(char));
|
||||||
|
|
||||||
|
if (fread(content, content_size, 1, opened) != 1) die("Reading socket failed!");
|
||||||
|
|
||||||
|
//DEALLOCATION
|
||||||
|
why2_deallocate(opened);
|
||||||
|
|
||||||
|
return content;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user