⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileio.c

📁 《汇编源代码大全》
💻 C
📖 第 1 页 / 共 5 页
字号:
ulg dostime(y, n, d, h, m, s)int y;                  /* year */int n;                  /* month */int d;                  /* day */int h;                  /* hour */int m;                  /* minute */int s;                  /* second *//* Convert the date y/n/d and time h:m:s to a four byte DOS date and   time (date in high two bytes, time in low two bytes allowing magnitude   comparison). */{  return y < 1980 ? dostime(1980, 1, 1, 0, 0, 0) :        (((ulg)y - 1980) << 25) | ((ulg)n << 21) | ((ulg)d << 16) |        ((ulg)h << 11) | ((ulg)m << 5) | ((ulg)s >> 1);}local ulg unix2dostime(t)statime *t;             /* unix time to convert *//* Return the Unix time t in DOS format, rounded up to the next two   second boundary. */{  struct tm *s;         /* result of localtime() */  s = localtime(t);             /* Use local time since MSDOS does */  inctime(s);                   /* Add one second to round up */  return dostime(s->tm_year + 1900, s->tm_mon + 1, s->tm_mday,                 s->tm_hour, s->tm_min, s->tm_sec);}ulg filetime(f, a, n)char *f;                /* name of file to get info on */ulg *a;                 /* return value: file attributes */long *n;                /* return value: file size *//* If file *f does not exist, return 0.  Else, return the file's last   modified date and time as an MSDOS date and time.  The date and   time is returned in a long with the date most significant to allow   unsigned integer comparison of absolute times.  Also, if a is not   a NULL pointer, store the file attributes there, with the high two   bytes being the Unix attributes, and the low byte being a mapping   of that to DOS attributes.  If n is not NULL, store the file size   there.   If f is "-", use standard input as the file. If f is a device, return   a file size of -1 */{  struct stat s;        /* results of stat() */  char name[FNMAX];  int len = strlen(f);  if (f == label) {    if (a != NULL)      *a = label_mode;    if (n != NULL)      *n = -2L; /* convention for a label name */    return label_time;  /* does not work for unknown reason */  }  strcpy(name, f);  if (name[len - 1] == '/')    name[len - 1] = 0;   /* not all systems allow stat'ing a file with / appended */  if (strcmp(f, "-") == 0) {#if defined(AMIGA) && !defined(__SASC_60)  /* forge stat values for stdin since Amiga has no fstat() */    s.st_mode = (S_IREAD|S_IWRITE|S_IFREG);     s.st_size = -1;    s.st_mtime = time(&s.st_mtime);#else /* !AMIGA */    if (fstat(fileno(stdin), &s) != 0)      error("fstat(stdin)");#endif /* ?AMIGA */  } else if ((#ifdef S_IFLNK             linkput ? lstat(name, &s) :#endif             SSTAT(name, &s)) != 0)             /* Accept about any file kind including directories              * (stored with trailing / with -r option)              */    return 0;  if (a != NULL) {#if defined(MSDOS) || defined(OS2) || defined(__human68k__)    *a = ((ulg)s.st_mode << 16) | (ulg)GetFileMode(name);#else    *a = ((ulg)s.st_mode << 16) | !(s.st_mode & S_IWRITE);    if ((s.st_mode & S_IFDIR) != 0) {      *a |= MSDOS_DIR_ATTR;    }#endif  }  if (n != NULL)    *n = (s.st_mode & S_IFMT) != S_IFREG ? -1L : s.st_size;#ifdef OS2  return GetFileTime(name);#else /* !OS2 */#  ifdef VMS     return unix2dostime(&s.st_ctime);   /* Use creation time in VMS */#  else /* !VMS */#    ifdef ATARI_ST       return s.st_mtime; /* Turbo C doesn't use UNIX times */#    else#      ifdef WIN32         return GetTheFileTime(name);#      else         return unix2dostime((statime*)&s.st_mtime);#      endif /* WIN32 */#    endif#  endif /* ?VMS */#endif /* ?OS2 */}#ifdef TOPS20#  include <monsym.h>   /* Get amazing monsym() macro */#  define       _FBBYV  monsym(".FBBYV")#  define         FBBSZ_S       -24     /* Obsolete, replace by FLDGET! */#  define         FBBSZ_M       077     /* ditto */extern int _gtjfn(), _rljfn(), _gtfdb(), stat();int set_extra_field(z)  struct zlist *z;  /* create extra field and change z->att if desired */{  int jfn;  translate_eol = 0;  jfn = _gtjfn(z->name, O_RDONLY);  z->att = (((_gtfdb (jfn, _FBBYV) << FBBSZ_S) & FBBSZ_M) != 8) ?           ASCII :BINARY;  _rljfn(jfn);  return 0;}#else /* !TOPS20 */# if !defined(OS2) && !defined(VMS)int set_extra_field(z)  struct zlist *z;  /* create extra field and change z->att if desired */{  return (int)(z-z);}# endif /* !OS2 && !VMS */#endif /* TOPS20 */int issymlnk(a)ulg a;                  /* Attributes returned by filetime() *//* Return true if the attributes are those of a symbolic link */{#ifdef S_IFLNK  return ((a >> 16) & S_IFMT) == S_IFLNK;#else /* !S_IFLNK */  return (int)a & 0;    /* avoid warning on unused parameter */#endif /* ?S_IFLNK */}int deletedir(d)char *d;                /* directory to delete *//* Delete the directory *d if it is empty, do nothing otherwise.   Return the result of rmdir(), delete(), or system().   For VMS, d must be in format [x.y]z.dir;1  (not [x.y.z]). */{#if (defined(MACOS) || defined(TOPS20))    warn("deletedir not implemented yet", "");    return 127;#else# ifdef RMDIR    /* code from Greg Roelofs, who horked it from Mark Edwards (unzip) */    int r, len;    char *s;              /* malloc'd string for system command */    len = strlen(d);    if ((s = malloc(len + 34)) == NULL)      return 127;#  ifdef VMS    system(strcat(strcpy(s, "set prot=(o:rwed) "), d));    r = delete(d);#  else /* !VMS */    sprintf(s, "IFS=\" \t\n\" /bin/rmdir %s 2>/dev/null", d);    r = system(s);#  endif /* ?VMS */    free(s);    return r;# else /* !RMDIR */    return rmdir(d);# endif /* ?RMDIR */#endif /* ? MACOS || TOPS20 */}#endif /* !UTIL */int destroy(f)char *f;                /* file to delete *//* Delete the file *f, returning non-zero on failure. */{  return unlink(f);}int replace(d, s)char *d, *s;            /* destination and source file names *//* Replace file *d by file *s, removing the old *s.  Return an error code   in the ZE_ class. This function need not preserve the file attributes,   this will be done by setfileattr() later. */{  struct stat t;        /* results of stat() */  int copy = 0;  int d_exists;#ifdef VMS  /* stat() is broken on VMS remote files (accessed through Decnet).   * This patch allows creation of remote zip files, but is not sufficient   * to update them or compress remote files */  unlink(d);#else  d_exists = (LSTAT(d, &t) == 0);  if (d_exists)  {    /*     * respect existing soft and hard links!     */    if (t.st_nlink > 1# ifdef S_IFLNK        || (t.st_mode & S_IFMT) == S_IFLNK# endif        )       copy = 1;    else if (unlink(d))       return ZE_CREAT;                 /* Can't erase zip file--give up */  }#endif /* VMS */  if (!copy) {      if (link(s, d)) {               /* Just move s on top of d */          copy = 1;                     /* failed ? */#if !defined(VMS) && !defined(ATARI_ST) && !defined(AZTEC_C)    /* For VMS & ATARI & AMIGA Aztec assume failure is EXDEV */          if (errno != EXDEV#  ifdef ENOTSAM           && errno != ENOTSAM /* Used at least on Turbo C */#  endif              ) return ZE_CREAT;#endif      }#ifndef link    /* UNIX link() */      /*       * Assuming a UNIX link(2), we still have to remove s.       * If link has been #defined to rename(), nothing to do.       */      else {# ifdef KEEP_OWNER          if (d_exists)              /* this will fail if the user isn't priviledged */              chown(d, t.st_uid, t.st_gid);# endif          unlink(s);      }#endif          /* ?UNIX link() */  }  if (copy) {    FILE *f, *g;      /* source and destination files */    int r;            /* temporary variable */    if ((f = fopen(s, FOPR)) == NULL) {      fprintf(stderr," replace: can't open %s\n", s);      return ZE_TEMP;    }    if ((g = fopen(d, FOPW)) == NULL)    {      fclose(f);      return ZE_CREAT;    }    r = fcopy(f, g, (ulg)-1L);    fclose(f);    if (fclose(g) || r != ZE_OK)    {      unlink(d);      return r ? (r == ZE_TEMP ? ZE_WRITE : r) : ZE_WRITE;    }    unlink(s);  }  return ZE_OK;}int getfileattr(f)char *f;                /* file path *//* Return the file attributes for file f or 0 if failure */{  struct stat s;  return SSTAT(f, &s) == 0 ? s.st_mode : 0;}int setfileattr(f, a)char *f;                /* file path */int a;                  /* attributes returned by getfileattr() *//* Give the file f the attributes a, return non-zero on failure */{#if defined(MACOS) || defined(TOPS20)  return 0;#else  return chmod(f, a);#endif}char *tempname(zip)  char *zip;              /* path name of zip file to generate temp name for *//* Return a temporary file name in its own malloc'ed space, using tempath. */{  char *t = zip;   /* malloc'ed space for name (use zip to avoid warning) */  if (tempath != NULL)  {    if ((t = malloc(strlen(tempath)+12)) == NULL)      return NULL;    strcpy(t, tempath);#if (!defined(VMS) && !defined(TOPS20))#  ifdef AMIGA    {          char c = t[strlen(t)-1];          if (c != '/' && c != ':')            strcat(t, "/");    }#  else /* !AMIGA */              if (t[strlen(t)-1] != '/')      strcat(t, "/");#  endif  /* ?AMIGA */            #endif  }  else  {    if ((t = malloc(12)) == NULL)      return NULL;    *t = 0;  }#ifdef NO_MKTEMP  {    char *p = t + strlen(t);    sprintf(p, "%08lx", (ulg)time(NULL));    return t;  }#else  strcat(t, "_ZXXXXXX");  return mktemp(t);#endif}int fcopy(f, g, n)FILE *f, *g;            /* source and destination files */ulg n;                  /* number of bytes to copy or -1 for all *//* Copy n bytes from file *f to file *g, or until EOF if n == -1.  Return   an error code in the ZE_ class. */{  char *b;              /* malloc'ed buffer for copying */  extent k;             /* result of fread() */  ulg m;                /* bytes copied so far */  if ((b = malloc(CBSZ)) == NULL)    return ZE_MEM;  m = 0;  while (n == -1L || m < n)  {    if ((k = fread(b, 1, n == -1 ?                   CBSZ : (n - m < CBSZ ? (extent)(n - m) : CBSZ), f)) == 0)      if (ferror(f))      {        free((voidp *)b);        return ZE_READ;      }      else        break;    if (fwrite(b, 1, k, g) != k)    {      free((voidp *)b);      fprintf(stderr," fcopy: write error\n");      return ZE_TEMP;    }    m += k;  }  free((voidp *)b);  return ZE_OK;}#ifdef ZMEM/************************//*  Function memset()  *//************************//* * memset - for systems without it *  bill davidsen - March 1990 */char *memset(buf, init, len)register char *buf;     /* buffer loc */register int init;      /* initializer */register unsigned int len;   /* length of the buffer */{    char *start;    start = buf;    while (len--) *(buf++) = init;    return(start);}/************************//*  Function memcpy()  *//************************/char *memcpy(dst,src,len)           /* v2.0f */register char *dst, *src;register unsigned int len;{    char *start;    start = dst;    while (len--)        *dst++ = *src++;    return(start);}/************************//*  Function memcmp()  *//************************/intmemcmp(b1,b2,len)                     /* jpd@usl.edu -- 11/16/90 */register char *b1, *b2;register unsigned int len;{    if (len) do {             /* examine each byte (if any) */      if (*b1++ != *b2++)        return (*((uch *)b1-1) - *((uch *)b2-1));  /* exit when miscompare */       } while (--len);    return(0);        /* no miscompares, yield 0 result */}#endif  /* ZMEM */#ifdef TOPS20intstrupper(s)     /* Returns s in uppercase */char *s;        /* String to be uppercased */{    char    *p;    p = s;    for (; *p; p++)        *p = toupper (*p);}intstrlower(s)     /* Returns s in lowercase. */char *s;        /* String to be lowercased */{    char    *p;    p = s;    for (; *p; p++)        *p = tolower (*p);}#endif /* TOPS20 */#if defined(__TURBOC__) && !defined(OS2)/************************//*  Functio

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -