ftp.nice.ch/Attic/openStep/implementation/gnustep/sources/objcX-0.87.tgz#/objcX-0.87/streams/streams.c

This is streams.c in view mode; [Download] [Up]

/*
    streams - Definitions and macros for streams package 

    Copyright (C) 1994. Adam Fedor

    streams.c,v 1.4 1995/12/13 22:36:05 fedor Exp

    Right now this is just a simple package based on stdio meant to 
    provide compile compatability with NeXT's streams package.

 */
#include <streams/streams.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>


/* This routine is a bit simplistic.  "rw" doesn't exist on sun unix */
/*
static const char *
type_for_mode(int mode)
{
    char *type = NULL;

    if (mode == NX_READONLY)
	type = "r";
    else if (mode == NX_WRITEONLY)
	type = "w";
    else if (mode == NX_READWRITE)
        type = "rw";
    
    return type;
}
*/

char *NXGetTempFilename( char *name, int pos)
    /* description: This function creates a unique file name by
       altering the "name" argument it is passed.  It replaces the six
       character starting at the "pos"th position within "name" with
       digits it generates; it then checks whether the file name is
       unique.  If it is, the file name is returned; if not, different
       digits are tried until a unique name is found.  It is similar
       to the standard C function mktemp(), except that it can leave
       suffixed intact since you specify the location of the character
       that get replaced.
       */
    
{
    FILE *file;
    
    int random,tries=100;
    
    if (pos < 0 || pos > (strlen(name)-6)) return NULL;

    random = (int) time(NULL);
    do {
        if (!tries--) return NULL;
        random %= 1000000;
        sprintf(&name[pos],"%06d",random);
        file = fopen(name,"r");
        if (file != NULL) fclose(file);
        random++;
    } while (file != NULL);

    return name;
}


NXStream *
NXOpenFile(int fd, int mode)
{
    NXStream 	*stream = NULL;

    /*
    const char*	type;

    type = type_for_mode(mode);
    if (type)
        stream = fdopen(fd, type);
        */
    if (mode == NX_READONLY)
        stream = fdopen(fd, "r");
    else if (mode == NX_WRITEONLY)
        stream = fdopen(fd, "w");
    else if (mode == NX_READWRITE) {
/* READWRITE should work whether or not the file exists already. */
/* here, if it doesn't exist, it is created. */       
        if ((stream = fdopen(fd, "r+")) == NULL) {
            stream = fdopen(fd, "w+");
        }
    }

    return stream;
}

/* Need a real streams package for this */
/* THIS IS NOT IMPLEMENTED CORRECTLY, but it should work for a while */
NXStream *
NXOpenMemory(const char *addr, int size, int mode)
{
    char tempname[20];

    strcpy(tempname,"/tmp/NXStreamXXXXXX");
    NXGetTempFilename(tempname,13);
    return NXMapFile(tempname,mode);
}

NXStream *
NXMapFile(const char *name, int mode)
{
    NXStream 	*stream = NULL;

    /*
    const char*	type;

    type = type_for_mode(mode);
    if (type)
        stream = fopen(name, type);
        */
    if (mode == NX_READONLY)
        stream = fopen(name, "r");
    else if (mode == NX_WRITEONLY)
        stream = fopen(name, "w");
    else if (mode == NX_READWRITE) {
/* READWRITE should work whether or not the file exists already. */
/* here, if it doesn't exist, it is created. */       
        if ((stream = fopen(name, "r+")) == NULL) {
            stream = fopen(name, "w+");
        }
    }
    

    return stream;
}

/* Need a real streams package for this */
/* THIS IS NOT IMPLEMENTED CORRECTLY, but it should work for a while */
int 
NXSaveToFile(NXStream *s, const char *name)
{
    FILE *outputfile;
    long temppos;
    char byte;
    
    
    if ((outputfile = fopen(name,"w")) == NULL)
        return -1;
    temppos = ftell(s);
    rewind(s);
    while ( (byte=getc(s))!= EOF) {
        putc(byte,outputfile);
    }
    fseek(s,temppos,0);
    return fclose(outputfile)==0?0:-1;
    
}

/* Need a real streams package for this */
/* THIS IS NOT IMPLEMENTED CORRECTLY, but it should work for a while */
void 
NXGetMemoryBuffer(NXStream *s, char **addr, int *len, int *maxlen)
{
}

/* Need a real streams package for this */
void 
NXCloseMemory(NXStream *s, int option)
{
    NXClose(s);
}

void useredit(const char *filename)
{
    char buffer[256];
    char *editorname = NULL;
    
    if (editorname == NULL) {
        editorname = getenv("EDITOR");
        if (editorname == NULL) {
            editorname = "/bin/vi";
        }
    }
    
    sprintf(buffer,"%s %s",editorname,filename);
    system(buffer);
}


These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.