/****************************************************************************** * Prim_PCLOSE - close a pipe opened by Prim_POPEN(). * (code stolen from xlfio.c:xclose()) * * syntax: (pclose <stream>) * <stream> is a stream created by popen. * returns T if the command executed successfully, otherwise, * returns the exit status of the opened command. * * Added to XLISP by Niels Mayer ******************************************************************************/ LVAL Prim_PCLOSE() { extern LVAL true; LVAL fptr; int result; /* get file pointer */ fptr = xlgastream(); xllastarg(); /* make sure the file exists */ if (getfile(fptr) == NULL) xlfail("file not open"); /* close the pipe */ result = pclose(getfile(fptr)); if (result == -1) xlfail("<stream> has not been opened with popen"); setfile(fptr,NULL); /* return T if success (exit status 0), else return exit status */ return (result ? cvfixnum(result) : true); }
LVAL xlc_seq_write_smf(void) { seq_type arg1 = getseq(xlgaseq()); FILE * arg2 = getfile(xlgastream()); xllastarg(); seq_write_smf(arg1, arg2); return NIL; }
LVAL xlc_seq_write(void) { seq_type arg1 = getseq(xlgaseq()); FILE * arg2 = getfile(xlgastream()); int arg3 = getboolean(xlgetarg()); xllastarg(); seq_write(arg1, arg2, arg3); return NIL; }
/****************************************************************************** * (FSCANF-FLONUM <stream> <scanf-format>) * This routine calls fscanf(3s) on a <stream> that was previously openend * via open or popen. It will not work on an USTREAM. * <scanf-format> is a format string containing a single conversion * directive that will result in an FLONUM valued conversion. * %e %f or %g are valid conversion specifiers for this routine. * * WARNING: specifying a <scanf-format> that will result in the conversion * of a result larger than sizeof(float) will result in corrupted memory and * core dumps. * * This routine will return a FLONUM if fscanf() returns 1 (i.e. if * the one expected conversion has succeeded. It will return NIL if the * conversion wasn't successful, or if EOF was reached. ******************************************************************************/ LVAL Prim_FSCANF_FLONUM() { LVAL lval_stream; char* fmt; FILE * fp; float result; lval_stream = xlgastream(); if (getfile(lval_stream) == NULL) xlerror("File not opened.", lval_stream); fmt = (char *) getstring(xlgastring()); xllastarg(); /* if scanf returns result <1 then an error or eof occured. */ if (fscanf(getfile(lval_stream), fmt, &result) < 1) return (NIL); else return (cvflonum((FLOTYPE) result)); }
/****************************************************************************** * (FSCANF-FIXNUM <stream> <scanf-format>) * This routine calls fscanf(3s) on a <stream> that was previously openend * via open or popen. It will not work on an USTREAM. * <scanf-format> is a format string containing a single conversion * directive that will result in an integer valued conversion. * %d, %u, %o, %x, %ld, %lu, %lo and %lx style conversions * are acceptable for this routine. * WARNING: specifying a <scanf-format> that will result in the conversion * of a result larger than sizeof(long) will result in corrupted memory and * core dumps. * * This routine will return an FIXNUM if fscanf() returns 1 (i.e. if * the one expected conversion has succeeded. It will return NIL if the * conversion wasn't successful, or if EOF was reached. ******************************************************************************/ LVAL Prim_FSCANF_FIXNUM() { LVAL lval_stream; char* fmt; long result; lval_stream = xlgastream(); if (getfile(lval_stream) == NULL) xlerror("File not opened.", lval_stream); fmt = (char *) getstring(xlgastring()); xllastarg(); result = 0L; /* clear it out hibits incase short is written */ /* if scanf returns result <1 then an error or eof occured. */ if (fscanf(getfile(lval_stream), fmt, &result) < 1) return (NIL); else return (cvfixnum((FIXTYPE) result)); }
/* xclose - close a file */ LVAL xclose(void) { LVAL fptr; /* get file pointer */ fptr = xlgastream(); xllastarg(); /* make sure the file exists */ if (getfile(fptr) == NULL) xlfail("file not open"); /* close the file */ osclose(getfile(fptr)); setfile(fptr,NULL); /* return nil */ return (NIL); }
/****************************************************************************** * (FSCANF-STRING <stream> <scanf-format>) * This routine calls fscanf(3s) on a <stream> that was previously openend * via open or popen. It will not work on an USTREAM. * <scanf-format> is a format string containing a single conversion * directive that will result in a string valued conversion. * %s, %c, and %[...] style conversions are acceptable for * this routine. * WARNING: specifying a <scanf-format> that will result in the conversion * of a result larger than 1024 characters will result in corrupted * memory and core dumps. * * This routine will return a string if fscanf() returns 1 (i.e. if * the one expected conversion has succeeded. It will return NIL if the * conversion wasn't successful, or if EOF was reached. ******************************************************************************/ LVAL Prim_FSCANF_STRING() { LVAL lval_stream; char* fmt; char result[BUFSIZ]; lval_stream = xlgastream(); if (getfile(lval_stream) == NULL) xlerror("File not opened.", lval_stream); fmt = (char *) getstring(xlgastring()); xllastarg(); result[0] = result[1] = '\0'; /* if the conversion is %c, then fscanf doesn't null terminate the string, so do it just incase */ /* if scanf returns result <1 then an error or eof occured. */ if (fscanf(getfile(lval_stream), fmt, result) < 1) return (NIL); else return (cvstring(result)); }