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

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.24    ! noro       48:  * $OpenXM: OpenXM_contrib2/asir2000/builtin/file.c,v 1.23 2006/09/13 02:26:13 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.16      noro       54: #if !defined(VISUAL)
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:
                     61: #if defined(VISUAL)
                     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"
                     68: #endif
                     69:
                     70: void Pget_rootdir();
                     71: void Paccess(),Premove_file();
                     72: void Pbsave_enc(), Pbload_enc();
                     73:
                     74: void Pload(), Pwhich(), Ploadfiles(), Poutput();
                     75: void Pbsave(), Pbload(), Pbload27();
                     76: void Pbsave_compat(), Pbload_compat();
                     77: void Pbsave_cmo(), Pbload_cmo();
1.13      noro       78: void Popen_file(), Pclose_file(), Pget_line(), Pget_byte(), Pput_byte();
1.24    ! noro       79: void Pput_word(), Pget_word();
1.10      noro       80: void Ppurge_stdin();
1.22      noro       81: void Pimport();
1.1       noro       82:
                     83: extern int des_encryption;
                     84: extern char *asir_libdir;
                     85:
                     86: struct ftab file_tab[] = {
1.10      noro       87:        {"purge_stdin",Ppurge_stdin,0},
1.13      noro       88:        {"open_file",Popen_file,-2},
1.3       noro       89:        {"close_file",Pclose_file,1},
1.8       noro       90:        {"get_byte",Pget_byte,1},
1.13      noro       91:        {"put_byte",Pput_byte,2},
1.24    ! noro       92:        {"get_word",Pget_word,1},
        !            93:        {"put_word",Pput_word,2},
1.10      noro       94:        {"get_line",Pget_line,-1},
1.1       noro       95:        {"remove_file",Premove_file,1},
                     96:        {"access",Paccess,1},
                     97:        {"load",Pload,-1},
1.22      noro       98:        {"import",Pimport,-1},
1.1       noro       99:        {"which",Pwhich,1},
                    100:        {"loadfiles",Ploadfiles,1},
                    101:        {"output",Poutput,-1},
                    102:        {"bsave",Pbsave,2},
                    103:        {"bload",Pbload,1},
                    104:        {"get_rootdir",Pget_rootdir,0},
1.6       noro      105: #if defined(VISUAL) && defined(DES_ENC)
1.1       noro      106:        {"bsave_enc",Pbsave_enc,2},
                    107:        {"bload_enc",Pbload_enc,1},
1.6       noro      108: #endif
1.1       noro      109:        {"bload27",Pbload27,1},
                    110:        {"bsave_compat",Pbsave_compat,2},
                    111:        {"bload_compat",Pbload_compat,1},
                    112:        {"bsave_cmo",Pbsave_cmo,2},
                    113:        {"bload_cmo",Pbload_cmo,1},
                    114:        {0,0,0},
                    115: };
1.3       noro      116:
                    117: static FILE *file_ptrs[BUFSIZ];
                    118:
1.15      noro      119: void Ppurge_stdin(Q *rp)
1.10      noro      120: {
                    121:        purge_stdin(stdin);
                    122:        *rp = 0;
                    123: }
                    124:
1.15      noro      125: void Popen_file(NODE arg,Q *rp)
1.3       noro      126: {
                    127:        char *name;
                    128:        FILE *fp;
                    129:        char errbuf[BUFSIZ];
                    130:        int i;
                    131:
                    132:        asir_assert(ARG0(arg),O_STR,"open_file");
                    133:        for ( i = 0; i < BUFSIZ && file_ptrs[i]; i++ );
                    134:        if ( i == BUFSIZ )
                    135:                error("open_file : too many open files");
                    136:        name = BDY((STRING)ARG0(arg));
1.18      takayama  137:        if (strcmp(name,"unix://stdin") == 0) {
                    138:          fp = stdin;
                    139:        }else if (strcmp(name,"unix://stdout") == 0) {
                    140:          fp = stdout;
                    141:     }else if (strcmp(name,"unix://stderr") == 0) {
                    142:          fp = stderr;
                    143:     }else{
                    144:          if ( argc(arg) == 2 ) {
1.13      noro      145:                asir_assert(ARG1(arg),O_STR,"open_file");
                    146:                fp = fopen(name,BDY((STRING)ARG1(arg)));
1.18      takayama  147:          } else
1.13      noro      148:                fp = fopen(name,"r");
1.18      takayama  149:     }
1.3       noro      150:        if ( !fp ) {
1.13      noro      151:                sprintf(errbuf,"open_file : cannot open \"%s\"",name);
1.3       noro      152:                error(errbuf);
                    153:        }
                    154:        file_ptrs[i] = fp;
                    155:        STOQ(i,*rp);
                    156: }
                    157:
1.15      noro      158: void Pclose_file(NODE arg,Q *rp)
1.3       noro      159: {
                    160:        int i;
                    161:
1.8       noro      162:        asir_assert(ARG0(arg),O_N,"close_file");
1.3       noro      163:        i = QTOS((Q)ARG0(arg));
                    164:        if ( file_ptrs[i] ) {
                    165:                fclose(file_ptrs[i]);
                    166:                file_ptrs[i] = 0;
                    167:        } else
                    168:                error("close_file : invalid argument");
                    169:        *rp = ONE;
                    170: }
                    171:
1.15      noro      172: void Pget_line(NODE arg,STRING *rp)
1.3       noro      173: {
                    174:        int i,j,c;
                    175:        FILE *fp;
                    176:        fpos_t head;
                    177:        char *str;
1.10      noro      178:        char buf[BUFSIZ];
                    179:
                    180:        if ( !arg ) {
                    181: #if defined(VISUAL_LIB)
                    182:                get_string(buf,sizeof(buf));
                    183: #else
                    184:                fgets(buf,sizeof(buf),stdin);
                    185: #endif
                    186:                i = strlen(buf);
                    187:                str = (char *)MALLOC_ATOMIC(i+1);
                    188:                strcpy(str,buf);
                    189:                MKSTR(*rp,str);
                    190:                return;
                    191:        }
1.3       noro      192:
1.8       noro      193:        asir_assert(ARG0(arg),O_N,"get_line");
1.3       noro      194:        i = QTOS((Q)ARG0(arg));
                    195:        if ( fp = file_ptrs[i] ) {
                    196:                if ( feof(fp) ) {
                    197:                        *rp = 0;
                    198:                        return;
                    199:                }
                    200:                fgetpos(fp,&head);
                    201:                j = 0;
                    202:                while ( 1 ) {
                    203:                        c = getc(fp);
                    204:                        if ( c == EOF ) {
                    205:                                if ( !j ) {
                    206:                                        *rp = 0;
                    207:                                        return;
                    208:                                } else
                    209:                                        break;
                    210:                        }
                    211:                        j++;
                    212:                        if ( c == '\n' )
                    213:                                break;
                    214:                }
                    215:                fsetpos(fp,&head);
                    216:                str = (char *)MALLOC_ATOMIC(j+1);
                    217:                fgets(str,j+1,fp);
                    218:                MKSTR(*rp,str);
                    219:        } else
                    220:                error("get_line : invalid argument");
1.8       noro      221: }
                    222:
1.15      noro      223: void Pget_byte(NODE arg,Q *rp)
1.8       noro      224: {
                    225:        int i,c;
                    226:        FILE *fp;
                    227:
                    228:        asir_assert(ARG0(arg),O_N,"get_byte");
                    229:        i = QTOS((Q)ARG0(arg));
                    230:        if ( fp = file_ptrs[i] ) {
                    231:                if ( feof(fp) ) {
                    232:                        STOQ(-1,*rp);
                    233:                        return;
                    234:                }
                    235:                c = getc(fp);
                    236:                STOQ(c,*rp);
                    237:        } else
                    238:                error("get_byte : invalid argument");
1.13      noro      239: }
                    240:
1.24    ! noro      241: void Pget_word(NODE arg,Q *rp)
        !           242: {
        !           243:        int i,c;
        !           244:        FILE *fp;
        !           245:
        !           246:        asir_assert(ARG0(arg),O_N,"get_word");
        !           247:        i = QTOS((Q)ARG0(arg));
        !           248:        if ( fp = file_ptrs[i] ) {
        !           249:                if ( feof(fp) ) {
        !           250:                        error("get_word : end of file");
        !           251:                        return;
        !           252:                }
        !           253:                read_int(fp,&c);
        !           254:                STOQ(c,*rp);
        !           255:        } else
        !           256:                error("get_word : invalid argument");
        !           257: }
        !           258:
1.20      noro      259: void Pput_byte(NODE arg,Obj *rp)
1.13      noro      260: {
1.20      noro      261:        int i,j,c;
1.13      noro      262:        FILE *fp;
1.20      noro      263:        Obj obj;
                    264:        TB tb;
1.13      noro      265:
                    266:        asir_assert(ARG0(arg),O_N,"put_byte");
                    267:        i = QTOS((Q)ARG0(arg));
1.20      noro      268:        if ( !(fp = file_ptrs[i]) )
                    269:                error("put_byte : invalid argument");
                    270:
                    271:        obj = (Obj)ARG1(arg);
                    272:        if ( !obj || OID(obj) == O_N ) {
                    273:                c = QTOS((Q)obj);
1.13      noro      274:                putc(c,fp);
1.20      noro      275:        } else if ( OID(obj) == O_STR )
                    276:                fputs(BDY((STRING)obj),fp);
                    277:        else if ( OID(obj) == O_TB ) {
                    278:                tb = (TB)obj;
                    279:                for ( j = 0; j < tb->next; j++ )
                    280:                        fputs(tb->body[j],fp);
                    281:        }
                    282:        *rp = obj;
1.3       noro      283: }
1.1       noro      284:
1.24    ! noro      285: void Pput_word(NODE arg,Obj *rp)
        !           286: {
        !           287:        int i,c;
        !           288:        FILE *fp;
        !           289:        Obj obj;
        !           290:
        !           291:        asir_assert(ARG0(arg),O_N,"put_word");
        !           292:        asir_assert(ARG1(arg),O_N,"put_word");
        !           293:        i = QTOS((Q)ARG0(arg));
        !           294:        if ( !(fp = file_ptrs[i]) )
        !           295:                error("put_word : invalid argument");
        !           296:
        !           297:        obj = (Q)ARG1(arg);
        !           298:        c = QTOS((Q)obj);
        !           299:        write_int(fp,&c);
        !           300:        *rp = obj;
        !           301: }
        !           302:
1.15      noro      303: void Pload(NODE arg,Q *rp)
1.1       noro      304: {
                    305:        int ret = 0;
                    306:        char *name,*name0;
                    307:        char errbuf[BUFSIZ];
                    308:
1.23      noro      309:        if ( !arg ) error("load : invalid argument");
1.1       noro      310:        if ( ARG0(arg) ) {
                    311:                switch (OID(ARG0(arg))) {
                    312:                        case O_STR:
                    313:                                name0 = BDY((STRING)ARG0(arg));
                    314:                                searchasirpath(name0,&name);
                    315:                                if ( !name ) {
                    316:                                        sprintf(errbuf,"load : \"%s\" not found in the search path",name0);
                    317:                                        error(errbuf);
                    318:                                }
1.22      noro      319:                                execasirfile(name);
1.1       noro      320:                                break;
                    321:                        default:
                    322:                                error("load : invalid argument");
                    323:                                break;
                    324:                }
                    325:        }
1.14      noro      326:        STOQ(ret,*rp);
                    327: }
                    328:
1.21      noro      329: NODE imported_files;
                    330:
                    331: void Pimport(NODE arg,Q *rp)
                    332: {
1.22      noro      333:        char *name;
                    334:        NODE t,p,opt;
                    335:
                    336:        name = BDY((STRING)ARG0(arg));
                    337:        for ( t = imported_files; t; t = NEXT(t) )
                    338:                if ( !strcmp((char *)BDY(t),name) ) break;
                    339:        if ( !t ) {
                    340:                Pload(arg,rp);
                    341:                MKNODE(t,name,imported_files);
                    342:                imported_files = t;
                    343:                return;
                    344:        } else if ( current_option ) {
                    345:                for ( opt = current_option; opt; opt = NEXT(opt) ) {
                    346:                        p = BDY((LIST)BDY(opt));
                    347:                        if ( !strcmp(BDY((STRING)BDY(p)),"reimport") && BDY(NEXT(p)) ) {
                    348:                                Pload(arg,rp);
                    349:                                return;
1.21      noro      350:                        }
                    351:                }
1.15      noro      352:        }
1.22      noro      353:        *rp = 0;
1.1       noro      354: }
                    355:
