148 lines
3.7 KiB
C
148 lines
3.7 KiB
C
#include "utility.h"
|
|
|
|
void splitFtpRequest(char* pRequest, char* pCommand, char* pContent){
|
|
/* [1] Vérification du format
|
|
===========================================*/
|
|
int firstSpaceIndex = indexOf(pRequest, ' ');
|
|
|
|
if( firstSpaceIndex != 3 && firstSpaceIndex != 4){ // contient aucun espace en position 3 ou 4, on quitte
|
|
strcpy(pCommand, "ERROR");
|
|
strcpy(pContent, "");
|
|
return;
|
|
}
|
|
|
|
|
|
/* [2] Séparation des 2 parties
|
|
===========================================*/
|
|
int i;
|
|
|
|
for( i = 0 ; i < strlen(pRequest) || pRequest[i] != '\0' ; i++ ){
|
|
|
|
if( i < firstSpaceIndex ) // première partie (pCommand)
|
|
strcpy( pCommand, strcat(pCommand, (char[2]) { (char) pRequest[i], '\0' }) );
|
|
if( i > firstSpaceIndex ) // seconde partie (pContent)
|
|
strcpy( pContent, strcat(pContent, (char[2]) { (char) pRequest[i], '\0' }) );
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void splitFtpResponse(char* pAnswer, char* ftpCode, char* ftpText){
|
|
/* [1] Vérification du format
|
|
===========================================*/
|
|
int codeLength = 3; // taille du code
|
|
|
|
/* [2] Séparation des 2 parties
|
|
===========================================*/
|
|
int i;
|
|
|
|
for( i = 0 ; i < strlen(pAnswer) || pAnswer[i] != '\0' ; i++ ){
|
|
|
|
if( i < codeLength ) // première partie (ftpCode)
|
|
strcpy( ftpCode, strcat(ftpCode, (char[2]) { (char) pAnswer[i], '\0' }) );
|
|
if( i > codeLength ) // seconde partie (ftpText)
|
|
strcpy( ftpText, strcat(ftpText, (char[2]) { (char) pAnswer[i], '\0' }) );
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int indexOf(char* haystack, char needle){
|
|
int i;
|
|
|
|
for( i = 0 ; i < strlen(haystack) || haystack[i] != '\0' ; i++ )
|
|
if( haystack[i] == needle ) // si on trouve le caractère
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
void formatBuffer(char* pBuffer){
|
|
if( DEBUGMOD ) printf( "BUFLEN (bef): %lu\n", strlen(pBuffer) );
|
|
if( DEBUGMOD ) printf( "BUFFER: (%s)\n", pBuffer );
|
|
|
|
/* [1] On retire les "\n" et "\r" de la fin de la chaine
|
|
============================================================*/
|
|
int i = strlen(pBuffer);
|
|
|
|
while( pBuffer[strlen(pBuffer)-1] == '\r' || pBuffer[strlen(pBuffer)-1] == '\n' )
|
|
pBuffer[strlen(pBuffer)-1] = '\0';
|
|
|
|
/* [2] On ajoute "\r\n" à la fin
|
|
============================================================*/
|
|
strcpy(pBuffer, strcat(pBuffer, "\r\n"));
|
|
|
|
if( DEBUGMOD ) printf( "BUFLEN (aft): %lu\n", strlen(pBuffer) );
|
|
}
|
|
|
|
|
|
|
|
void read_stdin(char* pBuffer, unsigned long pLength){
|
|
fgets(pBuffer, pLength, stdin); // on lit l'entrée standard
|
|
|
|
// on supprimes les retours à la ligne de la fin
|
|
int i = strlen(pBuffer);
|
|
|
|
while( pBuffer[strlen(pBuffer)-1] == '\r' || pBuffer[strlen(pBuffer)-1] == '\n' )
|
|
pBuffer[strlen(pBuffer)-1] = '\0';
|
|
|
|
strcpy(pBuffer, strcat(pBuffer, "\r\n"));
|
|
}
|
|
|
|
|
|
int swrite(int* pSocket, char* pBuffer){
|
|
if( *pSocket == -1 ) return -1; // si SOCKET fermée, on retourne une erreur
|
|
|
|
|
|
if( strlen(pBuffer) == 0 ) return 0;
|
|
// on formatte @pBuffer
|
|
// formatBuffer(pBuffer);
|
|
|
|
return write(*pSocket, pBuffer, strlen(pBuffer));
|
|
}
|
|
|
|
|
|
int sread(int* pSocket, char* pBuffer){
|
|
if( *pSocket == -1 ) return -1; // si SOCKET fermée, on retourne une erreur
|
|
|
|
|
|
// on vide le buffer avant de lire
|
|
memset(pBuffer, '\0', maxBuffLen);
|
|
int nbRead = read(*pSocket, pBuffer, maxBuffLen);
|
|
|
|
|
|
// si on est déconnecté, on ferme la SOCKET
|
|
if( nbRead == 0 ){
|
|
// close(*pSocket);
|
|
return -1; // on retourne une erreur
|
|
}
|
|
|
|
return nbRead;
|
|
}
|
|
|
|
|
|
|
|
void xPrint(char* pPattern, char* pBuffer){
|
|
char tmpBuffer[maxBuffLen];
|
|
strcpy(tmpBuffer, pBuffer);
|
|
|
|
|
|
int i;
|
|
|
|
// on supprimes les retours à la ligne de la fin
|
|
for( i = strlen(tmpBuffer)-1 ; i >= 0 || tmpBuffer[i] != '\0' ; i-- )
|
|
if( tmpBuffer[i] == '\n' || tmpBuffer[i] == '\r' ) // si c'est un retour chariot
|
|
tmpBuffer[i] = '\0'; // on efface
|
|
else
|
|
break;
|
|
|
|
printf(pPattern, pBuffer);
|
|
} |