📄 util.c
字号:
int set_message(char *buf,int num_words,int num_bytes,BOOL zero){ if (zero) memset(buf + smb_size,'\0',num_words*2 + num_bytes); CVAL(buf,smb_wct) = num_words; SSVAL(buf,smb_vwv + num_words*SIZEOFWORD,num_bytes); smb_setlen(buf,smb_size + num_words*2 + num_bytes - 4); return (smb_size + num_words*2 + num_bytes);}/*******************************************************************return the number of smb words********************************************************************/static int smb_numwords(char *buf){ return (CVAL(buf,smb_wct));}/*******************************************************************return the size of the smb_buf region of a message********************************************************************/int smb_buflen(char *buf){ return(SVAL(buf,smb_vwv0 + smb_numwords(buf)*2));}/******************************************************************* return a pointer to the smb_buf data area********************************************************************/static int smb_buf_ofs(char *buf){ return (smb_size + CVAL(buf,smb_wct)*2);}/******************************************************************* return a pointer to the smb_buf data area********************************************************************/char *smb_buf(char *buf){ return (buf + smb_buf_ofs(buf));}/*******************************************************************return the SMB offset into an SMB buffer********************************************************************/int smb_offset(char *p,char *buf){ return(PTR_DIFF(p,buf+4) + chain_size);}/*******************************************************************reduce a file name, removing .. elements.********************************************************************/void dos_clean_name(char *s){ char *p=NULL; DEBUG(3,("dos_clean_name [%s]\n",s)); /* remove any double slashes */ string_sub(s, "\\\\", "\\"); while ((p = strstr(s,"\\..\\")) != NULL) { pstring s1; *p = 0; pstrcpy(s1,p+3); if ((p=strrchr(s,'\\')) != NULL) *p = 0; else *s = 0; pstrcat(s,s1); } trim_string(s,NULL,"\\.."); string_sub(s, "\\.\\", "\\");}/*******************************************************************reduce a file name, removing .. elements. ********************************************************************/void unix_clean_name(char *s){ char *p=NULL; DEBUG(3,("unix_clean_name [%s]\n",s)); /* remove any double slashes */ string_sub(s, "//","/"); /* Remove leading ./ characters */ if(strncmp(s, "./", 2) == 0) { trim_string(s, "./", NULL); if(*s == 0) pstrcpy(s,"./"); } while ((p = strstr(s,"/../")) != NULL) { pstring s1; *p = 0; pstrcpy(s1,p+3); if ((p=strrchr(s,'/')) != NULL) *p = 0; else *s = 0; pstrcat(s,s1); } trim_string(s,NULL,"/..");}/*******************************************************************reduce a file name, removing .. elements and checking that it is below dir in the heirachy. This uses dos_GetWd() and so must be runon the system that has the referenced file system.widelinks are allowed if widelinks is true********************************************************************/#if 0BOOL reduce_name(char *s,char *dir,BOOL widelinks){#ifndef REDUCE_PATHS return True;#else pstring dir2; pstring wd; pstring base_name; pstring newname; char *p=NULL; BOOL relative = (*s != '/'); *dir2 = *wd = *base_name = *newname = 0; if (widelinks) { unix_clean_name(s); /* can't have a leading .. */ if (strncmp(s,"..",2) == 0 && (s[2]==0 || s[2]=='/')) { DEBUG(3,("Illegal file name? (%s)\n",s)); return(False); } if (strlen(s) == 0) pstrcpy(s,"./"); return(True); } DEBUG(3,("reduce_name [%s] [%s]\n",s,dir)); /* remove any double slashes */ string_sub(s,"//","/"); pstrcpy(base_name,s); p = strrchr(base_name,'/'); if (!p) return(True); if (!dos_GetWd(wd)) { DEBUG(0,("couldn't getwd for %s %s\n",s,dir)); return(False); } if (dos_ChDir(dir) != 0) { DEBUG(0,("couldn't chdir to %s\n",dir)); return(False); } if (!dos_GetWd(dir2)) { DEBUG(0,("couldn't getwd for %s\n",dir)); dos_ChDir(wd); return(False); } if (p && (p != base_name)) { *p = 0; if (strcmp(p+1,".")==0) p[1]=0; if (strcmp(p+1,"..")==0) *p = '/'; } if (dos_ChDir(base_name) != 0) { dos_ChDir(wd); DEBUG(3,("couldn't chdir for %s %s basename=%s\n",s,dir,base_name)); return(False); } if (!dos_GetWd(newname)) { dos_ChDir(wd); DEBUG(2,("couldn't get wd for %s %s\n",s,dir2)); return(False); } if (p && (p != base_name)) { pstrcat(newname,"/"); pstrcat(newname,p+1); } { size_t l = strlen(dir2); if (dir2[l-1] == '/') l--; if (strncmp(newname,dir2,l) != 0) { dos_ChDir(wd); DEBUG(2,("Bad access attempt? s=%s dir=%s newname=%s l=%d\n",s,dir2,newname,(int)l)); return(False); } if (relative) { if (newname[l] == '/') pstrcpy(s,newname + l + 1); else pstrcpy(s,newname+l); } else pstrcpy(s,newname); } dos_ChDir(wd); if (strlen(s) == 0) pstrcpy(s,"./"); DEBUG(3,("reduced to %s\n",s)); return(True);#endif}#endif /* 0 *//****************************************************************************expand some *s ****************************************************************************/static void expand_one(char *Mask,int len){ char *p1; while ((p1 = strchr(Mask,'*')) != NULL) { int lfill = (len+1) - strlen(Mask); int l1= (p1 - Mask); pstring tmp; pstrcpy(tmp,Mask); memset(tmp+l1,'?',lfill); pstrcpy(tmp + l1 + lfill,Mask + l1 + 1); pstrcpy(Mask,tmp); }}/****************************************************************************parse out a directory name from a path name. Assumes dos style filenames.****************************************************************************/static void dirname_dos(char *path,char *buf){ split_at_last_component(path, buf, '\\', NULL);}/****************************************************************************expand a wildcard expression, replacing *s with ?s****************************************************************************/void expand_mask(char *Mask,BOOL doext){ pstring mbeg,mext; pstring dirpart; pstring filepart; BOOL hasdot = False; char *p1; BOOL absolute = (*Mask == '\\'); *mbeg = *mext = *dirpart = *filepart = 0; /* parse the directory and filename */ if (strchr(Mask,'\\')) dirname_dos(Mask,dirpart); filename_dos(Mask,filepart); pstrcpy(mbeg,filepart); if ((p1 = strchr(mbeg,'.')) != NULL) { hasdot = True; *p1 = 0; p1++; pstrcpy(mext,p1); } else { pstrcpy(mext,""); if (strlen(mbeg) > 8) { pstrcpy(mext,mbeg + 8); mbeg[8] = 0; } } if (*mbeg == 0) pstrcpy(mbeg,"????????"); if ((*mext == 0) && doext && !hasdot) pstrcpy(mext,"???"); if (strequal(mbeg,"*") && *mext==0) pstrcpy(mext,"*"); /* expand *'s */ expand_one(mbeg,8); if (*mext) expand_one(mext,3); pstrcpy(Mask,dirpart); if (*dirpart || absolute) pstrcat(Mask,"\\"); pstrcat(Mask,mbeg); pstrcat(Mask,"."); pstrcat(Mask,mext); DEBUG(6,("Mask expanded to [%s]\n",Mask));} /**************************************************************************** make a dir struct****************************************************************************/void make_dir_struct(char *buf,char *mask,char *fname,SMB_OFF_T size,int mode,time_t date){ char *p; pstring mask2; pstrcpy(mask2,mask); if ((mode & aDIR) != 0) size = 0; memset(buf+1,' ',11); if ((p = strchr(mask2,'.')) != NULL) { *p = 0; memcpy(buf+1,mask2,MIN(strlen(mask2),8)); memcpy(buf+9,p+1,MIN(strlen(p+1),3)); *p = '.'; } else memcpy(buf+1,mask2,MIN(strlen(mask2),11)); memset(buf+21,'\0',DIR_STRUCT_SIZE-21); CVAL(buf,21) = mode; put_dos_date(buf,22,date); SSVAL(buf,26,size & 0xFFFF); SSVAL(buf,28,(size >> 16)&0xFFFF); StrnCpy(buf+30,fname,12); if (!case_sensitive) strupper(buf+30); DEBUG(8,("put name [%s] into dir struct\n",buf+30));}/*******************************************************************close the low 3 fd's and open dev/null in their place********************************************************************/void close_low_fds(void){ int fd; int i; close(0); close(1); close(2); /* try and use up these file descriptors, so silly library routines writing to stdout etc won't cause havoc */ for (i=0;i<3;i++) { fd = sys_open("/dev/null",O_RDWR,0); if (fd < 0) fd = sys_open("/dev/null",O_WRONLY,0); if (fd < 0) { DEBUG(0,("Cannot open /dev/null\n")); return; } if (fd != i) { DEBUG(0,("Didn't get file descriptor %d\n",i)); return; } }}/****************************************************************************Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,elseif SYSV use O_NDELAYif BSD use FNDELAY****************************************************************************/int set_blocking(int fd, BOOL set){ int val;#ifdef O_NONBLOCK#define FLAG_TO_SET O_NONBLOCK#else#ifdef SYSV#define FLAG_TO_SET O_NDELAY#else /* BSD */#define FLAG_TO_SET FNDELAY#endif#endif if((val = fcntl(fd, F_GETFL, 0)) == -1) return -1; if(set) /* Turn blocking on - ie. clear nonblock flag */ val &= ~FLAG_TO_SET; else val |= FLAG_TO_SET; return fcntl( fd, F_SETFL, val);#undef FLAG_TO_SET}/*******************************************************************find the difference in milliseconds between two struct timevalvalues********************************************************************/int TvalDiff(struct timeval *tvalold,struct timeval *tvalnew){ return((tvalnew->tv_sec - tvalold->tv_sec)*1000 + ((int)tvalnew->tv_usec - (int)tvalold->tv_usec)/1000); }#if 0/****************************************************************************transfer some data between two fd's****************************************************************************/SMB_OFF_T transfer_file(int infd,int outfd,SMB_OFF_T n,char *header,int headlen,int align){ static char *buf=NULL; static int size=0; char *buf1,*abuf; SMB_OFF_T total = 0; DEBUG(4,("transfer_file n=%.0f (head=%d) called\n",(double)n,headlen)); if (size == 0) { size = lp_readsize(); size = MAX(size,1024); } while (!buf && size>0) { buf = (char *)Realloc(buf,size+8); if (!buf) size /= 2; } if (!buf) { DEBUG(0,("Cannot allocate transfer buffer!\n")); exit(1); } abuf = buf + (align%8); if (header) n += headlen; while (n > 0) { int s = (int)MIN(n,(SMB_OFF_T)size); int ret,ret2=0; ret = 0; if (header && (headlen >= MIN(s,1024))) { buf1 = header; s = headlen; ret = headlen; headlen = 0; header = NULL; } else { buf1 = abuf; } if (header && headlen > 0) { ret = MIN(headlen,size); memcpy(buf1,header,ret); headlen -= ret; header += ret; if (headlen <= 0) header = NULL; } if (s > ret) ret += read(infd,buf1+ret,s-ret); if (ret > 0) { ret2 = (outfd>=0?write_data(outfd,buf1,ret):ret); if (ret2 > 0) total += ret2; /* if we can't write then dump excess data */ if (ret2 != ret) transfer_file(infd,-1,n-(ret+headlen),NULL,0,0); } if (ret <= 0 || ret2 != ret) return(total); n -= ret; } return(total);}#endif /* 0 *//****************************************************************************find a pointer to a netbios name****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -