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

1.4       noro        1: /*
                      2:  * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
                      3:  * All rights reserved.
                      4:  *
                      5:  * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
                      6:  * non-exclusive and royalty-free license to use, copy, modify and
                      7:  * redistribute, solely for non-commercial and non-profit purposes, the
                      8:  * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
                      9:  * conditions of this Agreement. For the avoidance of doubt, you acquire
                     10:  * only a limited right to use the SOFTWARE hereunder, and FLL or any
                     11:  * third party developer retains all rights, including but not limited to
                     12:  * copyrights, in and to the SOFTWARE.
                     13:  *
                     14:  * (1) FLL does not grant you a license in any way for commercial
                     15:  * purposes. You may use the SOFTWARE only for non-commercial and
                     16:  * non-profit purposes only, such as academic, research and internal
                     17:  * business use.
                     18:  * (2) The SOFTWARE is protected by the Copyright Law of Japan and
                     19:  * international copyright treaties. If you make copies of the SOFTWARE,
                     20:  * with or without modification, as permitted hereunder, you shall affix
                     21:  * to all such copies of the SOFTWARE the above copyright notice.
                     22:  * (3) An explicit reference to this SOFTWARE and its copyright owner
                     23:  * shall be made on your publication or presentation in any form of the
                     24:  * results obtained by use of the SOFTWARE.
                     25:  * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.5       noro       26:  * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.4       noro       27:  * for such modification or the source code of the modified part of the
                     28:  * SOFTWARE.
                     29:  *
                     30:  * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
                     31:  * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
                     32:  * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
                     33:  * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
                     34:  * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
                     35:  * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
                     36:  * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
                     37:  * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
                     38:  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
                     39:  * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
                     40:  * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
                     41:  * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
                     42:  * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
                     43:  * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
                     44:  * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
                     45:  * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
                     46:  * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
                     47:  *
1.36    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.35 2017/06/10 05:32:24 noro Exp $
1.4       noro       49: */
1.1       noro       50: #include "ca.h"
                     51: #include "parse.h"
1.15      noro       52: #include "ox.h"
1.1       noro       53: #include "base.h"
1.34      fujimoto   54: #if !defined(VISUAL) && !defined(__MINGW32__)
1.2       noro       55: #include "unistd.h"
1.16      noro       56: #endif
1.17      ohara      57: #if defined(PARI)
1.1       noro       58: #include "genpari.h"
1.2       noro       59: #endif
1.1       noro       60:
1.34      fujimoto   61: #if defined(VISUAL) || defined(__MINGW32__)
1.1       noro       62: #include <windows.h>
1.9       noro       63: #include <process.h>
1.15      noro       64: #include <io.h>
1.1       noro       65: /* #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\WinECgen\\1.00.000" */
                     66: #define ECGEN_KEYNAME "SoftWare\\Fujitsu\\FSEcParamGen\\V1.0L10"
                     67: #define ASIR_KEYNAME "SoftWare\\Fujitsu\\Asir\\1999.03.31"
1.27      ohara      68: #define R_OK 0x04
1.1       noro       69: #endif
                     70:
1.32      ohara      71: void Psprintf(NODE,STRING *);
                     72:
1.1       noro       73: void Pget_rootdir();
                     74: void Paccess(),Premove_file();
                     75: void Pbsave_enc(), Pbload_enc();
                     76:
                     77: void Pload(), Pwhich(), Ploadfiles(), Poutput();
                     78: void Pbsave(), Pbload(), Pbload27();
                     79: void Pbsave_compat(), Pbload_compat();
                     80: void Pbsave_cmo(), Pbload_cmo();
1.13      noro       81: void Popen_file(), Pclose_file(), Pget_line(), Pget_byte(), Pput_byte();
1.24      noro       82: void Pput_word(), Pget_word();
1.10      noro       83: void Ppurge_stdin();
1.25      ohara      84: void Pfprintf();
1.22      noro       85: void Pimport();
1.28      noro       86: void Pgetpid();
1.1       noro       87:
                     88: extern int des_encryption;
                     89: extern char *asir_libdir;
                     90:
                     91: struct ftab file_tab[] = {
1.25      ohara      92:        {"fprintf",Pfprintf,-99999999},
1.10      noro       93:        {"purge_stdin",Ppurge_stdin,0},
1.13      noro       94:        {"open_file",Popen_file,-2},
1.3       noro       95:        {"close_file",Pclose_file,1},
1.8       noro       96:        {"get_byte",Pget_byte,1},
1.13      noro       97:        {"put_byte",Pput_byte,2},
1.24      noro       98:        {"get_word",Pget_word,1},
                     99:        {"put_word",Pput_word,2},
1.10      noro      100:        {"get_line",Pget_line,-1},
1.1       noro      101:        {"remove_file",Premove_file,1},
                    102:        {"access",Paccess,1},
                    103:        {"load",Pload,-1},
1.22      noro      104:        {"import",Pimport,-1},
1.1       noro      105:        {"which",Pwhich,1},
                    106:        {"loadfiles",Ploadfiles,1},
                    107:        {"output",Poutput,-1},
                    108:        {"bsave",Pbsave,2},
                    109:        {"bload",Pbload,1},
                    110:        {"get_rootdir",Pget_rootdir,0},
1.27      ohara     111: #if defined(DES_ENC)
1.1       noro      112:        {"bsave_enc",Pbsave_enc,2},
                    113:        {"bload_enc",Pbload_enc,1},
1.6       noro      114: #endif
1.1       noro      115:        {"bload27",Pbload27,1},
                    116:        {"bsave_compat",Pbsave_compat,2},
                    117:        {"bload_compat",Pbload_compat,1},
                    118:        {"bsave_cmo",Pbsave_cmo,2},
                    119:        {"bload_cmo",Pbload_cmo,1},
1.28      noro      120:        {"getpid",Pgetpid,0},
1.1       noro      121:        {0,0,0},
                    122: };
1.3       noro      123:
                    124: static FILE *file_ptrs[BUFSIZ];
                    125:
1.25      ohara     126: void Pfprintf(NODE arg,pointer *rp)
                    127: {
                    128:        FILE *fp;
                    129:        STRING s;
                    130:        asir_assert(ARG0(arg),O_N,"fprintf");
                    131:        fp = file_ptrs[QTOS((Q)ARG0(arg))];
                    132:        if ( !fp ) {
                    133:                error("fprintf : invalid argument");
                    134:        }
                    135:        arg = NEXT(arg);
                    136:        if ( arg ) {
                    137:                Psprintf(arg,&s);
                    138:                fputs(BDY(s),fp);
1.26      ohara     139:                /* fflush(fp); */
1.25      ohara     140:        }
                    141:        *rp = 0;
                    142:        return;
                    143: }
                    144:
1.15      noro      145: void Ppurge_stdin(Q *rp)
1.10      noro      146: {
                    147:        purge_stdin(stdin);
                    148:        *rp = 0;
                    149: }
                    150:
1.15      noro      151: void Popen_file(NODE arg,Q *rp)
1.3       noro      152: {
                    153:        char *name;
                    154:        FILE *fp;
                    155:        char errbuf[BUFSIZ];
                    156:        int i;
                    157:
                    158:        asir_assert(ARG0(arg),O_STR,"open_file");
                    159:        for ( i = 0; i < BUFSIZ && file_ptrs[i]; i++ );
                    160:        if ( i == BUFSIZ )
                    161:                error("open_file : too many open files");
                    162:        name = BDY((STRING)ARG0(arg));
1.18      takayama  163:        if (strcmp(name,"unix://stdin") == 0) {
                    164:          fp = stdin;
                    165:        }else if (strcmp(name,"unix://stdout") == 0) {
                    166:          fp = stdout;
                    167:     }else if (strcmp(name,"unix://stderr") == 0) {
                    168:          fp = stderr;
                    169:     }else{
                    170:          if ( argc(arg) == 2 ) {
1.13      noro      171:                asir_assert(ARG1(arg),O_STR,"open_file");
                    172:                fp = fopen(name,BDY((STRING)ARG1(arg)));
1.18      takayama  173:          } else
1.35      noro      174:                fp = fopen(name,"rb");
1.18      takayama  175:     }
1.3       noro      176:        if ( !fp ) {
1.13      noro      177:                sprintf(errbuf,"open_file : cannot open \"%s\"",name);
1.3       noro      178:                error(errbuf);
                    179:        }
                    180:        file_ptrs[i] = fp;
                    181:        STOQ(i,*rp);
                    182: }
                    183:
1.15      noro      184: void Pclose_file(NODE arg,Q *rp)
1.3       noro      185: {
                    186:        int i;
                    187:
1.8       noro      188:        asir_assert(ARG0(arg),O_N,"close_file");
1.3       noro      189:        i = QTOS((Q)ARG0(arg));
                    190:        if ( file_ptrs[i] ) {
                    191:                fclose(file_ptrs[i]);
                    192:                file_ptrs[i] = 0;
                    193:        } else
                    194:                error("close_file : invalid argument");
                    195:        *rp = ONE;
                    196: }
                    197:
1.15      noro      198: void Pget_line(NODE arg,STRING *rp)
1.3       noro      199: {
                    200:        int i,j,c;
                    201:        FILE *fp;
                    202:        fpos_t head;
                    203:        char *str;
1.10      noro      204:        char buf[BUFSIZ];
                    205:
                    206:        if ( !arg ) {
                    207: #if defined(VISUAL_LIB)
                    208:                get_string(buf,sizeof(buf));
                    209: #else
                    210:                fgets(buf,sizeof(buf),stdin);
                    211: #endif
                    212:                i = strlen(buf);
                    213:                str = (char *)MALLOC_ATOMIC(i+1);
                    214:                strcpy(str,buf);
                    215:                MKSTR(*rp,str);
                    216:                return;
                    217:        }
1.3       noro      218:
1.8       noro      219:        asir_assert(ARG0(arg),O_N,"get_line");
1.3       noro      220:        i = QTOS((Q)ARG0(arg));
                    221:        if ( fp = file_ptrs[i] ) {
                    222:                if ( feof(fp) ) {
                    223:                        *rp = 0;
                    224:                        return;
                    225:                }
                    226:                fgetpos(fp,&head);
                    227:                j = 0;
                    228:                while ( 1 ) {
                    229:                        c = getc(fp);
                    230:                        if ( c == EOF ) {
                    231:                                if ( !j ) {
                    232:                                        *rp = 0;
                    233:                                        return;
                    234:                                } else
                    235:                                        break;
                    236:                        }
                    237:                        j++;
                    238:                        if ( c == '\n' )
                    239:                                break;
                    240:                }
                    241:                fsetpos(fp,&head);
                    242:                str = (char *)MALLOC_ATOMIC(j+1);
                    243:                fgets(str,j+1,fp);
1.36    ! noro      244:                 for ( i = 0; i < j; i++ )
        !           245:                   if ( str[i] == '\r' ) {
        !           246:                      str[i] = '\n';
        !           247:                      str[i+1] = 0;
        !           248:                      break;
        !           249:                   }
1.3       noro      250:                MKSTR(*rp,str);
                    251:        } else
                    252:                error("get_line : invalid argument");
1.8       noro      253: }
                    254:
1.15      noro      255: void Pget_byte(NODE arg,Q *rp)
1.8       noro      256: {
                    257:        int i,c;
                    258:        FILE *fp;
                    259:
                    260:        asir_assert(ARG0(arg),O_N,"get_byte");
                    261:        i = QTOS((Q)ARG0(arg));
                    262:        if ( fp = file_ptrs[i] ) {
                    263:                if ( feof(fp) ) {
                    264:                        STOQ(-1,*rp);
                    265:                        return;
                    266:                }
                    267:                c = getc(fp);
                    268:                STOQ(c,*rp);
                    269:        } else
                    270:                error("get_byte : invalid argument");
1.13      noro      271: }
                    272:
1.24      noro      273: void Pget_word(NODE arg,Q *rp)
                    274: {
                    275:        int i,c;
                    276:        FILE *fp;
                    277:
                    278:        asir_assert(ARG0(arg),O_N,"get_word");
                    279:        i = QTOS((Q)ARG0(arg));
                    280:        if ( fp = file_ptrs[i] ) {
                    281:                if ( feof(fp) ) {
                    282:                        error("get_word : end of file");
                    283:                        return;
                    284:                }
                    285:                read_int(fp,&c);
                    286:                STOQ(c,*rp);
                    287:        } else
                    288:                error("get_word : invalid argument");
                    289: }
                    290:
1.20      noro      291: void Pput_byte(NODE arg,Obj *rp)
1.13      noro      292: {
1.20      noro      293:        int i,j,c;
1.13      noro      294:        FILE *fp;
1.20      noro      295:        Obj obj;
                    296:        TB tb;
1.13      noro      297:
                    298:        asir_assert(ARG0(arg),O_N,"put_byte");
                    299:        i = QTOS((Q)ARG0(arg));
1.20      noro      300:        if ( !(fp = file_ptrs[i]) )
                    301:                error("put_byte : invalid argument");
                    302:
                    303:        obj = (Obj)ARG1(arg);
                    304:        if ( !obj || OID(obj) == O_N ) {
                    305:                c = QTOS((Q)obj);
1.13      noro      306:                putc(c,fp);
1.20      noro      307:        } else if ( OID(obj) == O_STR )
                    308:                fputs(BDY((STRING)obj),fp);
                    309:        else if ( OID(obj) == O_TB ) {
                    310:                tb = (TB)obj;
                    311:                for ( j = 0; j < tb->next; j++ )
                    312:                        fputs(tb->body[j],fp);
                    313:        }
                    314:        *rp = obj;
1.3       noro      315: }
1.1       noro      316:
1.24      noro      317: void Pput_word(NODE arg,Obj *rp)
                    318: {
                    319:        int i,c;
                    320:        FILE *fp;
                    321:        Obj obj;
                    322:
                    323:        asir_assert(ARG0(arg),O_N,"put_word");
                    324:        asir_assert(ARG1(arg),O_N,"put_word");
                    325:        i = QTOS((Q)ARG0(arg));
                    326:        if ( !(fp = file_ptrs[i]) )
                    327:                error("put_word : invalid argument");
                    328:
                    329:        obj = (Q)ARG1(arg);
                    330:        c = QTOS((Q)obj);
                    331:        write_int(fp,&c);
                    332:        *rp = obj;
                    333: }
                    334:
1.15      noro      335: void Pload(NODE arg,Q *rp)
1.1       noro      336: {
                    337:        int ret = 0;
                    338:        char *name,*name0;
                    339:        char errbuf[BUFSIZ];
                    340:
1.23      noro      341:        if ( !arg ) error("load : invalid argument");
1.1       noro      342:        if ( ARG0(arg) ) {
                    343:                switch (OID(ARG0(arg))) {
                    344:                        case O_STR:
                    345:                                name0 = BDY((STRING)ARG0(arg));
                    346:                                searchasirpath(name0,&name);
                    347:                                if ( !name ) {
                    348:                                        sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
                    349:                                        error(errbuf);
                    350:                                }
1.22      noro      351:                                execasirfile(name);
1.1       noro      352:                                break;
                    353:                        default:
                    354:                                error("load : invalid argument");
                    355:                                break;
                    356:                }
                    357:        }
1.14      noro      358:        STOQ(ret,*rp);
                    359: }
                    360:
1.21      noro      361: NODE imported_files;
                    362:
                    363: void Pimport(NODE arg,Q *rp)
                    364: {
1.22      noro      365:        char *name;
                    366:        NODE t,p,opt;
                    367:
                    368:        name = BDY((STRING)ARG0(arg));
                    369:        for ( t = imported_files; t; t = NEXT(t) )
                    370:                if ( !strcmp((char *)BDY(t),name) ) break;
                    371:        if ( !t ) {
                    372:                Pload(arg,rp);
                    373:                MKNODE(t,name,imported_files);
                    374:                imported_files = t;
                    375:                return;
                    376:        } else if ( current_option ) {
                    377:                for ( opt = current_option; opt; opt = NEXT(opt) ) {
                    378:                        p = BDY((LIST)BDY(opt));
                    379:                        if ( !strcmp(BDY((STRING)BDY(p)),"reimport") && BDY(NEXT(p)) ) {
                    380:                                Pload(arg,rp);
                    381:                                return;
1.21      noro      382:                        }
                    383:                }
1.15      noro      384:        }
1.22      noro      385:        *rp = 0;
1.1       noro      386: }
                    387:
1.15      noro      388: void Pwhich(NODE arg,STRING *rp)
1.1       noro      389: {
                    390:        char *name;
                    391:        STRING str;
                    392:
                    393:        switch (OID(ARG0(arg))) {
                    394:                case O_STR:
                    395:                        searchasirpath(BDY((STRING)ARG0(arg)),&name);
                    396:                        break;
                    397:                default:
                    398:                        name = 0;
                    399:                        break;
                    400:        }
                    401:        if ( name ) {
                    402:                MKSTR(str,name); *rp = str;
                    403:        } else
                    404:                *rp = 0;
                    405: }
                    406:
1.15      noro      407: void Ploadfiles(NODE arg,Q *rp)
1.1       noro      408: {
                    409:        int ret;
                    410:
                    411:        if ( ARG0(arg) )
                    412:                if ( OID(ARG0(arg)) != O_LIST )
                    413:                        ret = 0;
                    414:                else
                    415:                        ret = loadfiles(BDY((LIST)ARG0(arg)));
                    416:        else
                    417:                ret = 0;
                    418:        STOQ(ret,*rp);
                    419: }
                    420:
1.15      noro      421: void Poutput(NODE arg,Q *rp)
1.1       noro      422: {
1.17      ohara     423: #if defined(PARI)
1.1       noro      424:        extern FILE *outfile;
                    425: #endif
                    426:        FILE *fp;
                    427:
                    428:        fflush(asir_out);
                    429:        if ( asir_out != stdout )
                    430:                fclose(asir_out);
                    431:        switch ( argc(arg) ) {
                    432:                case 0:
                    433:                        fp = stdout; break;
                    434:                case 1:
                    435:                        asir_assert(ARG0(arg),O_STR,"output");
                    436:                        fp = fopen(((STRING)ARG0(arg))->body,"a+");
                    437:                        if ( !fp )
                    438:                                error("output : invalid filename");
                    439:                        break;
                    440:        }
1.17      ohara     441: #if defined(PARI)
1.1       noro      442:        pari_outfile =
                    443: #endif
                    444:        asir_out = fp;
                    445:        *rp = ONE;
                    446: }
                    447:
                    448: extern int ox_file_io;
                    449:
1.15      noro      450: void Pbsave(NODE arg,Q *rp)
1.1       noro      451: {
                    452:        FILE *fp;
                    453:        VL vl,t;
                    454:
                    455:        asir_assert(ARG1(arg),O_STR,"bsave");
                    456:        get_vars_recursive(ARG0(arg),&vl);
                    457:        for ( t = vl; t; t = NEXT(t) )
                    458:                if ( t->v->attr == (pointer)V_UC )
                    459:                        error("bsave : not implemented");
                    460:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    461:        if ( !fp )
                    462:                error("bsave : invalid filename");
                    463:        ox_file_io = 1; /* network byte order is used */
                    464:        savevl(fp,vl);
                    465:        saveobj(fp,ARG0(arg));
                    466:        fclose(fp);
                    467:        ox_file_io = 0;
                    468:        *rp = ONE;
                    469: }
                    470:
1.15      noro      471: void Pbload(NODE arg,Obj *rp)
1.1       noro      472: {
                    473:        FILE *fp;
                    474:
                    475:        asir_assert(ARG0(arg),O_STR,"bload");
                    476:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    477:        if ( !fp )
                    478:                error("bload : invalid filename");
                    479:        ox_file_io = 1; /* network byte order is used */
                    480:        loadvl(fp);
                    481:        loadobj(fp,rp);
                    482:        fclose(fp);
                    483:        ox_file_io = 0;
                    484: }
                    485:
1.15      noro      486: void Pbsave_cmo(NODE arg,Q *rp)
1.1       noro      487: {
                    488:        FILE *fp;
                    489:
                    490:        asir_assert(ARG1(arg),O_STR,"bsave_cmo");
                    491:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    492:        if ( !fp )
                    493:                error("bsave_cmo : invalid filename");
                    494:        ox_file_io = 1; /* network byte order is used */
                    495:        write_cmo(fp,ARG0(arg));
                    496:        fclose(fp);
                    497:        ox_file_io = 0;
                    498:        *rp = ONE;
                    499: }
                    500:
1.15      noro      501: void Pbload_cmo(NODE arg,Obj *rp)
1.1       noro      502: {
                    503:        FILE *fp;
                    504:
                    505:        asir_assert(ARG0(arg),O_STR,"bload_cmo");
                    506:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    507:        if ( !fp )
                    508:                error("bload_cmo : invalid filename");
                    509:        ox_file_io = 1; /* network byte order is used */
                    510:        read_cmo(fp,rp);
                    511:        fclose(fp);
                    512:        ox_file_io = 0;
                    513: }
                    514:
1.7       noro      515: static struct oSTRING rootdir;
                    516:
1.34      fujimoto  517: #if defined(VISUAL) || defined(__MINGW32__)
1.15      noro      518: void get_rootdir(char *name,int len)
1.1       noro      519: {
                    520:        LONG    ret;
                    521:        HKEY    hOpenKey;
                    522:        DWORD   Type;
1.7       noro      523:        char    *slash;
1.12      noro      524:
1.7       noro      525:        if ( rootdir.body ) {
                    526:                strcpy(name,rootdir.body);
1.12      noro      527:                return;
                    528:        }
1.29      ohara     529:        if ( GetModuleFileName(NULL,name,BUFSIZ) ) {
                    530:                slash = strrchr(name,'\\');
                    531:                *slash = 0;
1.12      noro      532:                slash = strrchr(name,'\\');
1.29      ohara     533:                if ( slash )
1.12      noro      534:                        *slash = 0;
1.7       noro      535:                return;
                    536:        }
1.1       noro      537:        name[0] = 0;
                    538:        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
                    539:                KEY_QUERY_VALUE, &hOpenKey);
                    540:        if ( ret != ERROR_SUCCESS )
                    541:                ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
                    542:                        KEY_QUERY_VALUE, &hOpenKey);
                    543:        if( ret == ERROR_SUCCESS ) {
                    544:                RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
                    545:                RegCloseKey(hOpenKey);
1.7       noro      546:        } else {
                    547:                GetCurrentDirectory(len,name);
                    548:                slash = strrchr(name,'\\');
                    549:                if ( slash )
                    550:                        *slash = 0;
1.1       noro      551:        }
                    552: }
1.7       noro      553:
1.15      noro      554: void set_rootdir(char *name)
1.7       noro      555: {
                    556:        static char DirName[BUFSIZ];
                    557:
                    558:        strcpy(DirName,name);
                    559:        rootdir.id = O_STR;
                    560:        rootdir.body = DirName;
                    561:        asir_libdir = DirName;
                    562:        /* XXX */
                    563:        env_init();
                    564: }
                    565:
1.1       noro      566: #else
1.15      noro      567: void get_rootdir(char *name,int len)
1.1       noro      568: {
                    569:        strcpy(name,asir_libdir);
                    570: }
1.7       noro      571:
1.15      noro      572: void set_rootdir(char *name)
1.7       noro      573: {
                    574:        static char DirName[BUFSIZ];
                    575:
                    576:        strcpy(DirName,name);
                    577:        asir_libdir = DirName;
                    578:        /* XXX */
                    579:        env_init();
                    580: }
                    581:
1.1       noro      582: #endif
                    583:
1.15      noro      584: void Pget_rootdir(STRING *rp)
1.1       noro      585: {
                    586:        static char DirName[BUFSIZ];
                    587:
                    588:        if ( !rootdir.body ) {
                    589:                get_rootdir(DirName,sizeof(DirName));
                    590:                rootdir.id = O_STR;
                    591:                rootdir.body = DirName;
                    592:        }
                    593:        *rp = &rootdir;
                    594: }
                    595:
1.28      noro      596: void Pgetpid(Q *rp)
                    597: {
                    598:        int id;
                    599:
1.34      fujimoto  600: #if defined(VISUAL) || defined(__MINGW32__)
1.28      noro      601:        id = GetCurrentProcessId();
                    602: #else
                    603:        id = getpid();
                    604: #endif
                    605:        STOQ(id,*rp);
                    606: }
                    607:
1.27      ohara     608: #if defined(DES_ENC)
1.15      noro      609: void Pbsave_enc(NODE arg,Obj *rp)
1.1       noro      610: {
                    611:        init_deskey();
                    612:        des_encryption = 1;
                    613:        Pbsave(arg,rp);
                    614:        des_encryption = 0;
                    615: }
                    616:
1.15      noro      617: void Pbload_enc(NODE arg,Obj *rp)
1.1       noro      618: {
                    619:        init_deskey();
                    620:        des_encryption = 1;
                    621:        Pbload(arg,rp);
                    622:        des_encryption = 0;
                    623: }
1.6       noro      624: #endif
1.1       noro      625:
1.15      noro      626: void Pbload27(NODE arg,Obj *rp)
1.1       noro      627: {
                    628:        FILE *fp;
                    629:        Obj r;
                    630:
                    631:        asir_assert(ARG0(arg),O_STR,"bload27");
                    632:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    633:        if ( !fp )
                    634:                error("bload : invalid filename");
                    635:        loadvl(fp);
                    636:        loadobj(fp,&r);
                    637:        fclose(fp);
                    638:        bobjtoobj(BASE27,r,rp);
                    639: }
                    640:
1.15      noro      641: void Pbsave_compat(NODE arg,Q *rp)
1.1       noro      642: {
                    643:        FILE *fp;
                    644:        VL vl,t;
                    645:
                    646:        asir_assert(ARG1(arg),O_STR,"bsave_compat");
                    647:        get_vars_recursive(ARG0(arg),&vl);
                    648:        for ( t = vl; t; t = NEXT(t) )
                    649:                if ( t->v->attr == (pointer)V_UC )
                    650:                        error("bsave : not implemented");
                    651:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    652:        if ( !fp )
                    653:                error("bsave : invalid filename");
                    654:        /* indicator of an asir32 file */
                    655:        putw(0,fp); putw(0,fp);
                    656:        savevl(fp,vl);
                    657:        saveobj(fp,ARG0(arg));
                    658:        fclose(fp);
                    659:        *rp = ONE;
                    660: }
                    661:
1.15      noro      662: void Pbload_compat(NODE arg,Obj *rp)
1.1       noro      663: {
                    664:        FILE *fp;
                    665:        unsigned int hdr[2];
                    666:        Obj r;
                    667:        int c;
                    668:
                    669:        asir_assert(ARG0(arg),O_STR,"bload_compat");
                    670:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    671:        if ( !fp )
                    672:                error("bload : invalid filename");
                    673:        fread(hdr,sizeof(unsigned int),2,fp);
                    674:        if ( !hdr[0] && !hdr[1] ) {
                    675:                /* asir32 file or asir27 0 */
                    676:                c = fgetc(fp);
                    677:                if ( c == EOF ) {
                    678:                        /* asir27 0 */
                    679:                        *rp = 0;
                    680:                } else {
                    681:                        /* asir32 file */
                    682:                        ungetc(c,fp);
                    683:                        loadvl(fp);
                    684:                        loadobj(fp,rp);
                    685:                }
                    686:        } else {
                    687:                /* asir27 file */
                    688:                rewind(fp);
                    689:                loadvl(fp);
                    690:                loadobj(fp,&r);
                    691:                bobjtoobj(BASE27,r,rp);
                    692:        }
                    693:        fclose(fp);
                    694: }
                    695:
1.15      noro      696: void Premove_file(NODE arg,Q *rp)
1.1       noro      697: {
                    698:        unlink((char *)BDY((STRING)ARG0(arg)));
                    699:        *rp = ONE;
                    700: }
                    701:
1.15      noro      702: void Paccess(NODE arg,Q *rp)
1.1       noro      703: {
                    704:        if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
                    705:                *rp = ONE;
                    706:        else
                    707:                *rp = 0;
                    708: }
1.9       noro      709:
1.34      fujimoto  710: #if defined(VISUAL) || defined(__MINGW32__)
1.9       noro      711: int process_id()
                    712: {
                    713:        return GetCurrentProcessId();
                    714: }
                    715:
1.15      noro      716: void call_exe(char *name,char **av)
1.9       noro      717: {
                    718:        _spawnv(_P_WAIT,name,av);
                    719: }
                    720: #endif

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