main() { FILE *temp; char buf[100]; if (NULL == (temp = mytmpfile())) err("%s: tmpfile failed", __FILE__); fputs("Some data written to a temporary file.\n", temp); rewind(temp); fgets(buf, 100, temp); fputs(buf, stdout); return 0; }
void run(int socket) { char input[1024]=""; char outputLine[4096]=""; char prompt[3] = "$>"; FILE *cmdOut = mytmpfile(); if (!cmdOut) { printf("NULL!\n"); } FILE *cmdIn = mytmpfile(); if (!cmdIn) { printf("NULL!\n"); } perror("PERROR: "); char tempBuf[1024]; int tmpBytes; char* curDir = malloc(1024*sizeof(char)); int bListening = 1; strcpy(curDir,"/"); pthread_setspecific(workerKey, curDir); pthread_setspecific(listeningKey, &bListening); send(socket, prompt, 3, 0); while(bListening) { /*if( !feof(cmdIn) ) { }*/ tmpBytes = -1; rewind(cmdIn); while(tmpBytes < 0) { while( (tmpBytes = recv(socket, tempBuf, 1, 0)) && tmpBytes > 0 ) { //if(tempBuf[0] == '\n') //{ // break; //} //printf("tmpBytes == %d\n", tempBytes); //printf("IBuffer == %s\n", tempBuf); if(tempBuf[0] == '\n') { break; } fwrite(tempBuf, 1, 1, cmdIn); } } //printf("NumBytes = %d\n", tmpBytes); //printf("Buffer = %s\n", tempBuf); fwrite(tempBuf,1, 1, cmdIn); fflush(cmdIn); rewind(cmdIn); strcpy(input, ""); readLine(cmdIn, input); //recv(socket, tempBuf, strlen(input), 0); //printf("numBytesReceived=%d\n",tmpBytes); //printf("numBytesUsed=%d\n",strlen(input)); if( strcmp(input, "")) { strcpy(outputLine, ""); printf("Input = %s\n", input); processCommand(input, socket, outputLine); //printf("output = %s", outputLine); if(strlen(outputLine) > 0) { fputs(outputLine, cmdOut); fputc('\n',cmdOut); fputs(prompt, cmdOut); fputc('\0', cmdOut); fflush(cmdOut); rewind(cmdOut); //printf("tempBuf = %s\n", tempBuf); int bytez = 0; while( (bytez = fread(tempBuf, 1, 1024, cmdOut)) == 1024) { send(socket, tempBuf, bytez, 0); } //printf("tempBuf = %s\n", tempBuf); send(socket, tempBuf, bytez, 0); strcpy(tempBuf, ""); rewind(cmdOut); } else { send(socket, prompt, 3, 0); } fclose(cmdOut); fclose(cmdIn); cmdIn = mytmpfile(); cmdOut = mytmpfile(); if(!strcmp(outputLine, "exit")) { AllStopListening(); bListening = 0; } if(!strcmp(outputLine, "quit")) { bListening = 0; } } else { break; } } fclose(cmdIn); fclose(cmdOut); close(socket); }
/* ** ** read source file into internal buffer ** */ char *ePerl_ReadSourceFile(char *filename, char **cpBufC, int *nBufC) { char *rc; FILE *fp = NULL; char *cpBuf = NULL; int nBuf; char tmpfile[256], *ptr_tmpfile; int usetmp = 0; int c; if (stringEQ(filename, "-")) { /* file is given on stdin */ ptr_tmpfile = mytmpfile("ePerl.source"); sprintf(tmpfile, "%s", ptr_tmpfile); if ((fp = fopen(tmpfile, "w")) == NULL) { ePerl_SetError("Cannot open temporary source file %s for writing", tmpfile); CU(NULL); } nBuf = 0; while ((c = fgetc(stdin)) != EOF) { fprintf(fp, "%c", c); } fclose(fp); fp = NULL; filename = tmpfile; usetmp = 1; } if ((fp = fopen(filename, "r")) == NULL) { ePerl_SetError("Cannot open source file %s for reading", filename); CU(NULL); } fseek(fp, 0, SEEK_END); nBuf = ftell(fp); if (nBuf == 0) { cpBuf = (char *)malloc(sizeof(char) * 1); *cpBuf = NUL; } else { if ((cpBuf = (char *)malloc(sizeof(char) * nBuf+1)) == NULL) { ePerl_SetError("Cannot allocate %d bytes of memory", nBuf); CU(NULL); } fseek(fp, 0, SEEK_SET); if (fread(cpBuf, nBuf, 1, fp) == 0) { ePerl_SetError("Cannot read from file %s", filename); CU(NULL); } cpBuf[nBuf] = '\0'; } *cpBufC = cpBuf; *nBufC = nBuf; RETURN_WVAL(cpBuf); CUS: if (cpBuf) free(cpBuf); if (fp) fclose(fp); if (usetmp) unlink(tmpfile); RETURN_EXRC; }