#include <sys/stat.h>
#include "Include/common.h"
Vai al codice sorgente di questo file.
Funzioni | |
static void | byebye () |
int | main (int argc, char **argv) |
Variabili | |
message_t | msg |
channel_t | sk_client |
char * | path |
This program is free software; you can redistribuite it and/or modify it under the terms of the GNU/General Pubblic License as published the Free software Foundation; either version 2 of the License, or (at your opinion) any later version.
Definizione nel file sfat_read.c.
void byebye | ( | ) | [static] |
exit function
Definizione alla linea 127 del file sfat_read.c.
00127 { 00128 extern message_t msg; 00129 extern char * path; 00130 extern channel_t sk_client; 00131 00132 /* Chiudo la socket di comunicazione del client */ 00133 if (sk_client != -1) closeConnection(sk_client); 00134 00135 if (msg.buffer!=NULL) free(msg.buffer); 00136 if (path!=NULL) free(path); 00137 }
int main | ( | int | argc, | |
char ** | argv | |||
) |
Comando: sfat_read path offset len legge al piu' len byte dal file a partire dall’offset.
All’invocazione il client controlla i parametri contatta il server inviando una richesta di connessione sul socket.
Se la connessione ha successo il client invia una richiesta di MSG_FREAD.
Tutti gli errori sono riportati sullo standar error.
Definizione alla linea 33 del file sfat_read.c.
00033 { 00034 extern message_t msg; 00035 extern channel_t sk_client; 00036 extern char * path; 00037 char *endptr, *buffer; 00038 int offset=0, len=0; 00039 00040 sk_client = 0; 00041 path = NULL; 00042 msg.buffer = NULL; 00043 00044 if ((atexit(byebye))!=0){ 00045 errore(__FILE__,__LINE__,"sfat_read: cannot set exit function",errno); 00046 exit(EXIT_FAILURE); 00047 } 00048 00049 /* Argomenti da riga di comando */ 00050 if (argc<4){ 00051 fprintf(stderr,"sfat_read: error too few arguments\n"); 00052 exit(EXIT_FAILURE); 00053 } 00054 /* OFFSET */ 00055 offset = strtol(argv[2], &endptr, 10); 00056 if (errno!=0){ 00057 errore(__FILE__,__LINE__,"sfat_read: No digits were found (offset)",errno); 00058 exit(EXIT_FAILURE); 00059 } 00060 if (endptr == argv[2]){ 00061 fprintf(stderr, "sfat_read: No digits were found (offset)\n"); 00062 exit(EXIT_FAILURE); 00063 } 00064 if (offset < 0){ 00065 fprintf(stderr, "sfat_read: error negative offset number\n"); 00066 exit(EXIT_FAILURE); 00067 } 00068 /* LEN */ 00069 len = strtol(argv[3], &endptr, 10); 00070 if (errno!=0){ 00071 errore(__FILE__,__LINE__,"sfat_read: No digits were found (len)",errno); 00072 exit(EXIT_FAILURE); 00073 } 00074 if (endptr == argv[3]){ 00075 fprintf(stderr, "sfat_read: No digits were found (len)\n"); 00076 exit(EXIT_FAILURE); 00077 } 00078 if (len <= 0){ 00079 fprintf(stderr, "sfat_read: error in len number\n"); 00080 exit(EXIT_FAILURE); 00081 } 00082 00083 /* Path del socket */ 00084 if (!(path = calloc((strlen(TMP) + strlen(SKTNAME) + 1), sizeof(char)))){ 00085 errore(__FILE__,__LINE__, 00086 "sfat_read: error on allocate memory for 'path'",errno); 00087 exit(EXIT_FAILURE); 00088 } 00089 strncpy(path, TMP, strlen(TMP)+1); 00090 strncat(path, SKTNAME, strlen(SKTNAME)); 00091 00092 /* Preparo il messaggio da inviare al server */ 00093 msg.type = MSG_FREAD; 00094 msg.length = sizeof(int)*2 + strlen(argv[1]) + 3; 00095 if (!(msg.buffer = malloc(msg.length))){ 00096 errore(__FILE__,__LINE__, 00097 "sfat_read: error on allocate memory for 'buffer'",errno); 00098 exit(EXIT_FAILURE); 00099 } 00100 memset(msg.buffer, 0x0, msg.length); 00101 /* Struttura del messaggio: Offset\0Path\0Len\0 */ 00102 memcpy(msg.buffer, &offset, sizeof(int)); 00103 buffer = msg.buffer + sizeof(int) + 1; 00104 strncpy(buffer,argv[1],strlen(argv[1]) + 1); 00105 buffer += strlen(argv[1]) + 1; 00106 memcpy(buffer, &len, sizeof(int)); 00107 00108 /* Apro la socket di comunicazione col server */ 00109 if ((sk_client = openConnection(path)) == -1) { 00110 fprintf(stderr,"sfat_read: Errore nella apertura del socket di comunicazione\n"); 00111 exit(EXIT_FAILURE); 00112 } 00113 else if (sk_client == SFATENAMETOOLONG){ 00114 fprintf(stderr,"sfat_read: Error Path Too Long (exceeding UNIX_PATH_MAX)\n"); 00115 exit(EXIT_FAILURE); 00116 } 00117 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00118 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00119 if (msg.type==MSG_OK && msg.length!=0) 00120 printf("sfat_read: read: %s \"%s\"\n", argv[1], msg.buffer); 00121 else exit(EXIT_FAILURE); 00122 00123 exit(EXIT_SUCCESS); 00124 return 0; 00125 }
struttura rappresentante un messaggio tra client e server
Definizione alla linea 19 del file sfat_read.c.
tipo descrittore del canale di comunicazione (server e client)
Definizione alla linea 20 del file sfat_read.c.
char* path |
directory e nome del socket AF_UNIX
Definizione alla linea 21 del file sfat_read.c.