void ngcopen(void) { if(ngcdb == 0) { nameopen(); ngcdb = eopen("ngc2000"); ngctypedb = eopen("ngc2000type"); Eread(ngctypedb, "ngctype", ngctype, sizeof ngctype); close(ngctypedb); } }
int main(int argc, char *argv[]) { struct stat a, b; char path[] = "test_O_TRUC"; int fd = eopen(path, O_RDWR | O_CREAT | O_TRUNC); efstat(fd, &a); eclose(fd); fd = eopen(path, O_RDWR | O_CREAT | O_TRUNC); efstat(fd, &b); aver(a.st_ino == b.st_ino); eclose(fd); eunlink(path); return 0; }
void fb_init(struct framebuffer *fb) { char *path; struct fb_fix_screeninfo finfo; struct fb_var_screeninfo vinfo; if ((path = getenv("FRAMEBUFFER")) != NULL) fb->fd = eopen(path, O_RDWR); else fb->fd = eopen(fb_path, O_RDWR); if (ioctl(fb->fd, FBIOGET_FSCREENINFO, &finfo) < 0) fatal("ioctl: FBIOGET_FSCREENINFO failed"); if (ioctl(fb->fd, FBIOGET_VSCREENINFO, &vinfo) < 0) fatal("ioctl: FBIOGET_VSCREENINFO failed"); /* check screen offset and initialize because linux console change this */ /* if (vinfo.xoffset != 0 || vinfo.yoffset != 0) { vinfo.xoffset = vinfo.yoffset = 0; ioctl(fb->fd, FBIOPUT_VSCREENINFO, &vinfo); } */ fb->width = vinfo.xres; fb->height = vinfo.yres; fb->screen_size = finfo.smem_len; fb->line_length = finfo.line_length; if ((finfo.visual == FB_VISUAL_TRUECOLOR || finfo.visual == FB_VISUAL_DIRECTCOLOR) && (vinfo.bits_per_pixel == 15 || vinfo.bits_per_pixel == 16 || vinfo.bits_per_pixel == 24 || vinfo.bits_per_pixel == 32)) { fb->cmap = fb->cmap_org = NULL; fb->bpp = my_ceil(vinfo.bits_per_pixel, BITS_PER_BYTE); } else if (finfo.visual == FB_VISUAL_PSEUDOCOLOR && vinfo.bits_per_pixel == 8) { cmap_create(&fb->cmap_org); if (ioctl(fb->fd, FBIOGETCMAP, fb->cmap_org) < 0) fatal("ioctl: FBIOGETCMAP failed"); fb->cmap = NULL; fb->bpp = 1; } else /* non packed pixel, mono color, grayscale: not implimented */ fatal("unsupported framebuffer type"); fb->fp = (unsigned char *) emmap(0, fb->screen_size, PROT_WRITE | PROT_READ, MAP_SHARED, fb->fd, 0); fb->buf = (unsigned char *) ecalloc(1, fb->screen_size); fb->vinfo = vinfo; }
void constelopen(void) { int i; if(condb == 0) { condb = eopen("con"); conindexdb = eopen("conindex"); Eread(conindexdb, "conindex", conindex, sizeof conindex); close(conindexdb); for(i=0; i<Ncon+1; i++) conindex[i] = Short((int16_t*)&conindex[i]); } }
void abellopen(void) { /* nothing extra to do with abell: it's directly indexed by number */ if(abelldb == 0) abelldb = eopen("abell"); }
static int /* boolean */ confirm(File *src, File *dest) { int absent, n, tty = eopen(TTY, 2); char c, junk; Dir *stp; if ((stp = dirstat(src->name)) == nil) sysfatal("no input file %s: %r", src->name); free(stp); stp = dirstat(dest->name); absent = (stp == nil); free(stp); fprint(2, "%s: copy %s to %s%s? ", argv0, src->name, dest->name, (absent? " (missing)": "")); n = read(tty, &c, 1); junk = c; if (n < 1) c = 'n'; while (n > 0 && junk != '\n') n = read(tty, &junk, 1); close(tty); if (isascii(c) && isupper(c)) c = tolower(c); return c == 'y'; }
void saoopen(void) { if(saodb == 0) { nameopen(); saodb = eopen("sao"); } }
void nameopen(void) { Biobuf b; int i; char *l, *p; if(namedb == 0) { namedb = eopen("name"); Binit(&b, namedb, OREAD); for(i=0; i<NName; i++) { l = Brdline(&b, '\n'); if(l == 0) break; p = strchr(l, '\t'); if(p == 0) { Badformat: Bprint(&bout, "warning: name.scat bad format; line %d\n", i+1); break; } *p++ = 0; strcpy(name[i].name, l); if(strncmp(p, "ngc", 3) == 0) name[i].ngc = atoi(p+3); else if(strncmp(p, "ic", 2) == 0) name[i].ngc = atoi(p+2)+NNGC; else if(strncmp(p, "sao", 3) == 0) name[i].sao = atoi(p+3); else if(strncmp(p, "abell", 5) == 0) name[i].abell = atoi(p+5); else goto Badformat; } if(i == NName) Bprint(&bout, "warning: too many names in name.scat (max %d); extra ignored\n", NName); close(namedb); bayerdb = eopen("bayer"); Eread(bayerdb, "bayer", bayer, sizeof bayer); close(bayerdb); for(i=0; i<NBayer; i++) bayer[i].sao = Long(&bayer[i].sao); } }
static void verify_sync(void) { int fd = eopen(Option.file, O_RDONLY); int rc = eread(fd, Data, sizeof(Data)); if (rc != sizeof(Data)) { fatal("Expeted %d bytes read but read %d", sizeof(Data), rc); } verify_data(); eclose(fd); }
static void mmap_msync(void) { u32 *map; int i; int fd = eopen(Option.file, O_RDWR | O_CREAT | O_TRUNC); epwrite(fd, "", 1, FILE_SIZE - 1); map = emmap(0, FILE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); for (i = 0; i < NUM_INTS; i++) map[i] = 2 * i; emsync(map, FILE_SIZE, MS_SYNC); crash(); }
void mopen(void) { int i; if(mindexdb == 0) { mindexdb = eopen("mindex"); Eread(mindexdb, "mindex", mindex, sizeof mindex); close(mindexdb); for(i=0; i<NMrec; i++) mindex[i].ngc = Short(&mindex[i].ngc); } }
void patchopen(void) { Biobuf *b; int32_t l, m; char buf[100]; if(patchdb == 0) { patchdb = eopen("patch"); sprint(buf, "%s/patchindex.scat", dir); b = Bopen(buf, OREAD); if(b == 0) { fprint(2, "can't open %s\n", buf); exits("open"); } for(m=0,l=0; l<=Npatch; l++) patchaddr[l] = m += Bgetc(b)*4; Bterm(b); } }
main(int argc, char **argv) { extern int optind; /* for use of getopt() */ extern char *optarg; /* for use of getopt() */ int ch; /* command-line option letter */ int outarch = NATIVE; char **field_names = NULL; int num_fields = 0; int alloc_fields = 0; int rflag = NO; /* -r option specified? */ char *rrange; /* arguments of -r option */ long start_rec; /* starting record number */ long end_rec; /* ending record number */ long num_recs; /* number of records to read (0 means all up to end of file) */ long num_read; /* number of records actually read */ int Aflag = NO; /* annotate Ascii output? */ Annot *annotate = NULL; int annwidth = 70; char *iname; /* input file name */ FILE *ifile; /* input stream */ struct header *ihd; /* input file header */ struct fea_data *irec; /* input record */ char *oname; /* output file name */ FILE *ofile; /* output stream */ FieldList list; /* output field list */ int outord = TYPE_ORDER; FieldSpec **ofields; /* output fields in field or type order */ double rec_freq; double start_time_offset; char *fdata; double *edata; int type; int len, i; long dim[1]; FieldSpec *field; FieldList source; /* field list of source file */ while ((ch = getopt(argc, argv, "a:f:r:x:A:F")) != EOF) switch (ch) { case 'a': outarch = ((!strcmp(optarg, "EDR1")) ? EDR1 : (!strcmp(optarg, "EDR2")) ? EDR2 : (!strcmp(optarg, "NATIVE")) ? NATIVE : (!strcmp(optarg, "ASCII")) ? ASCII : UNKNOWN); break; case 'f': if (num_fields >= alloc_fields) { size_t size; alloc_fields = num_fields + 1 + num_fields/2; size = (alloc_fields + 1) * sizeof(char *); field_names = (char **) ((field_names == NULL) ? malloc(size) : realloc(field_names, size)); } field_names[num_fields++] = optarg; field_names[num_fields] = NULL; break; case 'r': rflag = YES; rrange = optarg; break; case 'x': debug_level = atoi(optarg); break; case 'A': Aflag = YES; annwidth = atoi(optarg); break; case 'F': outord = FIELD_ORDER; break; default: SYNTAX; break; } if (argc - optind > 2) { fprintf(stderr, "%s: too many file names specified.\n", ProgName); SYNTAX; } if (argc - optind < 2) { fprintf(stderr, "%s: too few file names specified.\n", ProgName); SYNTAX; } iname = eopen(ProgName, argv[optind++], "r", FT_FEA, NONE, &ihd, &ifile); oname = argv[optind++]; start_rec = 1; end_rec = LONG_MAX; num_recs = 0; if (rflag) { lrange_switch(rrange, &start_rec, &end_rec, 0); if (end_rec != LONG_MAX) num_recs = end_rec - start_rec + 1; } REQUIRE(start_rec >= 1, "can't start before beginning of file"); REQUIRE(end_rec >= start_rec, "empty range of records specified"); if (debug_level) fprintf(stderr, "start_rec: %ld. end_rec: %ld. num_recs: %ld.\n", start_rec, end_rec, num_recs); DebugMsgLevel = debug_level; DebugMsgFunc = DebugPrint; irec = allo_fea_rec(ihd); REQUIRE(irec != NULL, "can't allocate memory for input record"); list = fea_to_FieldList(ihd, irec, field_names, FALSE); REQUIRE(list != NULL, "failure converting input header to field list"); switch (outord) { case TYPE_ORDER: if (debug_level) fprintf(stderr, "making type-ordered field array.\n"); ofields = TypeOrder(list); break; case FIELD_ORDER: if (debug_level) fprintf(stderr, "making field-ordered field array.\n"); ofields = FieldOrder(list); break; default: REQUIRE(0, "output order neither TYPE_ORDER nor FIELD_ORDER"); break; } if (debug_level) fprintf(stderr, "setting field ordering.\n"); REQUIRE(SetFieldOrdering(&list, outord), "can't set field ordering of output"); rec_freq = get_genhd_val("record_freq", ihd, 1.0); type = genhd_type("record_freq", &len, ihd); if (type != HD_UNDEF) { field = AddGlobalField(&list, "recordFreq", 0, NULL, EDOUBLE, NULL); *(double *) field->data = rec_freq; } type = genhd_type("start_time", &len, ihd); if (type != HD_UNDEF && rec_freq != 0) { start_time_offset = (start_rec - 1) / rec_freq; fdata = (char *) get_genhd("start_time", ihd); edata = (double *) type_convert((long) len, fdata, type, (char *) NULL, DOUBLE, (void (*)()) NULL); if (start_time_offset != 0) { for (i = 0; i < len; i++) edata[i] += start_time_offset; } dim[0] = len; AddGlobalField(&list, "startTime", 1, dim, EDOUBLE, edata); } source = fea_to_FieldList(ihd, NULL, NULL, TRUE); (void) AddSource(&list, 0, iname, source); (void) AddCommandLine(&list, GetCommandLine(argc, argv)); if (debug_level) fprintf(stderr, "annwidth %d.\n", annwidth); if (Aflag) { annotate = (Annot *) malloc(sizeof(Annot)); annotate->position = 0; annotate->indent = 0; annotate->width = annwidth; annotate->recnum = 0; } if (debug_level) fprintf(stderr, "writing Esignal header.\n"); REQUIRE(OpenOut(oname, list, outarch, &ofile, annotate), "write header failed"); if (ofile == stdout) oname = "<stdout>"; num_read = start_rec - 1; if (debug_level) fprintf(stderr, "skipping %ld records.\n", num_read); /* skiprec(ifile, num_read, size_rec(ihd)); */ fea_skiprec(ifile, num_read, ihd); while (num_read++ < end_rec && get_fea_rec(irec, ihd, ifile) != EOF) { if (debug_level > 2) fprintf(stderr, "Record number %ld read.\n", num_read); WriteRecord(ofields, outarch, ofile, annotate); } if (--num_read < end_rec && num_recs != 0) fprintf(stderr, "fea2esig: only %ld records read.\n", num_read - (start_rec - 1)); exit(0); /*NOTREACHED*/ }
static void retsFromOdbc(void) { void *q, *q1; int i, l; int fd, flags; bool yearfirst, indata = false; long dt; /* temporarily hold date or time */ char *s; short c_type; /* C data type */ long input_length, output_length; char tbuf[20]; /* temp buf, for dates and times */ double fmoney; /* float version of money */ int blobcount = 0; bool fbc = fetchBlobColumns; /* no blobs unless proven otherwise */ rv_blobLoc = 0; rv_blobSize = nullint; if(!rv_numRets) errorPrint("@calling retsFromOdbc() with no returns pending"); stmt_text = "retsFromOdbc"; debugStatement(); /* count the blobs */ if(fbc) for(i = 0; i < rv_numRets; ++i) if(rv_type[i] == 'B' || rv_type[i] == 'T') ++blobcount; if(blobcount > 1) { i_puts(MSG_DBManyBlobs); fbc = false; } for(i = 0; i < rv_numRets; ++i) { if(!indata) { q = va_arg(sqlargs, void *); if(!q) { if(i) break; indata = true; } } if(indata) { if(rv_type[i] == 'S') { q = retstring[i]; rv_data[i].ptr = q; } else q = rv_data + i; } if((int)q < 1000 && (int)q > -1000) errorPrint("2retsFromOdbc, pointer too close to 0"); q1 = q; tbuf[0] = 0; output_length = 0; switch (rv_type[i]) { case 'S': c_type = SQL_C_CHAR; input_length = STRINGLEN + 1; *(char *)q = 0; /* null */ break; case 'C': c_type = SQL_C_CHAR; input_length = 2; *(char *)q = 0; /* null */ q1 = tbuf; break; case 'F': c_type = SQL_C_DOUBLE; input_length = 8; *(double *)q = nullfloat; /* null */ break; case 'N': c_type = SQL_C_SLONG; input_length = 4; *(long *)q = nullint; /* null */ break; case 'M': c_type = SQL_C_DOUBLE; input_length = 8; fmoney = nullfloat; q1 = &fmoney; break; case 'D': c_type = SQL_C_CHAR; input_length = 11; q1 = tbuf; break; case 'I': c_type = SQL_C_CHAR; input_length = 10; q1 = tbuf; break; case 'B': case 'T': c_type = SQL_C_BINARY; input_length = sizeof (blobbuf); q1 = blobbuf; *(long *)q = nullint; break; default: errorPrint("@retsFromOdbc, rv_type[%d] = %c", i, rv_type[i]); } /* switch */ if(everything_null || c_type == SQL_C_BINARY && !fbc) { rc = SQL_SUCCESS; output_length = SQL_NULL_DATA; } else { rc = SQLGetData(hstmt, (ushort) (i + 1), c_type, q1, input_length, &output_length); /* we'll deal with blob overflow later */ if(rc == SQL_SUCCESS_WITH_INFO && c_type == SQL_C_BINARY && output_length > sizeof (blobbuf)) rc = SQL_SUCCESS; if(errorTrap(0)) break; if(output_length == SQL_NO_TOTAL) errorPrint ("@retsFromOdbc cannot get size of data for column %d", i + 1); } /* Postprocess the return values. */ /* For instance, turn string dates into our own 4-byte format. */ s = tbuf; clipString(s); switch (rv_type[i]) { case 'C': *(char *)q = tbuf[0]; break; case 'S': clipString(q); break; case 'D': yearfirst = false; if(s[4] == '-') yearfirst = true; dt = stringDate(s, yearfirst); if(dt < 0) errorPrint("@database holds invalid date %s", s); *(long *)q = dt; break; case 'I': /* thanks to stringTime(), this works for either hh:mm or hh:mm:ss */ if(s[0] == 0) *(long *)q = nullint; else { /* Note that Informix introduces a leading space, how about ODBC? */ leftClipString(s); if(s[1] == ':') shiftRight(s, '0'); dt = stringTime(s); if(dt < 0) errorPrint("@database holds invalid time %s", s); *(long *)q = dt; } break; case 'M': if(fmoney == nullfloat) dt = nullint; else dt = fmoney * 100.0 + 0.5; *(long *)q = dt; break; case 'B': case 'T': if(output_length == SQL_NULL_DATA) break; /* note, 0 length blob is treated as a null blob */ if(output_length == 0) break; /* the size of the blob is returned, in an int. */ *(long *)q = output_length; rv_blobSize = output_length; if(isnullstring(rv_blobFile)) { /* the blob is always allocated; you have to free it! */ /* SQL doesn't null terminate its text blobs, but we do. */ rv_blobLoc = allocMem(output_length + 1); l = output_length; if(l > sizeof (blobbuf)) l = sizeof (blobbuf); memcpy(rv_blobLoc, blobbuf, l); if(l < output_length) { /* more to do */ long waste; rc = SQLGetData(hstmt, (ushort) (i + 1), c_type, (char *)rv_blobLoc + l, output_length - l, &waste); if(rc) { nzFree(rv_blobLoc); rv_blobLoc = 0; *(long *)q = nullint; errorTrap(0); goto breakloop; } /* error getting rest of blob */ } /* blob is larger than the buffer */ if(rv_type[i] == 'T') /* null terminate */ ((char *)rv_blobLoc)[output_length] = 0; break; } /* blob in memory */ /* at this point the blob is being dumped into a file */ flags = O_WRONLY | O_BINARY | O_CREAT | O_TRUNC; if(rv_blobAppend) flags = O_WRONLY | O_BINARY | O_CREAT | O_APPEND; fd = eopen(rv_blobFile, flags, 0666); rc = SQL_SUCCESS; while(true) { int outbytes; l = output_length; if(l > sizeof (blobbuf)) l = sizeof (blobbuf); outbytes = write(fd, blobbuf, l); if(outbytes < l) { close(fd); errorPrint("2cannot write to file %s, errno %d", rv_blobFile, errno); } if(l == output_length) break; /* get the next chunk from ODBC */ rc = SQLGetData(hstmt, (ushort) (i + 1), c_type, q1, input_length, &output_length); if(rc == SQL_SUCCESS_WITH_INFO && output_length > input_length) rc = SQL_SUCCESS; /* data truncation error */ } close(fd); errorTrap(0); break; } /* switch */ } /* loop over returned elements */
main(int argc, char **argv) { extern int optind; /* for use of getopt() */ extern char *optarg; /* for use of getopt() */ int ch; /* command-line option letter */ static char *ProgName = "esig2fea"; /* name of this program */ static char *Version = SCCS_VERSION; /* program SCCS version */ static char *Date = SCCS_DATE; /* program SCCS date */ char **field_names = NULL; int num_fields = 0; int alloc_fields = 0; int rflag = NO; /* -r option specified? */ char *rrange; /* arguments of -r option */ long start_rec; /* starting record number */ long end_rec; /* ending record number */ long num_recs; /* number of records to read (0 means all up to end of file) */ long num_read; /* number of records actually read */ char *iname; /* input file name */ FILE *ifile; /* input stream */ FieldList list; /* input field list */ int inord; /* input: field order or type order? */ FieldSpec **ifields; /* input fields in field or type order */ char *subtype = NULL; /* FEA subtype name */ int subtype_code = NONE; /* numeric subtype code */ FieldSpec *fld; /* spec of various special fields */ char *oname; /* output file name */ FILE *ofile; /* output stream */ struct header *ohd; /* output file header */ struct fea_data *orec; /* output record */ int outord = TYPE_ORDER; char *version; /* version from input preamble */ int arch; /* architecture from input preamble */ long pre_size; /* preamble size */ long hdr_size; /* header size (bytes) from preamble */ long rec_size; /* record size from preamble */ double rec_freq; double start_time_offset; double *data; long len, i; struct header *source; /* embedded source-file header */ while ((ch = getopt(argc, argv, "f:r:x:FT:")) != EOF) switch (ch) { case 'f': if (num_fields >= alloc_fields) { size_t size; alloc_fields = num_fields + 1 + num_fields/2; size = (alloc_fields + 1) * sizeof(char *); field_names = (char **) ((field_names == NULL) ? malloc(size) : realloc(field_names, size)); } field_names[num_fields++] = optarg; field_names[num_fields] = NULL; break; case 'r': rflag = YES; rrange = optarg; break; case 'x': debug_level = atoi(optarg); break; case 'F': outord = FIELD_ORDER; break; case 'T': subtype = optarg; break; default: SYNTAX; break; } if (argc - optind > 2) { fprintf(stderr, "%s: too many file names specified.\n", ProgName); SYNTAX; } if (argc - optind < 2) { fprintf(stderr, "%s: too few file names specified.\n", ProgName); SYNTAX; } DebugMsgLevel = debug_level; DebugMsgFunc = DebugPrint; iname = argv[optind++]; list = OpenIn(iname, &version, &arch, &pre_size, &hdr_size, &rec_size, &ifile); REQUIRE(list != NULL, "read header failed"); if (ifile == stdin) iname = "<stdin>"; oname = argv[optind++]; start_rec = 0; end_rec = LONG_MAX; num_recs = 0; /* 0 means continue to end of file */ if (rflag) { lrange_switch(rrange, &start_rec, &end_rec, 0); if (end_rec != LONG_MAX) num_recs = end_rec - start_rec + 1; } REQUIRE(start_rec >= 0, "can't start before beginning of file"); REQUIRE(end_rec >= start_rec, "empty range of records specified"); if (debug_level) fprintf(stderr, "start_rec: %ld. end_rec: %ld. num_recs: %ld.\n", start_rec, end_rec, num_recs); REQUIRE(GetFieldOrdering(list, &inord), "cant get field ordering of input"); switch (inord) { case TYPE_ORDER: if (debug_level) fprintf(stderr, "making type-ordered field array.\n"); ifields = TypeOrder(list); break; case FIELD_ORDER: if (debug_level) fprintf(stderr, "making field-ordered field array.\n"); ifields = FieldOrder(list); break; default: REQUIRE(0, "input order neither TYPE_ORDER nor FIELD_ORDER"); break; } ohd = FieldList_to_fea(list, &orec, field_names, FALSE); REQUIRE(ohd != NULL, "failure converting input field list to header & record struct"); if (subtype != NULL) { subtype_code = lin_search(fea_file_type, subtype); if (subtype_code == -1) fprintf(stderr, "%s: unknown FEA file subtype \"%s\" ignored.\n", ProgName, subtype); else ohd->hd.fea->fea_type = subtype_code; } if (outord == FIELD_ORDER) ohd->hd.fea->field_order = YES; fld = FindField(list, "recordFreq"); if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL) { (void) type_convert(1L, (char *) fld->data, ElibTypeToEsps(fld->type), (char *) &rec_freq, FDOUBLE, (void (*)()) NULL); *add_genhd_d("record_freq", NULL, 1, ohd) = rec_freq; } else rec_freq = 1.0; fld = FindField(list, "startTime"); if (fld != NULL && fld->occurrence == GLOBAL && fld->data != NULL && rec_freq != 0) { start_time_offset = start_rec / rec_freq; len = FieldLength(fld); data = (double *) type_convert(len, (char *) fld->data, ElibTypeToEsps(fld->type), (char *) NULL, FDOUBLE, (void (*)()) NULL); if (start_time_offset != 0) { for (i = 0; i < len; i++) data[i] += start_time_offset; } (void) add_genhd_d("start_time", data, len, ohd); } (void) strcpy(ohd->common.prog, ProgName); (void) strcpy(ohd->common.vers, Version); (void) strcpy(ohd->common.progdate, Date); source = FieldList_to_fea(list, NULL, NULL, TRUE); add_source_file(ohd, savestring(iname), source); add_comment(ohd, get_cmd_line(argc, argv)); oname = eopen(ProgName, oname, "w", NONE, NONE, NULL, &ofile); write_header(ohd, ofile); num_read = SkipRecs(ifile, start_rec, RecordSize(list, arch), ifields, arch); if (num_read != start_rec) { fprintf(stderr, "%s: couldn't reach starting record; only %ld skipped.\n", ProgName, num_read); exit(0); } for ( ; num_read <= end_rec && ReadRecord(ifields, arch, ifile); num_read++) { put_fea_rec(orec, ohd, ofile); } if (num_read <= end_rec && num_recs != 0) fprintf(stderr, "esig2fea: only %ld records read.\n", num_read - start_rec); exit(0); /*NOTREACHED*/ }
char updateutmp_f (enum utmp_action options, struct utmp *new_entry) { int ufile; struct stat st; // strip the utmp_add action if we don't get a new entry to add along with it if ((options & utmp_add) && !new_entry) options ^= utmp_add; // if we don't have anything to do, bail out if (!options) return -1; if (coremode == einit_mode_sandbox) ufile = eopen ("var/run/utmp", O_RDWR); else ufile = eopen ("/var/run/utmp", O_RDWR); if (ufile) { if (!fstat (ufile, &st) && st.st_size) { struct utmp *utmpentries = mmap (NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, ufile, 0); if (utmpentries != MAP_FAILED) { uint32_t entries = st.st_size / sizeof(struct utmp), i = 0; eclose (ufile); ufile = 0; for (i = 0; i < entries; i++) { #ifdef LINUX switch (utmpentries[i].ut_type) { case DEAD_PROCESS: if (options & utmp_add) { memcpy (&(utmpentries[i]), new_entry, sizeof (struct utmp)); options ^= utmp_add; } break; case RUN_LVL: if (options & utmp_clean) { /* the higher 8 bits contain the old runlevel, the lower 8 bits the current one */ char *new_previous_runlevel = cfg_getstring ("configuration-compatibility-sysv-simulate-runlevel/before", NULL), *new_runlevel = cfg_getstring ("configuration-compatibility-sysv-simulate-runlevel/now", NULL); if (new_runlevel && new_runlevel[0]) { if (new_previous_runlevel) utmpentries[i].ut_pid = (new_previous_runlevel[0] << 8) | new_runlevel[0]; else utmpentries[i].ut_pid = (utmpentries[i].ut_pid << 8) | new_runlevel[0]; } } break; case UT_UNKNOWN: case BOOT_TIME: case NEW_TIME: case OLD_TIME: case INIT_PROCESS: case LOGIN_PROCESS: case USER_PROCESS: case ACCOUNTING: if (options & utmp_clean) { #ifdef LINUX struct stat xst; char path[BUFFERSIZE]; esprintf (path, BUFFERSIZE, "/proc/%i/", utmpentries[i].ut_pid); if (stat (path, &xst)) { // stat path under proc to see if process exists // if not... #endif // clean utmp record if (options & utmp_add) { memcpy (&(utmpentries[i]), new_entry, sizeof (struct utmp)); options ^= utmp_add; } else { utmpentries[i].ut_type = DEAD_PROCESS; memset (&(utmpentries[i].ut_user), 0, sizeof (utmpentries[i].ut_user)); memset (&(utmpentries[i].ut_host), 0, sizeof (utmpentries[i].ut_host)); memset (&(utmpentries[i].ut_time), 0, sizeof (utmpentries[i].ut_time)); } #ifdef LINUX } #endif } break; #ifdef DEBUG default: notice (6, "bad UTMP entry: [%c%c%c%c] %i (%s), %s@%s: %i.%i\n", utmpentries[i].ut_id[0], utmpentries[i].ut_id[1], utmpentries[i].ut_id[2], utmpentries[i].ut_id[3], utmpentries[i].ut_type, utmpentries[i].ut_line, utmpentries[i].ut_user, utmpentries[i].ut_host, (int)utmpentries[i].ut_tv.tv_sec, (int)utmpentries[i].ut_tv.tv_usec); break; #endif } if ((options & utmp_modify) && (utmpentries[i].ut_pid == new_entry->ut_pid)) { memcpy (&(utmpentries[i]), new_entry, sizeof (struct utmp)); options ^= utmp_modify; } #endif if (!options) break; } munmap (utmpentries, st.st_size); } else { bitch(bitch_stdio, 0, "mmap() failed"); } } if (ufile) eclose (ufile); } else { bitch(bitch_stdio, 0, "open() failed"); } if (options & utmp_add) { // still didn't get to add this.. try to append it to the file if (coremode == einit_mode_sandbox) ufile = open ("var/run/utmp", O_WRONLY | O_APPEND); else ufile = open ("/var/run/utmp", O_WRONLY | O_APPEND); if (ufile) { if (write(ufile, new_entry, sizeof (struct utmp)) != sizeof (struct utmp)) { bitch(bitch_stdio, 0, "short write to utmp file"); } eclose (ufile); } else { bitch(bitch_stdio, 0, "mmap() failed"); } options ^= utmp_add; } return 0; }
main(int argc, char **argv) { FILE *ifp; /* file pointer for input */ FILE *hfp; /* file pointer for popen write */ int bfd; /* file descriptor for bfile */ string tape; /* name of raw tape device */ int clean; /* clean trace header */ int verbose; /* echo every 20th trace */ int over; /* check format */ int convert; /* convert ibm fpt to ieee fpt */ string hfile; /* name of ascii header file */ string bfile; /* name of binary header file */ int trmin; /* first trace to read */ int trmax; /* last trace to read */ int nt; /* number of data samples */ char cmdbuf[BUFSIZ]; /* dd command buffer */ char ebcbuf[EBCBYTES]; /* ebcdic data buffer */ int itr = 0; /* current trace number */ bool nsflag = FALSE; /* flag for error in tr.ns */ char hdr_buf[10]; /* 1st 10 bytes of header in ascii */ char tmp_buf[3600]; /* temp. buffer to read in header */ unsigned int nsamp; /* number of samples per trace */ int i; /* loop counter to zero trace samples */ int *ibstart,*ibyte,*itype; int *obstart,*obyte,*otype; int nmap=0, imap; int ibs,iby,ity,obs,oby,oty; short itmp2; int itmp4; float tmp; int ntg=0; int rmbadtrace, ibt, nbt; /* initialize */ initargs(argc, argv); askdoc(1); /* make sure stdout is a file or pipe */ switch(filestat(STDOUT)) { case TTY: err("stdout can't be tty"); break; case DIRECTORY: err("stdout must be a file, not a directory"); break; case BADFILETYPE: err("stdout is illegal filetype"); break; } /* set filenames */ if (!getparstring("tape", &tape)) { ifp = stdin; file2g(ifp); } else { /* open files - first the tape */ ifp = efopen(tape, "r"); } file2g(stdout); /* set parameters */ if (!getparint("clean", &clean)) clean = 1; if (!getparint("verbose", &verbose)) verbose = 0; if (!getparint("over", &over)) over = 0; if (!getparint("convert", &convert)) convert = 1; if (!getparint("trmin", &trmin)) trmin = 1; if (!getparint("trmax", &trmax)) trmax = LONG_MAX; if (!getparint("rmbadtrace",&rmbadtrace)) rmbadtrace=0; nmap = countparval("ibstart"); if(nmap>0) { ibstart = (int*) malloc(nmap*sizeof(int)); ibyte = (int*) malloc(nmap*sizeof(int)); itype = (int*) malloc(nmap*sizeof(int)); obstart = (int*) malloc(nmap*sizeof(int)); obyte = (int*) malloc(nmap*sizeof(int)); otype = (int*) malloc(nmap*sizeof(int)); if(getparint("ibstart",ibstart)!=nmap) err(" check ibstart"); if(getparint("ibyte",ibyte)!=nmap) err(" check ibyte"); if(getparint("itype",itype)!=nmap) err(" check itype"); if(getparint("obstart",obstart)!=nmap) err(" check obstart"); if(getparint("obyte",obyte)!=nmap) err(" check obyte"); if(getparint("otype",otype)!=nmap) err(" check otype"); } /* read ebcdic and binary headers */ efread(ebcbuf, 1, EBCBYTES, ifp); efread((char *)&bh, 1, BNYBYTES, ifp); if (bh.format != 1) (over) ? warn("ignore bh.format ... continue") : err("format not IBM floating point"); if (!convert) warn( "assuming data is IEEE floating point, no conversion will be done"); /* set nt parameter */ if (!getparint("nt", &nt)) { nt = bh.hns; ntg = 0; } else { ntg = 1; } /* if needed, save ebcbuf into hfile */ if (getparstring("hfile", &hfile)) { /* Open pipe to use dd to convert ebcdic to ascii */ sprintf(cmdbuf, "dd ibs=3200 of=%s conv=ascii cbs=80 count=1", hfile); hfp = epopen(cmdbuf, "w"); /* Write ascii stream from buffer into pipe */ efwrite(ebcbuf, EBCBYTES, 1, hfp); epclose(hfp); } /* save the binary file, if needed */ if (getparstring("bfile", &bfile)) { /* - the binary data file */ bfd = eopen(bfile, O_WRONLY | O_CREAT | O_TRUNC, 0644); /* Write binary header from bhed structure to binary file */ ewrite(bfd, (char *)&bh, BNYBYTES); eclose(bfd); } /* convert ebcdic to ascii for output data */ tascii_((unsigned char*)ebcbuf, (unsigned char*)&ch, EBCBYTES, 0); if (strncmp((char*)&ch, "C 1 CLIENT",10) != 0 ) { memcpy((char *)&ch, "C 1 CLIENT", 10); } /* test if number of samples set in binary header */ if (!bh.hns) { warn("samples/trace not set in binary header \n"); if (nt == 0) warn("samples/trace in 1st trace used \n"); else warn("nt in input used for samples/trace \n"); } if ((nt != bh.hns) && (nt != 0)) { warn("samples/trace reset in binary header =%d \n",nt); bh.hns = nt; } /* output ascii and binary headers to stdout */ puthdr(&ch, &bh); nbt = 0; /* convert the traces */ while (efread((char *)&tr, 1, HDRBYTES, ifp) && (itr < trmax)) { /* check first 10 bytes to look for ebcdic header, if found, this probably indicates a tape switch */ /* tascii_((unsigned char*)&tr, &hdr_buf, 10, 0); if ((strncmp(hdr_buf, "C 1 CLIENT", 10) == 0) || (strncmp(hdr_buf, "C CLIENT ", 10) == 0) || (strncmp(hdr_buf, "C 1 ", 4) == 0)) { fprintf(stderr," % efread(tmp_buf, 1, 3600 - HDRBYTES, ifp); } else { */ /* read in the trace data */ if(tr.ns==0) tr.ns = nt; if(ntg==0) { nsamp = tr.ns * 4; } else { nsamp = nt * 4; } efread((char *)&tr + HDRBYTES, 1, nsamp, ifp); ibt = 0; /* Check bh.hns with tr.ns */ if (bh.hns != tr.ns) { nsflag = true; ibt = 1; nbt = nbt + 1; /* print warning message */ if(verbose==1 || nbt<1000) warn("discrepant tr.ns = %d with bh.hns = %d\n" "\t... noted on trace %d", tr.ns, bh.hns, itr + 1); /* if user wants to leave things the way they are (nt=0) */ /* otherwise, modify number of samples per trace */ if (nt != 0) { if (nt > tr.ns) { for (i = tr.ns; i < nt; i++) tr.data[i] = 0.0; } nsamp = nt * 4; tr.ns = nt; } } /* convert and write desired traces */ if (++itr >= trmin) { /* Convert IBM floats to native floats */ if (convert) conv_float((char *)tr.data, (char *)tr.data, tr.ns, 1); /* write the trace to disk */ if(nmap==0) { /* clean up trace header beyond 180 bytes */ if (clean == 1) bzero((char *)&tr + 180, 60); if (ibt==0 || rmbadtrace==0) efwrite((char *)&tr, 1, nsamp + HDRBYTES, stdout); } else { bcopy((char*)&tr,(char*)&tro,nsamp+HDRBYTES); for(imap=0;imap<nmap;imap++) { ibs = ibstart[imap]; iby = ibyte[imap]; ity = itype[imap]; obs = obstart[imap]; oby = obyte[imap]; oty = otype[imap]; /* fprintf(stderr,"ibs=%d iby=%d ity=%d obs=%d oby=%d oty=%d \n", ibs,iby,ity,obs,oby,oty); */ if(iby==oby && ity==oty && ity!=1 ) { bcopy((char*)&tr+ibs-1,(char*)&tro+obs-1,iby); } else { if(ity==1) { conv_float((char*)&tr+ibs-1,(char*)&tmp,1,1); } else { if(iby==2) { bcopy((char*)&tr+ibs-1,(char*)&itmp2,iby); tmp = itmp2; } else if(iby==4) { bcopy((char*)&tr+ibs-1,(char*)&itmp4,iby); tmp = itmp4; } } if(oty==1) { bcopy((char*)&tmp,(char*)&tro+obs-1,oby); } else { tmp = tmp + 0.5; if(oby==2) { itmp2 = (short) tmp; bcopy((char*)&itmp2,(char*)&tro+obs-1,oby); } else { itmp4 = (int) tmp; bcopy((char*)&itmp4,(char*)&tro+obs-1,oby); } } } } /* clean up trace header beyond 180 bytes */ if (clean == 1) bzero((char *)&tro + 180, 60); if (ibt==0 || rmbadtrace==0) efwrite((char *)&tro, 1, nsamp + HDRBYTES, stdout); } /* echo under verbose option */ if (verbose && itr % 20 == 0) warn(" %d traces from tape", itr); } /* } */ } /* while loop */ /* re-iterate error in case not seen during run */ if ((nsflag) && (nt != 0)) warn("discrepancy found in header and trace ns values\n" "\theader value (%d) was used to extract traces", bh.hns); /* clean up */ efclose(ifp); if(nmap>0) { free(ibstart); free(ibyte); free(itype); free(obstart); free(obyte); free(otype); } return EXIT_SUCCESS; }
void sql_blobInsert(const char *tabname, const char *colname, int rowid, const char *filename, void *offset, int length) { char blobcmd[100]; SQLINTEGER output_length; bool isfile; int fd; /* basic sanity checks */ checkConnect(); if(isnullstring(tabname)) errorPrint("2blobInsert, null table name"); if(isnullstring(colname)) errorPrint("2blobInsert, null column name"); if(rowid <= 0) errorPrint("2invalid rowid in blobInsert"); if(length < 0) errorPrint("2invalid length in blobInsert"); if(strlen(tabname) + strlen(colname) + 42 >= sizeof (blobcmd)) errorPrint("@internal blobInsert command too long"); isfile = true; if(isnullstring(filename)) { isfile = false; if(!offset) errorPrint("2blobInsert is given null filename and null buffer"); } else { offset = blobbuf; fd = eopen(filename, O_RDONLY | O_BINARY, 0); length = fileSizeByHandle(fd); if(length == 0) { isfile = false; close(fd); } } /* set up the blob insert command, using one host variable */ sprintf(blobcmd, "update %s set %s = %s where rowid = %d", tabname, colname, (length ? "?" : "NULL"), rowid); stmt_text = blobcmd; debugStatement(); newStatement(); rv_lastNrows = 0; output_length = length; rc = SQL_SUCCESS; if(isfile) { output_length = SQL_LEN_DATA_AT_EXEC(length); rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARCHAR, length, 0, blobcmd, length, &output_length); if(rc) close(fd); } else if(length) { rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_BINARY, SQL_LONGVARCHAR, length, 0, offset, length, &output_length); } if(errorTrap(0)) { SQLFreeHandle(SQL_HANDLE_STMT, hstmt); return; } rc = SQLExecDirect(hstmt, blobcmd, SQL_NTS); SQLRowCount(hstmt, &rv_lastNrows); if(isfile) { if(rc != SQL_NEED_DATA) { close(fd); if(rc == SQL_SUCCESS) errorPrint("@blobInsert expected SQL_NEED_DATA"); errorTrap(0); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); return; } output_length = 0; rc = SQLParamData(hstmt, (void **)&output_length); if((char *)output_length != blobcmd) { close(fd); errorPrint("2blobInsert got bad key from SQLParamData"); } lseek(fd, 0L, 0); while(length) { int n = length; if(n > sizeof (blobbuf)) n = sizeof (blobbuf); if(read(fd, blobbuf, n) != n) { close(fd); errorPrint("2cannot read file %s, errno %d", filename, errno); } length -= n; rc = SQLPutData(hstmt, blobbuf, n); if(rc) { close(fd); errorTrap(0); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); return; } } /* loop reading the file */ close(fd); /* since there are no more exec-time parameters, * this call completes the execution of the SQL statement. */ rc = SQLParamData(hstmt, (void **)&output_length); } if(errorTrap(0)) { SQLFreeHandle(SQL_HANDLE_STMT, hstmt); return; } if(sql_debug) appendFile(sql_debuglog, "%d rows affected", rv_lastNrows); if(sql_debug2) printf("%d rows affected\n", rv_lastNrows); SQLFreeHandle(SQL_HANDLE_STMT, hstmt); exclist = 0; } /* sql_blobInsert */