00001
00013 #include <sys/stat.h>
00014 #include "Include/common.h"
00015
00017 static void byebye();
00018
00019 message_t msg;
00020 channel_t sk_client;
00021 char *path;
00033 int main(int argc, char **argv){
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
00050 if (argc<4){
00051 fprintf(stderr,"sfat_read: error too few arguments\n");
00052 exit(EXIT_FAILURE);
00053 }
00054
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
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
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
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
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
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 }
00126
00127 void byebye(){
00128 extern message_t msg;
00129 extern char * path;
00130 extern channel_t sk_client;
00131
00132
00133 if (sk_client != -1) closeConnection(sk_client);
00134
00135 if (msg.buffer!=NULL) free(msg.buffer);
00136 if (path!=NULL) free(path);
00137 }