[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.2

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

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