📄 lylocal.c
字号:
} else if ((dir_info.st_mode & S_IFMT) == S_IFDIR) { _statusline( "There is already a directory with that name! Request ignored."); sleep(AlertSecs); } else if ((dir_info.st_mode & S_IFMT) == S_IFREG) { _statusline( "There is already a file with that name! Request ignored."); sleep(AlertSecs); } else { _statusline( "The specified name is already in use! Request ignored."); sleep(AlertSecs); } } } return 0;}/* * Change the location of a file or directory. */PRIVATE BOOLEAN modify_location ARGS1( char *, testpath){ int mode; char *cp; dev_t dev; ino_t inode; uid_t owner; char tmpbuf[1024]; char newpath[512]; char savepath[512]; struct stat dir_info; char *args[5]; /* * Determine the status of the selected item. */ testpath = strip_trailing_slash(testpath); if (stat(testpath, &dir_info) == -1) { sprintf(tmpbuf, "Unable to get status of '%s'.", testpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } /* * Change the location of the file or directory. */ if ((dir_info.st_mode & S_IFMT) == S_IFDIR) { cp = "Enter new location for directory: "; } else if ((dir_info.st_mode & S_IFMT) == S_IFREG) { cp = "Enter new location for file: "; } else { _statusline( "The specified item is not a file or a directory - request ignored."); sleep(AlertSecs); return 0; } if (filename(cp, tmpbuf, sizeof(tmpbuf)) == NULL) return 0; if (strlen(tmpbuf)) { strcpy(savepath, testpath); strcpy(newpath, testpath); /* * Allow ~/ references to the home directory. */ if (!strncmp(tmpbuf,"~/",2)) { strcpy(newpath, Home_Dir()); strcat(newpath, (tmpbuf + 1)); strcpy(tmpbuf, newpath); } if (tmpbuf[0] != '/') { if ((cp = strrchr(newpath,'/')) != NULL) { *++cp = '\0'; strcat(newpath,tmpbuf); } else { _statusline("Unexpected failure - unable to find trailing \"/\""); sleep(AlertSecs); return 0; } } else { strcpy(newpath,tmpbuf); } /* * Make sure the source and target have the same owner (uid). */ dev = dir_info.st_dev; mode = dir_info.st_mode; inode = dir_info.st_ino; owner = dir_info.st_uid; if (stat(newpath, &dir_info) == -1) { sprintf(tmpbuf,"Unable to get status of '%s'.",newpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } if ((dir_info.st_mode & S_IFMT) != S_IFDIR) { _statusline( "Destination is not a valid directory! Request denied."); sleep(AlertSecs); return 0; } /* * Make sure the source and target are not the same location. */ if (dev == dir_info.st_dev && inode == dir_info.st_ino) { _statusline( "Source and destination are the same location! Request ignored!"); sleep(AlertSecs); return 0; } if (dir_info.st_uid == owner) { sprintf(tmpbuf,"move %s to %s",savepath,newpath); args[0] = "mv"; args[1] = savepath; args[2] = newpath; args[3] = (char *) 0; if (LYExecv(MV_PATH, args, tmpbuf) <= 0) return (-1); return 1; } else { _statusline("Destination has different owner! Request denied."); sleep(AlertSecs); return 0; } } return 0;}/* * Modify name or location of a file or directory on localhost. */PUBLIC BOOLEAN local_modify ARGS2( document *, doc, char **, newpath){ int c, ans; char *cp; char testpath[512]; /* a bit ridiculous */ int count; if (!HTList_isEmpty(tagged)) { cp = doc->address; if (!strncmp(cp, "file://localhost", 16)) { cp += 16; } else if (!strncmp(cp, "file:", 5)) { cp += 5; } strcpy(testpath, cp); HTUnEscapeSome(testpath, "/"); count = modify_tagged(testpath); if (doc->link > (nlinks-count - 1)) doc->link = (nlinks-count - 1); doc->link = (doc->link < 0) ? 0 : doc->link; return count; } else if (doc->link < 0 || doc->link > nlinks) { /* * Added protection. */ return 0; } /* * Do not allow simultaneous change of name and location as in Unix. * This reduces functionality but reduces difficulty for the novice. */#ifdef OK_PERMIT _statusline("Modify name, location, or permission (n, l, or p): ");#else _statusline("Modify name, or location (n or l): ");#endif /* OK_PERMIT */ c = LYgetch(); ans = TOUPPER(c); if (strchr("NLP", ans) != NULL) { cp = links[doc->link].lname; if (!strncmp(cp, "file://localhost", 16)) { cp += 16; } else if(!strncmp(cp, "file:", 5)) { cp += 5; } strcpy(testpath, cp); HTUnEscape(testpath); if (ans == 'N') { return(modify_name(testpath)); } else if (ans == 'L') { if (modify_location(testpath)) { if (doc->link == (nlinks-1)) --doc->link; return 1; }#ifdef OK_PERMIT } else if (ans == 'P') { return(permit_location(NULL, testpath, newpath));#endif /* OK_PERMIT */ } else { /* * Code for changing ownership needed here. */ _statusline("This feature not yet implemented!"); sleep(AlertSecs); } } return 0;}/* * Create a new empty file in the current directory. */PRIVATE BOOLEAN create_file ARGS1( char *, current_location){ char tmpbuf[512]; char testpath[512]; struct stat dir_info; char *args[5]; char *bad_chars = ".~/"; if (filename("Enter name of file to create: ", tmpbuf, sizeof(tmpbuf)) == NULL) { return 0; } if (!no_dotfiles && show_dotfiles) { bad_chars = "~/"; } if (strstr(tmpbuf, "//") != NULL) { _statusline("Illegal redirection \"//\" found! Request ignored."); sleep(AlertSecs); } else if (strlen(tmpbuf) && strchr(bad_chars, tmpbuf[0]) == NULL) { strcpy(testpath,current_location); if (testpath[(strlen(testpath) - 1)] != '/') { strcat(testpath,"/"); } /* * Append the target filename to the current location. */ strcat(testpath, tmpbuf); /* * Make sure the target does not already exist */ if (stat(testpath, &dir_info) == -1) { if (errno != ENOENT) { sprintf(tmpbuf, "Unable to determine status of '%s'.", testpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } sprintf(tmpbuf,"create %s",testpath); args[0] = "touch"; args[1] = testpath; args[2] = (char *) 0; if (LYExecv(TOUCH_PATH, args, tmpbuf) <= 0) return (-1); return 1; } else if ((dir_info.st_mode & S_IFMT) == S_IFDIR) { _statusline( "There is already a directory with that name! Request ignored."); sleep(AlertSecs); } else if ((dir_info.st_mode & S_IFMT) == S_IFREG) { _statusline( "There is already a file with that name! Request ignored."); sleep(AlertSecs); } else { _statusline( "The specified name is already in use! Request ignored."); sleep(AlertSecs); } } return 0;}/* * Create a new directory in the current directory. */PRIVATE BOOLEAN create_directory ARGS1( char *, current_location){ char tmpbuf[512]; char testpath[512]; struct stat dir_info; char *args[5]; char *bad_chars = ".~/"; if (filename("Enter name for new directory: ", tmpbuf, sizeof(tmpbuf)) == NULL) { return 0; } if (!no_dotfiles && show_dotfiles) { bad_chars = "~/"; } if (strstr(tmpbuf, "//") != NULL) { _statusline("Illegal redirection \"//\" found! Request ignored."); sleep(AlertSecs); } else if (strlen(tmpbuf) && strchr(bad_chars, tmpbuf[0]) == NULL) { strcpy(testpath,current_location); if (testpath[(strlen(testpath) - 1)] != '/') { strcat(testpath,"/"); } strcat(testpath, tmpbuf); /* * Make sure the target does not already exist. */ if (stat(testpath, &dir_info) == -1) { if (errno != ENOENT) { sprintf(tmpbuf, "Unable to determine status of '%s'.", testpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } sprintf(tmpbuf,"make directory %s",testpath); args[0] = "mkdir"; args[1] = testpath; args[2] = (char *) 0; if (LYExecv(MKDIR_PATH, args, tmpbuf) <= 0) return (-1); return 1; } else if ((dir_info.st_mode & S_IFMT) == S_IFDIR) { _statusline( "There is already a directory with that name! Request ignored."); sleep(AlertSecs); } else if ((dir_info.st_mode & S_IFMT) == S_IFREG) { _statusline( "There is already a file with that name! Request ignored."); sleep(AlertSecs); } else { _statusline( "The specified name is already in use! Request ignored."); sleep(AlertSecs); } } return 0;}/* * Create a file or a directory at the current location. */PUBLIC BOOLEAN local_create ARGS1( document *, doc){ int c, ans; char *cp; char testpath[512]; _statusline("Create file or directory (f or d): "); c = LYgetch(); ans = TOUPPER(c); cp = doc->address; if (!strncmp(cp, "file://localhost", 16)) { cp += 16; } else if (!strncmp(cp, "file:", 5)) { cp += 5; } strcpy(testpath,cp); HTUnEscape(testpath); if (ans == 'F') { return(create_file(testpath)); } else if (ans == 'D') { return(create_directory(testpath)); } else { return 0; }}/* * Remove a single file or directory. */PRIVATE BOOLEAN remove_single ARGS1( char *, testpath){ int c; char *cp; char tmpbuf[1024]; struct stat dir_info; char *args[5]; /* * lstat() first in case its a symbolic link. */ if (lstat(testpath, &dir_info) == -1 && stat(testpath, &dir_info) == -1) { sprintf(tmpbuf, "System error - failed to get status of '%s'.", testpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } /* * Locate the filename portion of the path. */ if ((cp = strrchr(testpath, '/')) != NULL) { ++cp; } else { cp = testpath; } if ((dir_info.st_mode & S_IFMT) == S_IFDIR) { if (strlen(cp) < 37) { sprintf(tmpbuf, "Remove '%s' and all of its contents (y or n): ", cp); } else { sprintf(tmpbuf, "Remove directory and all of its contents (y or n): "); } } else if ((dir_info.st_mode & S_IFMT) == S_IFREG) { if (strlen(cp) < 60) { sprintf(tmpbuf, "Remove file '%s' (y or n): ", cp); } else { sprintf(tmpbuf, "Remove file (y or n): "); }#ifdef S_IFLNK } else if ((dir_info.st_mode & S_IFMT) == S_IFLNK) { if (strlen(cp) < 50) { sprintf(tmpbuf, "Remove symbolic link '%s' (y or n): ", cp); } else { sprintf(tmpbuf, "Remove symbolic link (y or n): "); }#endif } else { sprintf(tmpbuf, "Unable to determine status of '%s'.", testpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } _statusline(tmpbuf); c = LYgetch(); if (TOUPPER(c) == 'Y') { sprintf(tmpbuf,"remove %s",testpath); args[0] = "rm"; args[1] = "-rf"; args[2] = testpath; args[3] = (char *) 0; if (LYExecv(RM_PATH, args, tmpbuf) <= 0) return (-1); return 1; } return 0;}/* * Remove a file or a directory. */PUBLIC BOOLEAN local_remove ARGS1( document *, doc){ char *cp, *tp; char testpath[512]; int count, i; if (!HTList_isEmpty(tagged)) { count = remove_tagged(); if (doc->link > (nlinks-count - 1)) doc->link = (nlinks-count - 1); doc->link = (doc->link < 0) ? 0 : doc->link; return count; } else if (doc->link < 0 || doc->link > nlinks) { return 0; } cp = links[doc->link].lname; if (is_url(cp) == FILE_URL_TYPE) { tp = cp; if (!strncmp(tp, "file://localhost", 16)) { tp += 16; } else if (!strncmp(tp, "file:", 5)) { tp += 5; } strcpy(testpath, tp); HTUnEscape(testpath); if ((i = strlen(testpath)) && testpath[i - 1] == '/') testpath[(i - 1)] = '\0'; if (remove_single(testpath)) { if (doc->link == (nlinks - 1)) --doc->link; return 1; } } return 0;}#ifdef OK_PERMIT/* * Table of permission strings and chmod values. * Makes the code a bit cleaner. */static struct { char *string_mode; /* Key for value below */ long permit_bits; /* Value for chmod/whatever */} permissions[] = { {"IRUSR", S_IRUSR}, {"IWUSR", S_IWUSR}, {"IXUSR", S_IXUSR}, {"IRGRP", S_IRGRP}, {"IWGRP", S_IWGRP}, {"IXGRP", S_IXGRP}, {"IROTH", S_IROTH}, {"IWOTH", S_IWOTH}, {"IXOTH", S_IXOTH}, {NULL, 0} /* Don't include setuid and friends; use shell access for that. */};#ifndef S_ISDIR#define S_ISDIR(mode) ((mode&0xF000) == 0x4000)#endif /* !S_ISDIR */PRIVATE char LYValidPermitFile[256] = "\0";/* * Handle DIRED permissions. */PRIVATE BOOLEAN permit_location ARGS3( char *, destpath, char *, srcpath, char **, newpath){#ifndef UNIX _statusline("Sorry, don't know how to permit non-UNIX files yet."); sleep(AlertSecs); return(0);#else static char tempfile[256] = "\0"; static BOOLEAN first = TRUE; char *cp; char tmpbuf[LINESIZE]; struct stat dir_info; if (srcpath) { /* * Create form. */ FILE *fp0; char * user_filename; struct group * grp; char * group_name; /* * A couple of sanity tests. */ srcpath = strip_trailing_slash(srcpath); if (strncmp(srcpath, "file://localhost", 16) == 0) srcpath += 16; if (lstat(srcpath, &dir_info) == -1) { sprintf(tmpbuf, "Unable to get status of '%s'.", srcpath); _statusline(tmpbuf); sleep(AlertSecs); return 0; } else if ((dir_info.st_mode & S_IFMT) != S_IFDIR && (dir_info.st_mode & S_IFMT) != S_IFREG) { _statusline( "The specified item is not a file nor a directory - request ignored."); sleep(AlertSecs);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -