view anagram/support/file.cpp @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1993-1999 Parsifal Software. All Rights Reserved.
 * Copyright 2006 David A. Holland. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * file.cpp
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <limits.h>

#ifdef AG_ON_WINDOWS
#include <direct.h>
#include <share.h>
#endif

#ifdef AG_ON_UNIX
#define SH_DENYNO	0
#define SH_DENYWR	0
#endif

#include "port.h"

#include "agstring.h"
#include "file.h"

//#define INCLUDE_LOGGING
#include "log.h"


AgString work_dir_name;

////////////////////////////////////////////////////////////

/*
 * Open files, compatibly with DOS file sharing.
 * (Does this even mean anything in win32?)
 */

static int open_shared(const char *path, int openflags, int sharingmode) {
  int fd;

  LOGSECTION("open_shared");
  LOGV(path);

#ifdef AG_ON_UNIX
  /*
   * Unix filehandles are always shared, like it or not. We could
   * conceivably use mandatory locking for the denywrite case... but
   * (1) mandatory locking isn't universally supported, (2) it'll
   * likely cause permission problems, and (3) there's really not a
   * whole lot of point.
   */
  (void)sharingmode;
  fd = open(path, openflags, 0664 /* default mode */);
#endif

#ifdef AG_ON_WINDOWS
  fd = sopen(path, openflags, sharingmode);
#endif

  LOGV(fd);
  if (fd < 0) {
    LOGV(strerror(errno));
  }

  return fd;
}

int open_shared_denywrite(const char *path, int openflags) {
  LOGSECTION("open_shared_denywrite");
  return open_shared(path, openflags, SH_DENYWR);
}

int open_shared_any(const char *path, int openflags) {
  LOGSECTION("open_shared_any");
  return open_shared(path, openflags, SH_DENYNO);
}

////////////////////////////////////////////////////////////

time_t getFileTimestamp(const char *name) {
  struct stat statbuf;

  stat(name, &statbuf);
  return statbuf.st_mtime;
}

////////////////////////////////////////////////////////////

void set_work_dir(void) {
  LOGSECTION("set_work_dir");

  // in unix, this only needs to be PATH_MAX, but who knows about windows...
  char temp[PATH_MAX+1];
  getcwd(temp, PATH_MAX);
  work_dir_name = AgString(temp);

  LOGV(work_dir_name);
}