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

📄 pathutil.c

📁 xorp源码hg
💻 C
📖 第 1 页 / 共 2 页
字号:
	path->name[j++] = string[i];    };  } else {    memcpy(path->name, string, slen);  };  return path->name;}/*....................................................................... * If needed reallocate a given pathname buffer to allow a string of * a given length to be stored in it. * * Input: *  path     PathName *  The pathname container object. *  length     size_t    The required length of the pathname buffer, *                       not including the terminating '\0'. * Output: *  return       char *  The pathname buffer, or NULL if there was *                       insufficient memory (this isn't reported *                       to stderr). */char *_pn_resize_path(PathName *path, size_t length){/* * Check the arguments. */  if(!path) {    fprintf(stderr, "_pn_resize_path: NULL argument(s).\n");    return NULL;  };/* * If the pathname buffer isn't large enough to accomodate a string * of the specified length, attempt to reallocate it with the new * size, plus space for a terminating '\0'. Also add a bit of * head room to prevent too many reallocations if the initial length * turned out to be very optimistic. */  if(length + 1 > path->dim) {    size_t dim =  length + 1 + PN_PATHNAME_INC;    char *name = (char *) realloc(path->name, dim);    if(!name)      return NULL;    path->name = name;    path->dim = dim;  };  return path->name;}/*....................................................................... * Estimate the largest amount of space needed to store a pathname. * * Output: *  return size_t   The number of bytes needed, including space for the *                  terminating '\0'. */size_t _pu_pathname_dim(void){  int maxlen;   /* The return value excluding space for the '\0' *//* * If the POSIX PATH_MAX macro is defined in limits.h, use it. */#ifdef PATH_MAX  maxlen = PATH_MAX;/* * If we have pathconf, use it. */#elif defined(_PC_PATH_MAX)  maxlen = pathconf(FS_ROOT_DIR, _PC_PATH_MAX);  if(maxlen < 0) {    if(errno) {      fprintf(stderr, "pathconf error: %s\n", strerror(errno));      return 0;    };/* * If errno wasn't set then pathconf is telling us that the * pathlength is effectively infinite. We will thus have to * substitute a reasonable guess. */    maxlen = MAX_PATHLEN_FALLBACK;  };/* * None of the above approaches worked, so substitute our fallback * guess. */#else    maxlen = MAX_PATHLEN_FALLBACK;#endif/* * Return the amount of space needed to accomodate a pathname plus * a terminating '\0'. */  return maxlen + 1;}/*....................................................................... * Return non-zero if the specified path name refers to a directory. * * Input: *  pathname  const char *  The path to test. * Output: *  return           int    0 - Not a directory. *                          1 - pathname[] refers to a directory. */int _pu_path_is_dir(const char *pathname){  struct stat statbuf;    /* The file-statistics return buffer *//* * Look up the file attributes. */  if(stat(pathname, &statbuf) < 0)    return 0;/* * Is the file a directory? */  return S_ISDIR(statbuf.st_mode) != 0;}/*....................................................................... * Return non-zero if the specified path name refers to a regular file. * * Input: *  pathname  const char *  The path to test. * Output: *  return           int    0 - Not a regular file. *                          1 - pathname[] refers to a regular file. */int _pu_path_is_file(const char *pathname){  struct stat statbuf;    /* The file-statistics return buffer *//* * Look up the file attributes. */  if(stat(pathname, &statbuf) < 0)    return 0;/* * Is the file a regular file? */  return S_ISREG(statbuf.st_mode) != 0;}/*....................................................................... * Return non-zero if the specified path name refers to an executable. * * Input: *  pathname  const char *  The path to test. * Output: *  return           int    0 - Not an executable file. *                          1 - pathname[] refers to an executable file. */int _pu_path_is_exe(const char *pathname){  struct stat statbuf;    /* The file-statistics return buffer *//* * Look up the file attributes. */  if(stat(pathname, &statbuf) < 0)    return 0;/* * Is the file a regular file which is executable by the current user. */  return S_ISREG(statbuf.st_mode) != 0 &&#ifdef __MINGW32__    1;#else    (statbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) &&    access(pathname, X_OK) == 0;#endif}/*....................................................................... * Search backwards for the potential start of a filename. This * looks backwards from the specified index in a given string, * stopping at the first unescaped space or the start of the line. * * Input: *  string  const char *  The string to search backwards in. *  back_from      int    The index of the first character in string[] *                        that follows the pathname. * Output: *  return        char *  The pointer to the first character of *                        the potential pathname, or NULL on error. */char *_pu_start_of_path(const char *string, int back_from){  int i, j;/* * Check the arguments. */  if(!string || back_from < 0) {    fprintf(stderr, "_pu_start_path: Invalid argument(s).\n");    return NULL;  };/* * Search backwards from the specified index. */  for(i=back_from-1; i>=0; i--) {    int c = string[i];/* * Stop on unescaped spaces. */    if(isspace((int)(unsigned char)c)) {/* * The space can't be escaped if we are at the start of the line. */      if(i==0)        break;/* * Find the extent of the escape characters which precedes the space. */      for(j=i-1; j>=0 && string[j]=='\\'; j--)	;/* * If there isn't an odd number of escape characters before the space, * then the space isn't escaped. */      if((i - 1 - j) % 2 == 0)	break;    };  };  return (char *)string + i + 1;}/*....................................................................... * Find the length of a potential filename starting from a given * point. This looks forwards from the specified index in a given string, * stopping at the first unescaped space or the end of the line. * * Input: *  string   const char *  The string to search backwards in. *  start_from      int    The index of the first character of the pathname *                         in string[]. * Output: *  return         char *  The pointer to the character that follows *                         the potential pathname, or NULL on error. */char *_pu_end_of_path(const char *string, int start_from){  int c;             /* The character being examined */  int escaped = 0;   /* True when the next character is escaped */  int i;/* * Check the arguments. */  if(!string || start_from < 0) {    fprintf(stderr, "_pu_end_path: Invalid argument(s).\n");    return NULL;  };/* * Search forwards from the specified index. */  for(i=start_from; (c=string[i]) != '\0'; i++) {    if(escaped) {      escaped = 0;    } else if(isspace((int)(unsigned char) c)) {      break;    } else if(c == '\\') {      escaped = 1;    };  };  return (char *)string + i;}/*....................................................................... * Return non-zero if the specified path name refers to an existing file. * * Input: *  pathname   const char *  The path to test. * Output: *  return            int    0 - The file doesn't exist. *                           1 - The file does exist. */int _pu_file_exists(const char *pathname){  struct stat statbuf;  return stat(pathname, &statbuf) == 0;}

⌨️ 快捷键说明

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