1.15      noro      356: void Pwhich(NODE arg,STRING *rp)
1.1       noro      357: {
                    358:        char *name;
                    359:        STRING str;
                    360:
                    361:        switch (OID(ARG0(arg))) {
                    362:                case O_STR:
                    363:                        searchasirpath(BDY((STRING)ARG0(arg)),&name);
                    364:                        break;
                    365:                default:
                    366:                        name = 0;
                    367:                        break;
                    368:        }
                    369:        if ( name ) {
                    370:                MKSTR(str,name); *rp = str;
                    371:        } else
                    372:                *rp = 0;
                    373: }
                    374:
1.15      noro      375: void Ploadfiles(NODE arg,Q *rp)
1.1       noro      376: {
                    377:        int ret;
                    378:
                    379:        if ( ARG0(arg) )
                    380:                if ( OID(ARG0(arg)) != O_LIST )
                    381:                        ret = 0;
                    382:                else
                    383:                        ret = loadfiles(BDY((LIST)ARG0(arg)));
                    384:        else
                    385:                ret = 0;
                    386:        STOQ(ret,*rp);
                    387: }
                    388:
1.15      noro      389: void Poutput(NODE arg,Q *rp)
1.1       noro      390: {
1.17      ohara     391: #if defined(PARI)
1.1       noro      392:        extern FILE *outfile;
                    393: #endif
                    394:        FILE *fp;
                    395:
                    396:        fflush(asir_out);
                    397:        if ( asir_out != stdout )
                    398:                fclose(asir_out);
                    399:        switch ( argc(arg) ) {
                    400:                case 0:
                    401:                        fp = stdout; break;
                    402:                case 1:
                    403:                        asir_assert(ARG0(arg),O_STR,"output");
                    404:                        fp = fopen(((STRING)ARG0(arg))->body,"a+");
                    405:                        if ( !fp )
                    406:                                error("output : invalid filename");
                    407:                        break;
                    408:        }
1.17      ohara     409: #if defined(PARI)
1.1       noro      410:        pari_outfile =
                    411: #endif
                    412:        asir_out = fp;
                    413:        *rp = ONE;
                    414: }
                    415:
                    416: extern int ox_file_io;
                    417:
1.15      noro      418: void Pbsave(NODE arg,Q *rp)
1.1       noro      419: {
                    420:        FILE *fp;
                    421:        VL vl,t;
                    422:
                    423:        asir_assert(ARG1(arg),O_STR,"bsave");
                    424:        get_vars_recursive(ARG0(arg),&vl);
                    425:        for ( t = vl; t; t = NEXT(t) )
                    426:                if ( t->v->attr == (pointer)V_UC )
                    427:                        error("bsave : not implemented");
                    428:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    429:        if ( !fp )
                    430:                error("bsave : invalid filename");
                    431:        ox_file_io = 1; /* network byte order is used */
                    432:        savevl(fp,vl);
                    433:        saveobj(fp,ARG0(arg));
                    434:        fclose(fp);
                    435:        ox_file_io = 0;
                    436:        *rp = ONE;
                    437: }
                    438:
1.15      noro      439: void Pbload(NODE arg,Obj *rp)
1.1       noro      440: {
                    441:        FILE *fp;
                    442:
                    443:        asir_assert(ARG0(arg),O_STR,"bload");
                    444:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    445:        if ( !fp )
                    446:                error("bload : invalid filename");
                    447:        ox_file_io = 1; /* network byte order is used */
                    448:        loadvl(fp);
                    449:        loadobj(fp,rp);
                    450:        fclose(fp);
                    451:        ox_file_io = 0;
                    452: }
                    453:
1.15      noro      454: void Pbsave_cmo(NODE arg,Q *rp)
1.1       noro      455: {
                    456:        FILE *fp;
                    457:
                    458:        asir_assert(ARG1(arg),O_STR,"bsave_cmo");
                    459:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    460:        if ( !fp )
                    461:                error("bsave_cmo : invalid filename");
                    462:        ox_file_io = 1; /* network byte order is used */
                    463:        write_cmo(fp,ARG0(arg));
                    464:        fclose(fp);
                    465:        ox_file_io = 0;
                    466:        *rp = ONE;
                    467: }
                    468:
1.15      noro      469: void Pbload_cmo(NODE arg,Obj *rp)
1.1       noro      470: {
                    471:        FILE *fp;
                    472:
                    473:        asir_assert(ARG0(arg),O_STR,"bload_cmo");
                    474:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    475:        if ( !fp )
                    476:                error("bload_cmo : invalid filename");
                    477:        ox_file_io = 1; /* network byte order is used */
                    478:        read_cmo(fp,rp);
                    479:        fclose(fp);
                    480:        ox_file_io = 0;
                    481: }
                    482:
1.7       noro      483: static struct oSTRING rootdir;
                    484:
1.1       noro      485: #if defined(VISUAL)
1.15      noro      486: void get_rootdir(char *name,int len)
1.1       noro      487: {
                    488:        LONG    ret;
                    489:        HKEY    hOpenKey;
                    490:        DWORD   Type;
1.7       noro      491:        char    *slash;
1.12      noro      492:
1.7       noro      493:        if ( rootdir.body ) {
                    494:                strcpy(name,rootdir.body);
1.12      noro      495:                return;
                    496:        }
                    497:
                    498:        if ( access("UseCurrentDir",0) >= 0 ) {
                    499:                GetCurrentDirectory(BUFSIZ,name);
                    500:                slash = strrchr(name,'\\');
                    501:                if ( slash )
                    502:                        *slash = 0;
1.7       noro      503:                return;
                    504:        }
1.1       noro      505:        name[0] = 0;
                    506:        ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ECGEN_KEYNAME, 0,
                    507:                KEY_QUERY_VALUE, &hOpenKey);
                    508:        if ( ret != ERROR_SUCCESS )
                    509:                ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, ASIR_KEYNAME, 0,
                    510:                        KEY_QUERY_VALUE, &hOpenKey);
                    511:        if( ret == ERROR_SUCCESS ) {
                    512:                RegQueryValueEx(hOpenKey, "Directory", NULL, &Type, name, &len);
                    513:                RegCloseKey(hOpenKey);
1.7       noro      514:        } else {
                    515:                GetCurrentDirectory(len,name);
                    516:                slash = strrchr(name,'\\');
                    517:                if ( slash )
                    518:                        *slash = 0;
1.1       noro      519:        }
                    520: }
1.7       noro      521:
1.15      noro      522: void set_rootdir(char *name)
1.7       noro      523: {
                    524:        static char DirName[BUFSIZ];
                    525:
                    526:        strcpy(DirName,name);
                    527:        rootdir.id = O_STR;
                    528:        rootdir.body = DirName;
                    529:        asir_libdir = DirName;
                    530:        /* XXX */
                    531:        env_init();
                    532: }
                    533:
1.1       noro      534: #else
1.15      noro      535: void get_rootdir(char *name,int len)
1.1       noro      536: {
                    537:        strcpy(name,asir_libdir);
                    538: }
1.7       noro      539:
1.15      noro      540: void set_rootdir(char *name)
1.7       noro      541: {
                    542:        static char DirName[BUFSIZ];
                    543:
                    544:        strcpy(DirName,name);
                    545:        asir_libdir = DirName;
                    546:        /* XXX */
                    547:        env_init();
                    548: }
                    549:
1.1       noro      550: #endif
                    551:
1.15      noro      552: void Pget_rootdir(STRING *rp)
1.1       noro      553: {
                    554:        static char DirName[BUFSIZ];
                    555:
                    556:        if ( !rootdir.body ) {
                    557:                get_rootdir(DirName,sizeof(DirName));
                    558:                rootdir.id = O_STR;
                    559:                rootdir.body = DirName;
                    560:        }
                    561:        *rp = &rootdir;
                    562: }
                    563:
1.6       noro      564: #if defined(VISUAL) && defined(DES_ENC)
1.15      noro      565: void Pbsave_enc(NODE arg,Obj *rp)
1.1       noro      566: {
                    567:        init_deskey();
                    568:        des_encryption = 1;
                    569:        Pbsave(arg,rp);
                    570:        des_encryption = 0;
                    571: }
                    572:
1.15      noro      573: void Pbload_enc(NODE arg,Obj *rp)
1.1       noro      574: {
                    575:        init_deskey();
                    576:        des_encryption = 1;
                    577:        Pbload(arg,rp);
                    578:        des_encryption = 0;
                    579: }
1.6       noro      580: #endif
1.1       noro      581:
1.15      noro      582: void Pbload27(NODE arg,Obj *rp)
1.1       noro      583: {
                    584:        FILE *fp;
                    585:        Obj r;
                    586:
                    587:        asir_assert(ARG0(arg),O_STR,"bload27");
                    588:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    589:        if ( !fp )
                    590:                error("bload : invalid filename");
                    591:        loadvl(fp);
                    592:        loadobj(fp,&r);
                    593:        fclose(fp);
                    594:        bobjtoobj(BASE27,r,rp);
                    595: }
                    596:
1.15      noro      597: void Pbsave_compat(NODE arg,Q *rp)
1.1       noro      598: {
                    599:        FILE *fp;
                    600:        VL vl,t;
                    601:
                    602:        asir_assert(ARG1(arg),O_STR,"bsave_compat");
                    603:        get_vars_recursive(ARG0(arg),&vl);
                    604:        for ( t = vl; t; t = NEXT(t) )
                    605:                if ( t->v->attr == (pointer)V_UC )
                    606:                        error("bsave : not implemented");
                    607:        fp = fopen(BDY((STRING)ARG1(arg)),"wb");
                    608:        if ( !fp )
                    609:                error("bsave : invalid filename");
                    610:        /* indicator of an asir32 file */
                    611:        putw(0,fp); putw(0,fp);
                    612:        savevl(fp,vl);
                    613:        saveobj(fp,ARG0(arg));
                    614:        fclose(fp);
                    615:        *rp = ONE;
                    616: }
                    617:
1.15      noro      618: void Pbload_compat(NODE arg,Obj *rp)
1.1       noro      619: {
                    620:        FILE *fp;
                    621:        unsigned int hdr[2];
                    622:        Obj r;
                    623:        int c;
                    624:
                    625:        asir_assert(ARG0(arg),O_STR,"bload_compat");
                    626:        fp = fopen(BDY((STRING)ARG0(arg)),"rb");
                    627:        if ( !fp )
                    628:                error("bload : invalid filename");
                    629:        fread(hdr,sizeof(unsigned int),2,fp);
                    630:        if ( !hdr[0] && !hdr[1] ) {
                    631:                /* asir32 file or asir27 0 */
                    632:                c = fgetc(fp);
                    633:                if ( c == EOF ) {
                    634:                        /* asir27 0 */
                    635:                        *rp = 0;
                    636:                } else {
                    637:                        /* asir32 file */
                    638:                        ungetc(c,fp);
                    639:                        loadvl(fp);
                    640:                        loadobj(fp,rp);
                    641:                }
                    642:        } else {
                    643:                /* asir27 file */
                    644:                rewind(fp);
                    645:                loadvl(fp);
                    646:                loadobj(fp,&r);
                    647:                bobjtoobj(BASE27,r,rp);
                    648:        }
                    649:        fclose(fp);
                    650: }
                    651:
1.15      noro      652: void Premove_file(NODE arg,Q *rp)
1.1       noro      653: {
                    654:        unlink((char *)BDY((STRING)ARG0(arg)));
                    655:        *rp = ONE;
                    656: }
                    657:
1.15      noro      658: void Paccess(NODE arg,Q *rp)
1.1       noro      659: {
                    660: #if defined(VISUAL)
                    661:        if ( access(BDY((STRING)ARG0(arg)),04) >= 0 )
                    662: #else
                    663:        if ( access(BDY((STRING)ARG0(arg)),R_OK) >= 0 )
                    664: #endif
                    665:                *rp = ONE;
                    666:        else
                    667:                *rp = 0;
                    668: }
1.9       noro      669:
                    670: #if defined(VISUAL)
                    671: int process_id()
                    672: {
                    673:        return GetCurrentProcessId();
                    674: }
                    675:
1.15      noro      676: void call_exe(char *name,char **av)
1.9       noro      677: {
                    678:        _spawnv(_P_WAIT,name,av);
                    679: }
                    680: #endif

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