#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 128 del file sfat_read.c.
00128 { 00129 extern message_t msg; 00130 extern char * path; 00131 extern channel_t sk_client; 00132 00133 /* Chiudo la socket di comunicazione del client */ 00134 if (sk_client != -1) closeConnection(sk_client); 00135 00136 if (msg.buffer!=NULL) free(msg.buffer); 00137 if (path!=NULL) free(path); 00138 }
| 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 34 del file sfat_read.c.
00034 { 00035 extern message_t msg; 00036 extern channel_t sk_client; 00037 extern char * path; 00038 char *endptr, *buffer; 00039 int offset=0, len=0; 00040 00041 sk_client = 0; 00042 path = NULL; 00043 msg.buffer = NULL; 00044 00045 if ((atexit(byebye))!=0){ 00046 errore(__FILE__,__LINE__,"sfat_read: cannot set exit function",errno); 00047 exit(EXIT_FAILURE); 00048 } 00049 00050 /* Argomenti da riga di comando */ 00051 if (argc<4){ 00052 fprintf(stderr,"sfat_read: error too few arguments\n"); 00053 exit(EXIT_FAILURE); 00054 } 00055 /* OFFSET */ 00056 offset = strtol(argv[2], &endptr, 10); 00057 if (errno!=0){ 00058 errore(__FILE__,__LINE__,"sfat_read: No digits were found (offset)",errno); 00059 exit(EXIT_FAILURE); 00060 } 00061 if (endptr == argv[2]){ 00062 fprintf(stderr, "sfat_read: No digits were found (offset)\n"); 00063 exit(EXIT_FAILURE); 00064 } 00065 if (offset < 0){ 00066 fprintf(stderr, "sfat_read: error negative offset number\n"); 00067 exit(EXIT_FAILURE); 00068 } 00069 /* LEN */ 00070 len = strtol(argv[3], &endptr, 10); 00071 if (errno!=0){ 00072 errore(__FILE__,__LINE__,"sfat_read: No digits were found (len)",errno); 00073 exit(EXIT_FAILURE); 00074 } 00075 if (endptr == argv[3]){ 00076 fprintf(stderr, "sfat_read: No digits were found (len)\n"); 00077 exit(EXIT_FAILURE); 00078 } 00079 if (len <= 0){ 00080 fprintf(stderr, "sfat_read: error in len number\n"); 00081 exit(EXIT_FAILURE); 00082 } 00083 00084 /* Path del socket */ 00085 if (!(path = calloc((strlen(TMP) + strlen(SKTNAME) + 1), sizeof(char)))){ 00086 errore(__FILE__,__LINE__, 00087 "sfat_read: error on allocate memory for 'path'",errno); 00088 exit(EXIT_FAILURE); 00089 } 00090 strncpy(path, TMP, strlen(TMP)+1); 00091 strncat(path, SKTNAME, strlen(SKTNAME)); 00092 00093 /* Preparo il messaggio da inviare al server */ 00094 msg.type = MSG_FREAD; 00095 msg.length = sizeof(int)*2 + strlen(argv[1]) + 3; 00096 if (!(msg.buffer = malloc(msg.length))){ 00097 errore(__FILE__,__LINE__, 00098 "sfat_read: error on allocate memory for 'buffer'",errno); 00099 exit(EXIT_FAILURE); 00100 } 00101 memset(msg.buffer, 0x0, msg.length); 00102 /* Struttura del messaggio: Offset\0Path\0Len\0 */ 00103 memcpy(msg.buffer, &offset, sizeof(int)); 00104 buffer = msg.buffer + sizeof(int) + 1; 00105 strncpy(buffer,argv[1],strlen(argv[1]) + 1); 00106 buffer += strlen(argv[1]) + 1; 00107 memcpy(buffer, &len, sizeof(int)); 00108 00109 /* Apro la socket di comunicazione col server */ 00110 if ((sk_client = openConnection(path)) == -1) { 00111 fprintf(stderr,"sfat_read: Errore nella apertura del socket di comunicazione\n"); 00112 exit(EXIT_FAILURE); 00113 } 00114 else if (sk_client == SFATENAMETOOLONG){ 00115 fprintf(stderr,"sfat_read: Error Path Too Long (exceeding UNIX_PATH_MAX)\n"); 00116 exit(EXIT_FAILURE); 00117 } 00118 if (sendMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00119 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE); 00120 if (msg.type==MSG_OK && msg.length!=0) 00121 printf("sfat_read: read: %s \"%s\"\n", argv[1], msg.buffer); 00122 else exit(EXIT_FAILURE); 00123 00124 exit(EXIT_SUCCESS); 00125 return 0; 00126 }
struttura rappresentante un messaggio tra client e server
Definizione alla linea 20 del file sfat_read.c.
tipo descrittore del canale di comunicazione (server e client)
Definizione alla linea 21 del file sfat_read.c.
| char* path |
directory e nome del socket AF_UNIX
Definizione alla linea 22 del file sfat_read.c.
1.6.3