Example #1
0
File: fileio.c Project: barak/lush
const char *cwd(const char *s)
{
#ifdef UNIX
   if (s) {
      errno = 0;
      if (chdir(s)==-1)
         test_file_error(NULL, errno);
   }
#  ifdef HAVE_GETCWD
   assert(getcwd(string_buffer,STRING_BUFFER));
   return mm_strdup(string_buffer);
#  else
   assert(getwd(string_buffer));
   return mm_strdup(string_buffer);
#  endif
#endif
#ifdef WIN32
   char drv[2];
   if (s)
      errno = 0;
      if (_chdir(s)==-1)
         test_file_error(NULL, errno);
   drv[0]='.'; drv[1]=0;
   GetFullPathName(drv, STRING_BUFFER, string_buffer, &s);
   return mm_strdup(string_buffer);
#endif
}
Example #2
0
char *
cwd(const char *s)
{
#ifdef UNIX
  if (s)
    {
      if (chdir(s)==-1)
	test_file_error(NULL);
    }
#ifdef HAVE_GETCWD
  return getcwd(string_buffer,STRING_BUFFER);
#else
  return getwd(string_buffer);
#endif
#endif
#ifdef WIN32
  char drv[2];
  if (s)
    if (_chdir(s)==-1)
      test_file_error(NULL);
  drv[0]='.'; drv[1]=0;
  GetFullPathName(drv, STRING_BUFFER, string_buffer, &s);
  return string_buffer;
#endif
}
Example #3
0
File: fileio.c Project: barak/lush
static int lockfile(const char *filename)
{
   errno = 0;
#ifdef WIN32
   int fd = _open(filename, _O_RDWR|_O_CREAT|_O_EXCL, 0644);
#else
   int fd = open(filename, O_RDWR|O_CREAT|O_EXCL, 0644);
#endif
   if (fd<0) {
      if (errno==EEXIST)
         return 0;
      else
         test_file_error(NULL, errno);
   }
   time_t tl;
   time(&tl);
#ifdef UNIX
   {
      char hname[80];
      char *user;
      gethostname(hname,79);
      if (! (user=getenv("USER")))
      if (! (user=getenv("LOGNAME")))
         user="******";  
      sprintf(string_buffer,"created by %s@%s (pid=%d)\non %s", 
              user, hname, (int)getpid(), ctime(&tl));
  }
#endif
#ifdef WIN32
   {
      char user[80];
      char computer[80];
      int size = sizeof(user);
      if (! (GetUserName(user,&size)))
         strcpy(user,"<unknown>");
      size = sizeof(computer);
      if (! (GetComputerName(computer,&size)))
         strcpy(computer,"<unknown>");
      sprintf(string_buffer,"created by %s@%s on %s", 
              user, computer, time(&tl));
  }
#endif
   char *s = string_buffer;
   size_t n = strlen(s);
   while (n > 0) {
      ssize_t l = write(fd, s, n);
      if (l <= 0) {
	 close(fd);
	 test_file_error(NULL, errno);
      }
      s += l;
      n -= l;
   }
   close(fd);
   return 1;
}
Example #4
0
File: storage.c Project: barak/lush
storage_t *new_storage_mmap(storage_type_t t, FILE *f, size_t offs, bool ro)
{
   storage_t *st = mm_allocv(mt_storage, sizeof(storage_t));
   errno = 0;
#if HAVE_FSEEKO
   if (fseeko(f,(off_t)0,SEEK_END)==-1)
      test_file_error(NULL, errno);
#else
   if (fseek(f,0,SEEK_END)==-1)
      test_file_error(NULL, errno);
#endif
#if HAVE_FTELLO
   size_t len = (size_t)ftello(f);
#else
   size_t len = (size_t)ftell(f);
#endif
   rewind(f);
   if (t==ST_MPTR || t==ST_GPTR)
      RAISEF("cannot mmap a pointer storage", st->backptr);
   if (t==ST_AT)
      RAISEF("cannot mmap an atom-storage", st->backptr);
#ifdef UNIX
   errno = 0;
   gptr addr = mmap(0,len,(ro ? PROT_READ : PROT_WRITE),MAP_SHARED,fileno(f),0);
   if (addr == (void*)-1L)
      test_file_error(NULL, errno);
#endif
#ifdef WIN32
   gptr xtra, addr;
   if (! (xtra = (gptr)CreateFileMapping((HANDLE)(_get_osfhandle(fd)), 
                                         NULL, PAGE_READONLY, 0, len, NULL)))
      RAISEF("cannot create file mapping",NIL);
   if (! (addr = (gptr)MapViewOfFile((HANDLE)(xtra), 
                                     FILE_MAP_READ, 0, 0, size + pos)))
      RAISEF("cannot create view on mapped file",NIL);
   st->mmap_xtra = xtra;
#endif
   st->type = t;
   st->kind = STS_MMAP;
   st->isreadonly = ro;
   st->mmap_len = len;
   st->mmap_addr = addr;
   st->size = (len - offs) / storage_sizeof[st->type];
   st->data = (char *)(st->mmap_addr)+offs;
   st->backptr = new_at(storage_class[st->type], st);
   add_notifier(st, (wr_notify_func_t *)storage_notify, NULL);
   return st;
}
Example #5
0
File: dump.c Project: barak/lush
static void write32(FILE *f, int x)
{
   char c[4];
   c[0] = x>>24;
   c[1] = x>>16;
   c[2] = x>>8;
   c[3] = x;
   errno = 0;
   if (fwrite(&c, sizeof(char), 4, f) != 4)
      test_file_error(f, errno);
}
Example #6
0
File: storage.c Project: barak/lush
void storage_load(storage_t *st, FILE *f)
{
   if (st->type == ST_AT)
      RAISEF("cannot load an AT storage", NIL);
   if (st->data == NULL) {
      assert(st->size == 0);
    
#if HAVE_FSEEKO
      off_t here = ftello(f);
      errno = 0;
      if (fseeko(f,0,SEEK_END)==-1)
         test_file_error(NULL, errno);
      off_t len = ftello(f);
      errno = 0;
      if (fseeko(f,here,SEEK_SET)==-1)
         test_file_error(NULL, errno);
#else
      off_t here = ftell(f);
      errno = 0;
      if (fseek(f,0,SEEK_END)==-1)
         test_file_error(NULL, errno);
      int len = ftell(f);
      errno = 0;
      if (fseek(f,here,SEEK_SET)==-1)
         test_file_error(NULL, errno);
#endif
      if (len==0) 
         return;
      else
         storage_alloc(st,(size_t)len/storage_sizeof[st->type],0);
   }
   
   get_write_permit(st);
   char *pt = st->data;
   errno = 0;
   int nrec = fread(pt, storage_sizeof[st->type], st->size, f);
   if (nrec < st->size)
      RAISEF("file is too small",NIL);
   test_file_error(f, errno);
}
Example #7
0
File: storage.c Project: barak/lush
void storage_save(storage_t *st, FILE *f)
{
   if (st->type == ST_AT)
      RAISEF("cannot save an AT storage",NIL);
   if (st->data == NULL) 
      RAISEF("cannot save an unsized storage",NIL);
  
   char *pt = st->data;
   errno = 0;
   int nrec = fwrite(pt, storage_sizeof[st->type], st->size, f);
   test_file_error(f, errno);
   if (nrec < st->size)
      RAISEF("storage could not be saved completely",NIL);
}
Example #8
0
File: dump.c Project: barak/lush
/* return size of dump file */
static off_t dump(const char *s)
{
   /* Build the big list */
   at *ans = NIL, **where = &ans;
   
   /* 1 - the modules */
   at *p = module_list();
   at *q = p;
   while (CONSP(q)) {
      *where = new_cons(Car(q), NIL);
      where = &Cdr(*where);
      q = Cdr(q);
   }
   /* 2- the globals */
   *where = global_defs();

   /* Header */
   at *atf = OPEN_WRITE(s,"dump");
   FILE *f = Gptr(atf);
   write32(f, DUMPMAGIC);
   write32(f, DUMPVERSION);

   /* The macro character map */
   errno = 0;
   fwrite(char_map,1,256,f);
   test_file_error(f, errno);
   
   /* Write the big list */
   bool oldready = error_doc.ready_to_an_error;
   error_doc.ready_to_an_error = false;
   bwrite(ans, f, true);
   error_doc.ready_to_an_error = oldready;
   lush_delete(atf);     /* close file */

   /* get file size */
   struct stat buf;
   if (stat(s, &buf)>=0)
      if (S_ISREG(buf.st_mode))
         return buf.st_size;
   return (off_t)0;
}