1 /* bHDstruct.c 2 * 3 * Header structure 4 * 5 * ------- ------- --- ------------ -------- ------------ -------- -------- 6 * |hdr len|cmd len|cmd|srcfname len|srcfname|trgfname len|trgfname|file len| 7 * ------- ------- --- ------------ -------- ------------ -------- -------- 8 */ 9 #include 10 #include 11 #include 12 #include 13 #include 14 /* The following function is to build the header structure for file transfer */ 15 bHDstruct(char *HDbuf, int *buflen) 16 { 17 int i, len; 18 ssize_t nread; 19 char rbuf[BUFSIZ]; /* buf for KB input */ 20 char *cptr; /* a char pointer */ 21 char *pHDbuf = HDbuf; /* a ptr for HDbuf */ 22 char PROMPT[] = "Please enter command >> "; 23 char PROMPT1[] = "Please enter source filename >> "; 24 char PROMPT2[] = "Please enter target filename >> "; 25 write(STDOUT_FILENO, PROMPT, strlen(PROMPT)); /* write to stdout */ 26 nread = read(STDIN_FILENO, rbuf, BUFSIZ); /* read from stdin */ 27 nread--; /* to strip off '\n' */ 28 len = (int)nread; /* cast it to an int */ 29 30 pHDbuf += sizeof(int); /* leave room for hdr len */ 31 cptr = (char *)&len; /* cast to char */ 32 for (i = 0; i < sizeof(int); i++) 33 *pHDbuf++ = *cptr++; /* store cmd len in HDbuf */ 34 bcopy(rbuf, pHDbuf, len); /* store cmd, no NULL */ 35 pHDbuf += len; /* point to next empty slot*/ 36 37 write(STDOUT_FILENO, PROMPT1, strlen(PROMPT1)); 38 nread = read(STDIN_FILENO, rbuf, BUFSIZ); 39 nread--; /* to strip off '\n' */ 40 len = (int)nread; /* cast it to an int */ 41 42 cptr = (char *)&len; /* cast to char */ 43 for (i = 0; i < sizeof(int); i++) 44 *pHDbuf++ = *cptr++; /* store srcfname len in HDbuf */ 45 bcopy(rbuf, pHDbuf, len); /* store srcfname, no NULL */ 46 pHDbuf += len; /* point to next empty slot*/ 47 48 write(STDOUT_FILENO, PROMPT2, strlen(PROMPT2)); 49 nread = read(STDIN_FILENO, rbuf, BUFSIZ); 50 nread--; /* to strip off '\n' */ 51 len = (int)nread; /* cast it to an int */ 52 53 cptr = (char *)&len; /* cast to char */ 54 for (i = 0; i < sizeof(int); i++) 55 *pHDbuf++ = *cptr++; /* store trgfname len in HDbuf */ 56 bcopy(rbuf, pHDbuf, len); /* store trgfname, no NULL */ 57 pHDbuf += len; /* point to next empty slot*/ 58 59 len = pHDbuf - HDbuf; /* headr len, no file len yet */ 60 *buflen = len; /* value passed out to caller */ 61 pHDbuf = HDbuf; /* reset ptr to beginning */ 62 cptr = (char *)&len; /* cast to char */ 63 for (i = 0; i < sizeof(int); i++) 64 *pHDbuf++ = *cptr++; /* store header len in HDbuf */ 65 return; 66 } /* bHDstruct */ 67 /* The following shows an example of how to extract infor from the header structure. 68 */ 69 main() 70 { 71 int len, buflen = BUFSIZ * 2; /* a bigger buffer len */ 72 char wbuf[BUFSIZ], hdbuf[BUFSIZ * 2]; 73 char *cptr = hdbuf, *sptr; 74 bHDstruct(hdbuf, &buflen); /* assume buffer is big enough */ 75 bcopy(cptr, (char *)&len, sizeof(int)); 76 cptr += sizeof(int); 77 printf("header length = %d\n", len); 78 bcopy(cptr, (char *)&len, sizeof(int)); 79 cptr += sizeof(int); 80 printf("cmd length = %d\n", len); 81 bcopy(cptr, wbuf, len); 82 cptr += len; 83 wbuf[len] = '\0'; 84 printf("Command name = %s\n", wbuf); 85 bcopy(cptr, (char *)&len, sizeof(int)); 86 cptr += sizeof(int); 87 printf("source filename length = %d\n", len); 88 bcopy(cptr, wbuf, len); 89 cptr += len; 90 wbuf[len] = '\0'; 91 printf("Source filename = %s\n", wbuf); 92 bcopy(cptr, (char *)&len, sizeof(int)); 93 cptr += sizeof(int); 94 printf("target filename length = %d\n", len); 95 bcopy(cptr, wbuf, len); 96 cptr += len; 97 wbuf[len] = '\0'; 98 printf("Target filename = %s\n", wbuf); 99 /* may need to continue for file length */ 100 } /* main */ [pluto:~] bHDstruct Please enter command >> recv Please enter source filename >> sourceFile Please enter target filename >> targetFilename header length = 44 cmd length = 4 Command name = recv source filename length = 10 Source filename = sourceFile target filename length = 14 Target filename = targetFilename [pluto:~]