static ssize_t /* decode */ chunked_read(void *handle, char *buf, size_t size) { chunked_context *ctx = handle; for(;;) { if ( ctx->avail > 0 ) /* data waiting */ { size_t max_rd = ctx->avail < size ? ctx->avail : size; ssize_t rc; if ( (rc = Sfread(buf, sizeof(char), max_rd, ctx->stream)) > 0 ) { ctx->avail -= rc; if ( ctx->avail == 0 ) { if ( Sgetc(ctx->stream) != '\r' || Sgetc(ctx->stream) != '\n' ) { Sseterr(ctx->chunked_stream, 0, "Chunk not followed by \\r\\n"); return -1; } } return rc; } else if ( rc == 0 ) { Sseterr(ctx->chunked_stream, 0, "Unexpected EOF in chunked data"); return -1; } else { return -1; } } else { char hdr[MAXHDR]; char *s; if ( (s = Sfgets(hdr, sizeof(hdr), ctx->stream)) ) { char *ehdr; long len; errno = 0; len = strtol(hdr, &ehdr, 16); if ( errno || len < 0 ) { Sseterr(ctx->chunked_stream, 0, "Bad chunk length"); return -1; } if ( len == 0 ) { do { s = Sfgets(hdr, sizeof(hdr), ctx->stream); } while ( s && strcmp(s, "\r\n") != 0 ); if ( s ) return 0; Sseterr(ctx->chunked_stream, 0, "Bad end-of-stream"); return -1; } ctx->avail = len; /*continue*/ } } } }
static char * findHome(const char *symbols, int argc, const char **argv) { const char *home = NULL; char envbuf[MAXPATHLEN]; char plp[MAXPATHLEN]; const char *val; if ( (val=longopt("home", argc, argv)) ) { if ( (home=PrologPath(val, plp, sizeof(plp))) ) return store_string(home); return NULL; } if ( (val = exec_var("homevar")) && (home = Getenv(val, envbuf, sizeof(envbuf))) && (home = PrologPath(home, plp, sizeof(plp))) ) return store_string(home); if ( (val = exec_var("home")) && (home = PrologPath(val, plp, sizeof(plp))) ) return store_string(home); #ifdef PLHOMEVAR_1 if ( !(home = Getenv(PLHOMEVAR_1, envbuf, sizeof(envbuf))) ) { #ifdef PLHOMEVAR_2 home = Getenv(PLHOMEVAR_2, envbuf, sizeof(envbuf)); #endif } if ( home && (home = PrologPath(home, plp, sizeof(plp))) && ExistsDirectory(home) ) return store_string(home); #endif #ifdef PLHOMEFILE if ( (home = symbols) ) { char buf[MAXPATHLEN]; char parent[MAXPATHLEN]; IOSTREAM *fd; strcpy(parent, DirName(DirName(AbsoluteFile(home, buf), buf), buf)); Ssprintf(buf, "%s/" PLHOMEFILE, parent); if ( (fd = Sopen_file(buf, "r")) ) { if ( Sfgets(buf, sizeof(buf), fd) ) { size_t l = strlen(buf); while(l > 0 && buf[l-1] <= ' ') l--; buf[l] = EOS; #if O_XOS { char buf2[MAXPATHLEN]; _xos_canonical_filename(buf, buf2, MAXPATHLEN, 0); strcpy(buf, buf2); } #endif if ( !IsAbsolutePath(buf) ) { char buf2[MAXPATHLEN]; Ssprintf(buf2, "%s/%s", parent, buf); home = AbsoluteFile(buf2, plp); } else home = AbsoluteFile(buf, plp); if ( ExistsDirectory(home) ) { Sclose(fd); return store_string(home); } } Sclose(fd); } } #endif /*PLHOMEFILE*/ if ( (home = PrologPath(PLHOME, plp, sizeof(plp))) && ExistsDirectory(home) ) return store_string(home); return NULL; }
static unsigned char * read_x11_bitmap_file(IOSTREAM *fd, int *w, int *h) { unsigned char *data = NULL; char line[LINESIZE]; int size = 0; char name_and_type[LINESIZE]; /* an input line */ char *type; /* for parsing */ int value; /* from an input line */ int version10p; /* bool, old format */ int padding; /* to handle alignment */ int bytes_per_line; /* per scanline of data */ unsigned int ww = 0; /* width */ unsigned int hh = 0; /* height */ int hx = -1; /* x hotspot */ int hy = -1; /* y hotspot */ if (initialized == FALSE) initHexTable(); #define RETURN_ERROR { if (data) XFree(data); return NULL; } while (Sfgets(line, LINESIZE, fd)) { if ( sscanf(line,"#define %s %d",name_and_type,&value) == 2) { if (!(type = strrchr(name_and_type, '_'))) type = name_and_type; else type++; if (!strcmp("width", type)) ww = (unsigned int) value; if (!strcmp("height", type)) hh = (unsigned int) value; if (!strcmp("hot", type)) { if (type-- == name_and_type || type-- == name_and_type) continue; if (!strcmp("x_hot", type)) hx = value; if (!strcmp("y_hot", type)) hy = value; } continue; } if (sscanf(line, "static short %s = {", name_and_type) == 1) version10p = 1; else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1) version10p = 0; else if (sscanf(line, "static char %s = {", name_and_type) == 1) version10p = 0; else continue; if (!(type = strrchr(name_and_type, '_'))) type = name_and_type; else type++; if (strcmp("bits[]", type)) continue; if (!ww || !hh) RETURN_ERROR; if ((ww % 16) && ((ww % 16) < 9) && version10p) padding = 1; else padding = 0; bytes_per_line = (ww+7)/8 + padding; size = bytes_per_line * hh; data = (unsigned char *) malloc(size); if (version10p) { unsigned char *ptr; int bytes; for (bytes=0, ptr=data; bytes<size; (bytes += 2)) { if ((value = NextInt(fd)) < 0) RETURN_ERROR; *(ptr++) = value; if (!padding || ((bytes+2) % bytes_per_line)) *(ptr++) = value >> 8; } } else { unsigned char *ptr; int bytes; for (bytes=0, ptr=data; bytes<size; bytes++, ptr++) { if ((value = NextInt(fd)) < 0) RETURN_ERROR; *ptr=value; } } }
static char * findHome(char *symbols) { char *home = NULL; char envbuf[MAXPATHLEN]; char plp[MAXPATHLEN]; const char *val; if ( (val = exec_var("homevar")) && (home = getenv3(val, envbuf, sizeof(envbuf))) && (home = PrologPath(home, plp)) ) return store_string(home); if ( (val = exec_var("home")) && (home = PrologPath(home, plp)) ) return store_string(home); if ( !(home = getenv3("SWI_HOME_DIR", envbuf, sizeof(envbuf))) ) home = getenv3("SWIPL", envbuf, sizeof(envbuf)); if ( home && (home = PrologPath(home, plp)) && ExistsDirectory(home) ) return store_string(home); if ( (home = symbols) ) { char buf[MAXPATHLEN]; char parent[MAXPATHLEN]; IOSTREAM *fd; strcpy(parent, DirName(DirName(AbsoluteFile(home, buf), buf), buf)); Ssprintf(buf, "%s/swipl", parent); if ( (fd = Sopen_file(buf, "r")) ) { if ( Sfgets(buf, sizeof(buf), fd) ) { int l = strlen(buf); while(l > 0 && buf[l-1] <= ' ') l--; buf[l] = EOS; #if O_XOS { char buf2[MAXPATHLEN]; _xos_canonical_filename(buf, buf2); strcpy(buf, buf2); } #endif if ( !IsAbsolutePath(buf) ) { char buf2[MAXPATHLEN]; Ssprintf(buf2, "%s/%s", parent, buf); home = AbsoluteFile(buf2, plp); } else home = AbsoluteFile(buf, plp); if ( ExistsDirectory(home) ) { Sclose(fd); return store_string(home); } } Sclose(fd); } } if ( (home = PrologPath(PLHOME, plp)) && ExistsDirectory(home) ) return store_string(home); return NULL; }