sfat_append.c
Vai alla documentazione di questo file.00001
00013 #include "Include/common.h"
00014
00016 static void byebye();
00017
00018 message_t msg;
00019 channel_t sk_client;
00020 char *path;
00031 int main(int argc, char **argv){
00032 extern message_t msg;
00033 extern channel_t sk_client;
00034 extern char * path;
00035 char * buffer;
00036 int sent =0;
00037
00038 sk_client = 0;
00039 path = NULL;
00040 msg.buffer = NULL;
00041 if ((atexit(byebye))!=0){
00042 errore(__FILE__,__LINE__,"sfat_append: cannot set exit function",errno);
00043 exit(EXIT_FAILURE);
00044 }
00045
00046
00047 if (argc<2){
00048 fprintf(stderr,
00049 "sfat_append: errore non e' stato passato il nome del file\n");
00050 exit(EXIT_FAILURE);
00051 }else if (argc<3){
00052 fprintf(stderr,
00053 "sfat_append: errore non e' stato passato il testo da aggiungere al file\n");
00054 exit(EXIT_FAILURE);
00055 }
00056
00057 if (!(path = calloc((strlen(TMP) + strlen(SKTNAME) + 1), sizeof(char)))){
00058 errore(__FILE__,__LINE__,
00059 "sfat_append: error on allocate memory for 'path'",errno);
00060 exit(EXIT_FAILURE);
00061 }
00062 strncpy(path, TMP, strlen(TMP)+1);
00063 strncat(path, SKTNAME, strlen(SKTNAME));
00064
00065
00066 if ((sk_client = openConnection(path)) == -1) {
00067 fprintf(stderr,"sfat_append: Errore nella apertura del socket di comunicazione\n");
00068 exit(EXIT_FAILURE);
00069 }
00070 else if (sk_client == SFATENAMETOOLONG){
00071 fprintf(stderr,"sfat_append: Error Path Too Long (exceeding UNIX_PATH_MAX)\n");
00072 exit(EXIT_FAILURE);
00073 }
00074
00075
00076 msg.type = MSG_FWRITE;
00077 msg.length = strlen(argv[1]) + strlen(argv[2]) + 2;
00078 if (!(msg.buffer = calloc(msg.length, sizeof(char)))){
00079 errore(__FILE__,__LINE__,
00080 "sfat_append: error on allocate memory for 'buffer'",errno);
00081 exit(EXIT_FAILURE);
00082 }
00083
00084 strncpy(msg.buffer, argv[1], strlen(argv[1]));
00085 buffer = msg.buffer + strlen(argv[1]) + 1;
00086 strncpy(buffer,argv[2],strlen(argv[2]));
00087
00088 if ((sent = sendMessage(sk_client, &msg)) == -1) exit(EXIT_FAILURE);
00089 if (receiveMessage(sk_client, &msg) == -1) exit(EXIT_FAILURE);
00090 if (msg.type==MSG_OK)
00091 printf("sfat_append: appended \"%s\"\n", argv[2]);
00092 else exit(EXIT_FAILURE);
00093
00094 exit(EXIT_SUCCESS);
00095 return 0;
00096 }
00097
00098 void byebye(){
00099 extern message_t msg;
00100 extern char * path;
00101 extern channel_t sk_client;
00102
00103
00104 if (sk_client != -1) closeConnection(sk_client);
00105
00106 if (msg.buffer!=NULL) free(msg.buffer);
00107 if (path!=NULL) free(path);
00108 }