[BACK]Return to file.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / builtin

Annotation of OpenXM_contrib2/asir2000/builtin/file.c, Revision 1.1.1.1

1.1       noro        1: /* $OpenXM: OpenXM/src/asir99/builtin/file.c,v 1.2 1999/11/23 07:14:14 noro Exp $ */
                      2: #include "ca.h"
                      3: #include "parse.h"
                      4: #include "base.h"
                      5: #include "genpari.h"
                      6:
                      7: #if defined(VISUAL)
                      8: #include <windows.h>
                      9: /* #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\WinECgen\\1.00.000" */
                     10: #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10"
                     11: #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31"
                     12: #endif
                     13:
                     14: void Pget_rootdir();
                     15: void Paccess(),Premove_file();
                     16: void Pbsave_enc(), Pbload_enc();
                     17:
                     18: void Pload(), Pwhich(), Ploadfiles(), Poutput();
                     19: void Pbsave(), Pbload(), Pbload27();
                     20: void Pbsave_compat(), Pbload_compat();
                     21: void Pbsave_cmo(), Pbload_cmo();
                     22:
                     23: extern int des_encryption;
                     24: extern char *asir_libdir;
                     25:
                     26: struct ftab file_tab[] = {
                     27:        {"remove_file",Premove_file,1},
                     28:        {"access",Paccess,1},
                     29:        {"load",Pload,-1},
                     30:        {"which",Pwhich,1},
                     31:        {"loadfiles",Ploadfiles,1},
                     32:        {"output",Poutput,-1},
                     33:        {"bsave",Pbsave,2},
                     34:        {"bload",Pbload,1},
                     35:        {"get_rootdir",Pget_rootdir,0},
                     36:        {"bsave_enc",Pbsave_enc,2},
                     37:        {"bload_enc",Pbload_enc,1},
                     38:        {"bload27",Pbload27,1},
                     39:        {"bsave_compat",Pbsave_compat,2},
                     40:        {"bload_compat",Pbload_compat,1},
                     41:        {"bsave_cmo",Pbsave_cmo,2},
                     42:        {"bload_cmo",Pbload_cmo,1},
                     43:        {0,0,0},
                     44: };
                     45:
                     46: void Pload(arg,rp)
                     47: NODE arg;
                     48: Q *rp;
                     49: {
                     50:        int ret = 0;
                     51:        char *name,*name0;
                     52:        char errbuf[BUFSIZ];
                     53:
                     54: #if defined THINK_C
                     55:        if ( !arg ) {
                     56:                ret = finder_loadfile();
                     57:        } else
                     58: #endif
                     59:        if ( ARG0(arg) ) {
                     60:                switch (OID(ARG0(arg))) {
                     61:                        case O_STR:
                     62:                                name0 = BDY((STRING)ARG0(arg));
                     63:                                searchasirpath(name0,&name);
                     64:                                if ( !name ) {
                     65:                                        sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
                     66:                                        error(errbuf);
                     67:                                }
                     68:                                ret = loadfile(name);
                     69:                                if ( !ret ) {
                     70:                                        sprintf(errbuf,"load : \"%s\" could not be loaded",name);
                     71:                                        error(errbuf);
                     72:                                }
                     73:                                break;
                     74:                        default:
                     75:                                error("load : invalid argument");
                     76:                                break;
                     77:                }
                     78:        }
                     79:        STOQ(ret,*rp);
                     80: }
                     81:
                     82: void Pwhich(arg,rp)
                     83: NODE arg;
                     84: STRING *rp;
                     85: {
                     86:        char *name;
                     87:        STRING str;
                     88:
                     89:        switch (OID(ARG0(arg))) {
                     90:                case O_STR:
                     91:                        searchasirpath(BDY((STRING)ARG0(arg)),&name);
                     92:                        break;
                     93:                default:
                     94:                        name = 0;
                     95:                        break;
                     96:        }
                     97:        if ( name ) {
                     98:                MKSTR(str,name); *rp = str;
                     99:        } else
                    100:                *rp = 0;
                    101: }
                    102:
                    103: void Ploadfiles(arg,rp)
                    104: NODE arg;
                    105: Q *rp;
                    106: {
                    107:        int ret;
                    108:
                    109:        if ( ARG0(arg) )
                    110:                if ( OID(ARG0(arg)) != O_LIST )
                    111:                        ret = 0;
                    112:                else
                    113:                        ret = loadfiles(BDY((LIST)ARG0(arg)));
                    114:        else
                    115:                ret = 0;
                    116:        STOQ(ret,*rp);
                    117: }
                    118:
                    119: void Poutput(arg,rp)
                    120: NODE arg;
                    121: Q *rp;
                    122: {
                    123: #if PARI
                    124:        extern FILE *outfile;
                    125: #endif
                    126:        FILE *fp;
                    127:
                    128:        fflush(asir_out);
                    129:        if ( asir_out != stdout )
                    130:                fclose(asir_out);
                    131:        switch ( argc(arg) ) {
                    132:                case 0:
                    133:                        fp = stdout; break;
                    134:                case 1:
                    135:                        asir_assert(ARG0(arg),O_STR,"output");
                    136:                        fp = fopen(((STRING)ARG0(arg))->body,"a+");
                    137:                        if ( !fp )
                    138:                                error("output : invalid filename");
                    139:                        break;
                    140:        }
                    141: #if PARI
                    142:        pari_outfile =
                    143: #endif
                    144:        asir_out = fp;
                    145:        *rp = ONE;
                    146: }
                    147:
                    148: extern int ox_file_io;
                    149:
                    150: void Pbsave(arg,rp)
                    151: NODE arg;
                    152: Q *rp;
                    153: {
                    154:        FILE *fp;
                    155:        VL vl,t;
                    156:
                    157:        asir_assert(ARG1(arg),O_STR,"bsave");
                    158:        get_vars_recursive(ARG0(arg),&vl);
                    159:        for ( t = vl; t; t = NEXT(t) )
                    160:                if ( t->v->attr == (pointer)V_UC )
                    161:                        error("bsave : not implemented");
                    162: #if defined(THINK_C) || defined(VISUAL)
                    163:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    164: #else
                    165:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    166: #endif
                    167:        if ( !fp )
                    168:                error("bsave : invalid filename");
                    169:        ox_file_io = 1; /* network byte order is used */
                    170:        savevl(fp,vl);
                    171:        saveobj(fp,ARG0(arg));
                    172:        fclose(fp);
                    173:        ox_file_io = 0;
                    174:        *rp = ONE;
                    175: }
                    176:
                    177: void Pbload(arg,rp)
                    178: NODE arg;
                    179: Obj *rp;
                    180: {
                    181:        FILE *fp;
                    182:
                    183:        asir_assert(ARG0(arg),O_STR,"bload");
                    184: #if defined(THINK_C) || defined(VISUAL)
                    185:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    186: #else
                    187:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    188: #endif
                    189:        if ( !fp )
                    190:                error("bload : invalid filename");
                    191:        ox_file_io = 1; /* network byte order is used */
                    192:        loadvl(fp);
                    193:        loadobj(fp,rp);
                    194:        fclose(fp);
                    195:        ox_file_io = 0;
                    196: }
                    197:
                    198: void Pbsave_cmo(arg,rp)
                    199: NODE arg;
                    200: Q *rp;
                    201: {
                    202:        FILE *fp;
                    203:        VL vl,t;
                    204:
                    205:        asir_assert(ARG1(arg),O_STR,"bsave_cmo");
                    206: #if defined(THINK_C) || defined(VISUAL)
                    207:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    208: #else
                    209:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    210: #endif
                    211:        if ( !fp )
                    212:                error("bsave_cmo : invalid filename");
                    213:        ox_file_io = 1; /* network byte order is used */
                    214:        write_cmo(fp,ARG0(arg));
                    215:        fclose(fp);
                    216:        ox_file_io = 0;
                    217:        *rp = ONE;
                    218: }
                    219:
                    220: void Pbload_cmo(arg,rp)
                    221: NODE arg;
                    222: Obj *rp;
                    223: {
                    224:        FILE *fp;
                    225:
                    226:        asir_assert(ARG0(arg),O_STR,"bload_cmo");
                    227: #if defined(THINK_C) || defined(VISUAL)
                    228:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    229: #else
                    230:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    231: #endif
                    232:        if ( !fp )
                    233:                error("bload_cmo : invalid filename");
                    234:        ox_file_io = 1; /* network byte order is used */
                    235:        read_cmo(fp,rp);
                    236:        fclose(fp);
                    237:        ox_file_io = 0;
                    238: }
                    239:
                    240: #if defined(VISUAL)
                    241: void get_rootdir(name,len)
                    242: char *name;
                    243: int len;
                    244: {
                    245:        LONG    ret;
                    246:        HKEY    hOpenKey;
                    247:        DWORD   Type;
                    248:
                    249:        name[0] = 0;
                    250:        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
                    251:                KEY_QUERY_VALUE, &hOpenKey);
                    252:        if ( ret != ERROR_SUCCESS )
                    253:                ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
                    254:                        KEY_QUERY_VALUE, &hOpenKey);
                    255:        if( ret == ERROR_SUCCESS ) {
                    256:                RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
                    257:                RegCloseKey(hOpenKey);
                    258:        }
                    259: }
                    260: #else
                    261: void get_rootdir(name,len)
                    262: char *name;
                    263: int len;
                    264: {
                    265:        strcpy(name,asir_libdir);
                    266: }
                    267: #endif
                    268:
                    269: void Pget_rootdir(rp)
                    270: STRING *rp;
                    271: {
                    272:        static struct oSTRING rootdir;
                    273:        static char DirName[BUFSIZ];
                    274:        int DirNameLen;
                    275:
                    276:        if ( !rootdir.body ) {
                    277:                get_rootdir(DirName,sizeof(DirName));
                    278:                rootdir.id = O_STR;
                    279:                rootdir.body = DirName;
                    280:        }
                    281:        *rp = &rootdir;
                    282: }
                    283:
                    284: void Pbsave_enc(arg,rp)
                    285: NODE arg;
                    286: Obj *rp;
                    287: {
                    288:        init_deskey();
                    289:        des_encryption = 1;
                    290:        Pbsave(arg,rp);
                    291:        des_encryption = 0;
                    292: }
                    293:
                    294: void Pbload_enc(arg,rp)
                    295: NODE arg;
                    296: Obj *rp;
                    297: {
                    298:        init_deskey();
                    299:        des_encryption = 1;
                    300:        Pbload(arg,rp);
                    301:        des_encryption = 0;
                    302: }
                    303:
                    304: void Pbload27(arg,rp)
                    305: NODE arg;
                    306: Obj *rp;
                    307: {
                    308:        FILE *fp;
                    309:        Obj r;
                    310:
                    311:        asir_assert(ARG0(arg),O_STR,"bload27");
                    312: #if defined(THINK_C) || defined(VISUAL)
                    313:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    314: #else
                    315:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    316: #endif
                    317:        if ( !fp )
                    318:                error("bload : invalid filename");
                    319:        loadvl(fp);
                    320:        loadobj(fp,&r);
                    321:        fclose(fp);
                    322:        bobjtoobj(BASE27,r,rp);
                    323: }
                    324:
                    325: void Pbsave_compat(arg,rp)
                    326: NODE arg;
                    327: Q *rp;
                    328: {
                    329:        FILE *fp;
                    330:        VL vl,t;
                    331:
                    332:        asir_assert(ARG1(arg),O_STR,"bsave_compat");
                    333:        get_vars_recursive(ARG0(arg),&vl);
                    334:        for ( t = vl; t; t = NEXT(t) )
                    335:                if ( t->v->attr == (pointer)V_UC )
                    336:                        error("bsave : not implemented");
                    337: #if defined(THINK_C) || defined(VISUAL)
                    338:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    339: #else
                    340:        fp = fopen(BDY((STRING)ARG1(arg)),"w");
                    341: #endif
                    342:        if ( !fp )
                    343:                error("bsave : invalid filename");
                    344:        /* indicator of an asir32 file */
                    345:        putw(0,fp); putw(0,fp);
                    346:        savevl(fp,vl);
                    347:        saveobj(fp,ARG0(arg));
                    348:        fclose(fp);
                    349:        *rp = ONE;
                    350: }
                    351:
                    352: void Pbload_compat(arg,rp)
                    353: NODE arg;
                    354: Obj *rp;
                    355: {
                    356:        FILE *fp;
                    357:        unsigned int hdr[2];
                    358:        Obj r;
                    359:        int c;
                    360:
                    361:        asir_assert(ARG0(arg),O_STR,"bload_compat");
                    362: #if defined(THINK_C) || defined(VISUAL)
                    363:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    364: #else
                    365:        fp = fopen(BDY((STRING)ARG0(arg)),"r");
                    366: #endif
                    367:        if ( !fp )
                    368:                error("bload : invalid filename");
                    369:        fread(hdr,sizeof(unsigned int),2,fp);
                    370:        if ( !hdr[0] && !hdr[1] ) {
                    371:                /* asir32 file or asir27 0 */
                    372:                c = fgetc(fp);
                    373:                if ( c == EOF ) {
                    374:                        /* asir27 0 */
                    375:                        *rp = 0;
                    376:                } else {
                    377:                        /* asir32 file */
                    378:                        ungetc(c,fp);
                    379:                        loadvl(fp);
                    380:                        loadobj(fp,rp);
                    381:                }
                    382:        } else {
                    383:                /* asir27 file */
                    384:                rewind(fp);
                    385:                loadvl(fp);
                    386:                loadobj(fp,&r);
                    387:                bobjtoobj(BASE27,r,rp);
                    388:        }
                    389:        fclose(fp);
                    390: }
                    391:
                    392: void Premove_file(arg,rp)
                    393: NODE arg;
                    394: Q *rp;
                    395: {
                    396:        unlink((char *)BDY((STRING)ARG0(arg)));
                    397:        *rp = ONE;
                    398: }
                    399:
                    400: void Paccess(arg,rp)
                    401: NODE arg;
                    402: Q *rp;
                    403: {
                    404:        char *name;
                    405:        STRING str;
                    406:
                    407: #if defined(VISUAL)
                    408:        if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
                    409: #else
                    410:        if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
                    411: #endif
                    412:                *rp = ONE;
                    413:        else
                    414:                *rp = 0;
                    415: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>