?? pathutil.c
字號:
/* * Copyright (c) 2000, 2001 by Martin C. Shepherd. * * All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, and/or sell copies of the Software, and to permit persons * to whom the Software is furnished to do so, provided that the above * copyright notice(s) and this permission notice appear in all copies of * the Software and that both the above copyright notice(s) and this * permission notice appear in supporting documentation. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL * INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Except as contained in this notice, the name of a copyright holder * shall not be used in advertising or otherwise to promote the sale, use * or other dealings in this Software without prior written authorization * of the copyright holder. */#include <stdio.h>#include <stdlib.h>#include <limits.h>#include <errno.h>#include <string.h>#include <ctype.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include "pathutil.h"/*....................................................................... * Create a new PathName object. * * Output: * return PathName * The new object, or NULL on error. */PathName *_new_PathName(void){ PathName *path; /* The object to be returned *//* * Allocate the container. */ path = (PathName *)malloc(sizeof(PathName)); if(!path) { fprintf(stderr, "_new_PathName: Insufficient memory.\n"); return NULL; };/* * Before attempting any operation that might fail, initialize the * container at least up to the point at which it can safely be passed * to _del_PathName(). */ path->name = NULL; path->dim = 0;/* * Figure out the maximum length of an expanded pathname. */ path->dim = _pu_pathname_dim(); if(path->dim == 0) return _del_PathName(path);/* * Allocate the pathname buffer. */ path->name = (char *)malloc(path->dim * sizeof(char)); if(!path->name) { fprintf(stderr, "_new_PathName: Insufficient memory to allocate pathname buffer.\n"); return _del_PathName(path); }; return path;}/*....................................................................... * Delete a PathName object. * * Input: * path PathName * The object to be deleted. * Output: * return PathName * The deleted object (always NULL). */PathName *_del_PathName(PathName *path){ if(path) { if(path->name) free(path->name); free(path); }; return NULL;}/*....................................................................... * Return the pathname to a zero-length string. * * Input: * path PathName * The pathname container. * Output: * return char * The cleared pathname buffer, or NULL on error. */char *_pn_clear_path(PathName *path){/* * Check the arguments. */ if(!path) { fprintf(stderr, "_pn_clear_path: NULL argument.\n"); return NULL; }; path->name[0] = '\0'; return path->name;}/*....................................................................... * Append a string to a pathname, increasing the size of the pathname * buffer if needed. * * Input: * path PathName * The pathname container. * string const char * The string to be appended to the pathname. * Note that regardless of the slen argument, * this should be a '\0' terminated string. * slen int The maximum number of characters to append * from string[], or -1 to append the whole * string. * remove_escapes int If true, remove the backslashes that escape * spaces, tabs, backslashes etc.. * Output: * return char * The pathname string path->name[], which may * have been reallocated, or NULL if there was * insufficient memory to extend the pathname. */char *_pn_append_to_path(PathName *path, const char *string, int slen, int remove_escapes){ int pathlen; /* The length of the pathname */ int i;/* * Check the arguments. */ if(!path || !string) { fprintf(stderr, "_pn_append_to_path: NULL argument(s).\n"); return NULL; };/* * Get the current length of the pathname. */ pathlen = strlen(path->name);/* * How many characters should be appended? */ if(slen < 0 || slen > strlen(string)) slen = strlen(string);/* * Resize the pathname if needed. */ if(!_pn_resize_path(path, pathlen + slen)) return NULL;/* * Append the string to the output pathname, removing any escape * characters found therein. */ if(remove_escapes) { int is_escape = 0; for(i=0; i<slen; i++) { is_escape = !is_escape && string[i] == '\\'; if(!is_escape) path->name[pathlen++] = string[i]; };/* * Terminate the string. */ path->name[pathlen] = '\0'; } else {/* * Append the string directly to the pathname. */ memcpy(path->name + pathlen, string, slen); path->name[pathlen + slen] = '\0'; }; return path->name;}/*....................................................................... * Prepend a string to a pathname, increasing the size of the pathname * buffer if needed. * * Input: * path PathName * The pathname container. * string const char * The string to be prepended to the pathname. * Note that regardless of the slen argument, * this should be a '\0' terminated string. * slen int The maximum number of characters to prepend * from string[], or -1 to append the whole * string. * remove_escapes int If true, remove the backslashes that escape * spaces, tabs, backslashes etc.. * Output: * return char * The pathname string path->name[], which may * have been reallocated, or NULL if there was * insufficient memory to extend the pathname. */char *_pn_prepend_to_path(PathName *path, const char *string, int slen, int remove_escapes){ int pathlen; /* The length of the pathname */ int shift; /* The number of characters to shift the suffix by */ int i,j;/* * Check the arguments. */ if(!path || !string) { fprintf(stderr, "_pn_prepend_to_path: NULL argument(s).\n"); return NULL; };/* * Get the current length of the pathname. */ pathlen = strlen(path->name);/* * How many characters should be appended? */ if(slen < 0 || slen > strlen(string)) slen = strlen(string);/* * Work out how far we need to shift the original path string to make * way for the new prefix. When removing escape characters, we need * final length of the new prefix, after unescaped backslashes have * been removed. */ if(remove_escapes) { int is_escape = 0; for(shift=0,i=0; i<slen; i++) { is_escape = !is_escape && string[i] == '\\'; if(!is_escape) shift++; }; } else { shift = slen; };/* * Resize the pathname if needed. */ if(!_pn_resize_path(path, pathlen + shift)) return NULL;/* * Make room for the prefix at the beginning of the string. */ memmove(path->name + shift, path->name, pathlen+1);/* * Copy the new prefix into the vacated space at the beginning of the * output pathname, removing any escape characters if needed. */ if(remove_escapes) { int is_escape = 0; for(i=j=0; i<slen; i++) { is_escape = !is_escape && string[i] == '\\'; if(!is_escape) path->name[j++] = string[i]; };
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -