📄 smbctool.c
字号:
chdir(".."); pstrcpy(cur_dir,saved_curdir);}/**************************************************************************** View the file using the pager.****************************************************************************/static int cmd_more(void){ pstring rname,lname,pager_cmd; char *pager; int fd; int rc = 0; pstrcpy(rname,cur_dir); pstrcat(rname,"/"); slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir()); fd = smb_mkstemp(lname); if (fd == -1) { d_printf("failed to create temporary file for more\n"); return 1; } close(fd); if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) { d_printf("more <filename>\n"); unlink(lname); return 1; } dos_clean_name(rname); rc = do_get(rname, lname, False); pager=getenv("PAGER"); slprintf(pager_cmd,sizeof(pager_cmd)-1, "%s %s",(pager? pager:PAGER), lname); system(pager_cmd); unlink(lname); return rc;}/**************************************************************************** Do a mget command.****************************************************************************/static int cmd_mget(void){ mode_t mode; pstring mget_mask; pstring buf; char *p=buf; *mget_mask = 0; while (next_token_nr(NULL,p,NULL,sizeof(buf))) { pstrcpy(mget_mask, "smb:"); pstrcat(mget_mask, service); pstrcat(mget_mask,cur_dir); if(mget_mask[strlen(mget_mask)-1]!='/') pstrcat(mget_mask,"/"); if (*p == '/') { pstrcpy(mget_mask, "smb:"); pstrcat(mget_mask, service); pstrcat(mget_mask,p); } else pstrcat(mget_mask,p); /* TODO: enable directories on calls to tool_list once recursion is worked out */ tool_list(mget_mask, mode, do_mget, recurse, recurse); } if (!*mget_mask) { pstrcpy(mget_mask, "smb:"); pstrcat(mget_mask, service); pstrcat(mget_mask,cur_dir); if(mget_mask[strlen(mget_mask)-1]!='/') pstrcat(mget_mask,"/"); pstrcat(mget_mask,"*"); tool_list(mget_mask, mode, do_mget, recurse, recurse); } return 0;}/**************************************************************************** Make a directory of name "name".****************************************************************************/static BOOL do_mkdir(char *name){ if (smbc_mkdir(name, 755) < 0) { d_printf("Error: %s making remote directory %s\n", strerror(errno), name); return False; } return True;}/**************************************************************************** Show 8.3 name of a file.****************************************************************************/static BOOL do_altname(char *name){ pstring altname; if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) { d_printf("%s getting alt name for %s\n", cli_errstr(cli),name); return(False); } d_printf("%s\n", altname); return(True);}/**************************************************************************** Exit client.****************************************************************************/static int cmd_quit(void){ cli_cm_shutdown(); exit(0); /* NOTREACHED */ return 0;}/**************************************************************************** Make a directory.****************************************************************************/static int cmd_mkdir(void){ int dh; pstring mask; pstring buf; pstring targetname; char *p=buf; pstrcpy(mask,cur_dir); if (!next_token_nr(NULL,p,NULL,sizeof(buf))) { if (!recurse) d_printf("mkdir <dirname>\n"); return 1; } pstrcat(mask,p); if (recurse) { pstring ddir; pstring ddir2; *ddir2 = 0; pstrcpy(ddir,mask); trim_char(ddir,'.','\0'); p = strtok(ddir,"/\\"); while (p) { pstrcat(ddir2,p); pstrcpy(targetname, "smb:"); pstrcat(targetname, service); pstrcat(targetname, "/"); pstrcat(targetname, ddir2); DEBUG(3, ("Recursively making directory %s\n", targetname)); if ((dh = smbc_opendir(targetname)) < 0) { if (!do_mkdir(targetname)) return 1; } else smbc_closedir(dh); pstrcat(ddir2,"/"); p = strtok(NULL,"/\\"); } } else { pstrcpy(targetname, "smb:"); pstrcat(targetname, service); pstrcat(targetname, mask); if (!do_mkdir(targetname)) return 1; } return 0;}/**************************************************************************** Show alt name.****************************************************************************/static int cmd_altname(void){ pstring name; pstring buf; char *p=buf; pstrcpy(name,cur_dir); if (!next_token_nr(NULL,p,NULL,sizeof(buf))) { d_printf("altname <file>\n"); return 1; } pstrcat(name,p); do_altname(name); return 0;}/**************************************************************************** Put a single file.****************************************************************************/static int do_put(char *rname, char *lname, BOOL reput){ int fnum; XFILE *f; SMB_OFF_T start = 0; off_t nread = 0; char *buf = NULL; int maxwrite = io_bufsize; int rc = 0; struct timeval tp_start; struct stat stat; GetTimeOfDay(&tp_start); if (reput) { fnum = smbc_open(rname, O_RDWR|O_CREAT, 0644); if (fnum < 0) { d_printf("%s opening remote file %s\n", strerror(errno), rname); return 1; } if (smbc_fstat(fnum, &stat) < 0) { d_printf("%s trying to stat remote file %s\n", strerror(errno), rname); smbc_close(fnum); return 1; } start = stat.st_size; } else { fnum = smbc_creat(rname, 0644); if (fnum < 0) { d_printf("%s trying to create remote file %s\n", strerror(errno), rname); return 1; } } /* allow files to be piped into smbclient jdblair 24.jun.98 Note that in this case this function will exit(0) rather than returning. */ if (!strcmp(lname, "-")) { f = x_stdin; /* size of file is not known */ } else { f = x_fopen(lname,O_RDONLY, 0); if (f && reput) { if (x_tseek(f, start, SEEK_SET) == -1) { d_printf("Error seeking local file\n"); smbc_close(fnum); x_fclose(f); return 1; } } } if (!f) { d_printf("Error opening local file %s\n",lname); smbc_close(fnum); return 1; } DEBUG(1,("putting file %s as %s ",lname,rname)); buf = (char *)SMB_MALLOC(maxwrite); if (!buf) { d_printf("ERROR: Not enough memory!\n"); smbc_close(fnum); if (f != x_stdin) x_fclose(f); return 1; } if (smbc_lseek(fnum, start, SEEK_SET) < 0) { d_printf("%s trying to lseek remote file %s\n", strerror(errno), rname); if (f != x_stdin) x_fclose(f); smbc_close(fnum); SAFE_FREE(buf); return 1; } while (!x_feof(f)) { int n = maxwrite; int ret; if ((n = readfile(buf,n,f)) < 1) { if((n == 0) && x_feof(f)) break; /* Empty local file. */ d_printf("Error reading local file: %s\n", strerror(errno)); rc = 1; break; } ret = smbc_write(fnum, buf, n); if (n != ret) { d_printf("Error writing file: %s\n", strerror(errno)); rc = 1; break; } nread += n; } if (smbc_close(fnum) < 0) { d_printf("%s closing remote file %s\n",strerror(errno),rname); if (f != x_stdin) x_fclose(f); SAFE_FREE(buf); return 1; } if (f != x_stdin) { x_fclose(f); } SAFE_FREE(buf); { struct timeval tp_end; int this_time; GetTimeOfDay(&tp_end); this_time = (tp_end.tv_sec - tp_start.tv_sec)*1000 + (tp_end.tv_usec - tp_start.tv_usec)/1000; put_total_time_ms += this_time; put_total_size += nread; DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n", nread / (1.024*this_time + 1.0e-4), put_total_size / (1.024*put_total_time_ms))); } if (f == x_stdin) { cli_cm_shutdown(); exit(0); } return rc;}/**************************************************************************** Put a file.****************************************************************************/static int cmd_put(void){ pstring lname; pstring rname; pstring buf; char *p=buf; pstrcpy(rname, "smb:"); pstrcat(rname, service); pstrcat(rname, cur_dir); if (!next_token_nr(NULL,p,NULL,sizeof(buf))) { d_printf("put <filename>\n"); return 1; } pstrcpy(lname,p); if (next_token_nr(NULL,p,NULL,sizeof(buf))) pstrcat(rname,p); else pstrcat(rname,lname); { SMB_STRUCT_STAT st; /* allow '-' to represent stdin jdblair, 24.jun.98 */ if (!file_exist(lname,&st) && (strcmp(lname,"-"))) { d_printf("%s does not exist\n",lname); return 1; } } /*d_printf("lname: %s, rname: %s\n", lname, rname);*/ return do_put(rname, lname, False);}/************************************* File list structure.*************************************/static struct file_list { struct file_list *prev, *next; char *file_path; BOOL isdir;} *file_list;/**************************************************************************** Free a file_list structure.****************************************************************************/static void free_file_list (struct file_list * list){ struct file_list *tmp; while (list) { tmp = list; DLIST_REMOVE(list, list); SAFE_FREE(tmp->file_path); SAFE_FREE(tmp); }}/**************************************************************************** Seek in a directory/file list until you get something that doesn't start with the specified name.****************************************************************************/static BOOL seek_list(struct file_list *list, char *name){ while (list) { trim_string(list->file_path,"./","\n"); if (strncmp(list->file_path, name, strlen(name)) != 0) { return(True); } list = list->next; } return(False);}/**************************************************************************** Set the file selection mask.****************************************************************************/static int cmd_select(void){ pstrcpy(fileselection,""); next_token_nr(NULL,fileselection,NULL,sizeof(fileselection)); return 0;}/**************************************************************************** Recursive file matching function act as find match must be always set to True when calling this function****************************************************************************/static int file_find(struct file_list **list, const char *directory, const char *expression, BOOL match){ DIR *dir; struct file_list *entry; struct stat statbuf; int ret; char *path; BOOL isdir; const char *dname; dir = opendir(directory); if (!dir) return -1; while ((dname = readdirname(dir))) { if (!strcmp("..", dname)) continue; if (!strcmp(".", dname)) continue; if (asprintf(&path, "%s/%s", directory, dname) <= 0) { continue; } isdir = False; if (!match || !gen_fnmatch(expression, dname)) { if (recurse) { ret = stat(path, &statbuf); if (ret == 0) { if (S_ISDIR(statbuf.st_mode)) { isdir = True; ret = file_find(list, path, expression, False); } } else { d_printf("file_find: cannot stat file %s\n", path); } if (ret == -1) { SAFE_FREE(path); closedir(dir); return -1; } } entry = SMB_MALLOC_P(struct file_list); if (!entry) { d_printf("Out of memory in file_find\n"); closedir(dir); return -1; } entry->file_path = path; entry->isdir = isdir; DLIST_ADD(*list, entry); } else { SAFE_FREE(path); } } closedir(dir); return 0;}/**************************************************************************** mput some files.****************************************************************************/static int cmd_mput(void){ pstring buf; char *p=buf; while (next_token_nr(NULL,p,NULL,sizeof(buf))) { int ret; struct file_list *temp_list; char *quest, *lname, *rname; file_list = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -