static char *clean_path(TALLOC_CTX *ctx, const char *path) { size_t len; char *p1, *p2, *p; char *path_out; /* No absolute paths. */ while (IS_DIRECTORY_SEP(*path)) { path++; } path_out = talloc_strdup(ctx, path); if (!path_out) { return NULL; } p1 = strchr_m(path_out, '*'); p2 = strchr_m(path_out, '?'); if (p1 || p2) { if (p1 && p2) { p = MIN(p1,p2); } else if (!p1) { p = p2; } else { p = p1; } *p = '\0'; /* Now go back to the start of this component. */ p1 = strrchr_m(path_out, '/'); p2 = strrchr_m(path_out, '\\'); p = MAX(p1,p2); if (p) { *p = '\0'; } } /* Strip any trailing separator */ len = strlen(path_out); if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) { path_out[len-1] = '\0'; } return path_out; }
static void cli_dfs_make_full_path( struct cli_state *cli, const char *dir, pstring path_out) { /* Ensure the extrapath doesn't start with a separator. */ while (IS_DIRECTORY_SEP(*dir)) { dir++; } pstr_sprintf( path_out, "\\%s\\%s\\%s", cli->desthost, cli->share, dir); }
static void clean_path(const char *path, pstring path_out) { size_t len; char *p1, *p2, *p; /* No absolute paths. */ while (IS_DIRECTORY_SEP(*path)) { path++; } pstrcpy(path_out, path); p1 = strchr_m(path_out, '*'); p2 = strchr_m(path_out, '?'); if (p1 || p2) { if (p1 && p2) { p = MIN(p1,p2); } else if (!p1) { p = p2; } else { p = p1; } *p = '\0'; /* Now go back to the start of this component. */ p1 = strrchr_m(path_out, '/'); p2 = strrchr_m(path_out, '\\'); p = MAX(p1,p2); if (p) { *p = '\0'; } } /* Strip any trailing separator */ len = strlen(path_out); if ( (len > 0) && IS_DIRECTORY_SEP(path_out[len-1])) { path_out[len-1] = '\0'; } }
/* Return the current working directory. Returns NULL on errors. Any other returned value must be freed with free. This is used only when get_current_dir_name is not defined on the system. */ char* get_current_dir_name (void) { char *buf; const char *pwd; struct stat dotstat, pwdstat; /* If PWD is accurate, use it instead of calling getwd. PWD is sometimes a nicer name, and using it may avoid a fatal error if a parent directory is searchable but not readable. */ if ((pwd = egetenv ("PWD")) != 0 && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1]))) && stat (pwd, &pwdstat) == 0 && stat (".", &dotstat) == 0 && dotstat.st_ino == pwdstat.st_ino && dotstat.st_dev == pwdstat.st_dev #ifdef MAXPATHLEN && strlen (pwd) < MAXPATHLEN #endif ) { buf = (char *) xmalloc (strlen (pwd) + 1); strcpy (buf, pwd); } #ifdef HAVE_GETCWD else { size_t buf_size = 1024; for (;;) { int tmp_errno; buf = malloc (buf_size); if (! buf) break; if (getcwd (buf, buf_size) == buf) break; tmp_errno = errno; free (buf); if (tmp_errno != ERANGE) { errno = tmp_errno; return NULL; } buf_size *= 2; if (! buf_size) { errno = ENOMEM; return NULL; } } } #else else {
/* Add a source file name boundary marker in the output file. */ static void put_filename (char *filename) { char *tmp; for (tmp = filename; *tmp; tmp++) { if (IS_DIRECTORY_SEP (*tmp)) filename = tmp + 1; } printf ("\037S%s\n", filename); }
int set_log_file(const char *fname, int loglevel) { int ret; char *p = NULL; const char *fullpath; /* Path detection. */ if (! IS_DIRECTORY_SEP (*fname)) { char cwd[MAXPATHLEN+1]; cwd[MAXPATHLEN] = '\0'; if (getcwd (cwd, MAXPATHLEN) == NULL) { // zlog_err ("config_log_file: Unable to alloc mem!"); printf ("config_log_file: Unable to alloc mem!"); return -1; } if ( (p = calloc (1, strlen (cwd) + strlen (fname) + 2)) == NULL) { printf("config_log_file: Unable to alloc mem!"); return -1; } sprintf (p, "%s/%s", cwd, fname); fullpath = p; } else fullpath = fname; ret = zlog_set_file (NULL, fullpath, loglevel); if (p) free(p); if (!ret) { // vty_out (vty, "can't open logfile %s\n", fname); printf("can't open logfile %s\n", fname); return -1; } // if (host.logfile) // XFREE (MTYPE_HOST, host.logfile); // host.logfile = XSTRDUP (MTYPE_HOST, fname); return 1; }
static char *cli_dfs_make_full_path(TALLOC_CTX *ctx, struct cli_state *cli, const char *dir) { char path_sep = '\\'; /* Ensure the extrapath doesn't start with a separator. */ while (IS_DIRECTORY_SEP(*dir)) { dir++; } if (cli->requested_posix_capabilities & CIFS_UNIX_POSIX_PATHNAMES_CAP) { path_sep = '/'; } return talloc_asprintf(ctx, "%c%s%c%s%c%s", path_sep, cli_state_remote_name(cli), path_sep, cli->share, path_sep, dir); }
int main (int argc, char **argv) { char *inname, *outname; int indesc, outdesc; ssize_t nread; int wait_status; int c, preserve_mail = 0; #ifndef MAIL_USE_SYSTEM_LOCK struct stat st; int tem; char *lockname; char *tempname; size_t inname_len, inname_dirlen; int desc; #endif /* not MAIL_USE_SYSTEM_LOCK */ #ifdef MAIL_USE_MAILLOCK char *spool_name; #endif #ifdef MAIL_USE_POP int pop_reverse_order = 0; # define ARGSTR "pr" #else /* ! MAIL_USE_POP */ # define ARGSTR "p" #endif /* MAIL_USE_POP */ uid_t real_gid = getgid (); uid_t priv_gid = getegid (); #ifdef WINDOWSNT /* Ensure all file i/o is in binary mode. */ _fmode = _O_BINARY; #endif delete_lockname = 0; while ((c = getopt (argc, argv, ARGSTR)) != EOF) { switch (c) { #ifdef MAIL_USE_POP case 'r': pop_reverse_order = 1; break; #endif case 'p': preserve_mail++; break; default: exit (EXIT_FAILURE); } } if ( #ifdef MAIL_USE_POP (argc - optind < 2) || (argc - optind > 3) #else (argc - optind != 2) #endif ) { #ifdef MAIL_USE_POP fprintf (stderr, "Usage: movemail [-p] [-r] inbox destfile%s\n", " [POP-password]"); #else fprintf (stderr, "Usage: movemail [-p] inbox destfile%s\n", ""); #endif exit (EXIT_FAILURE); } inname = argv[optind]; outname = argv[optind+1]; #ifdef MAIL_USE_MMDF mmdf_init (argv[0]); #endif if (*outname == 0) fatal ("Destination file name is empty", 0, 0); #ifdef MAIL_USE_POP if (!strncmp (inname, "po:", 3)) { int status; status = popmail (inname + 3, outname, preserve_mail, (argc - optind == 3) ? argv[optind+2] : NULL, pop_reverse_order); exit (status); } if (setuid (getuid ()) < 0) fatal ("Failed to drop privileges", 0, 0); #endif /* MAIL_USE_POP */ #ifndef DISABLE_DIRECT_ACCESS #ifndef MAIL_USE_MMDF #ifndef MAIL_USE_SYSTEM_LOCK #ifdef MAIL_USE_MAILLOCK spool_name = mail_spool_name (inname); if (spool_name) { #ifdef lint lockname = 0; #endif } else #endif { /* Use a lock file named after our first argument with .lock appended: If it exists, the mail file is locked. */ /* Note: this locking mechanism is *required* by the mailer (on systems which use it) to prevent loss of mail. On systems that use a lock file, extracting the mail without locking WILL occasionally cause loss of mail due to timing errors! So, if creation of the lock file fails due to access permission on the mail spool directory, you simply MUST change the permission and/or make movemail a setgid program so it can create lock files properly. You might also wish to verify that your system is one which uses lock files for this purpose. Some systems use other methods. */ inname_len = strlen (inname); lockname = xmalloc (inname_len + sizeof ".lock"); strcpy (lockname, inname); strcpy (lockname + inname_len, ".lock"); for (inname_dirlen = inname_len; inname_dirlen && !IS_DIRECTORY_SEP (inname[inname_dirlen - 1]); inname_dirlen--) continue; tempname = xmalloc (inname_dirlen + sizeof "EXXXXXX"); while (1) { /* Create the lock file, but not under the lock file name. */ /* Give up if cannot do that. */ memcpy (tempname, inname, inname_dirlen); strcpy (tempname + inname_dirlen, "EXXXXXX"); #ifdef HAVE_MKSTEMP desc = mkstemp (tempname); #else mktemp (tempname); if (!*tempname) desc = -1; else { unlink (tempname); desc = open (tempname, O_WRONLY | O_CREAT | O_EXCL, 0600); } #endif if (desc < 0) { int mkstemp_errno = errno; error ("error while creating what would become the lock file", 0, 0); errno = mkstemp_errno; pfatal_with_name (tempname); } close (desc); tem = link (tempname, lockname); #ifdef EPERM if (tem < 0 && errno == EPERM) fatal ("Unable to create hard link between %s and %s", tempname, lockname); #endif unlink (tempname); if (tem >= 0) break; sleep (1); /* If lock file is five minutes old, unlock it. Five minutes should be good enough to cope with crashes and wedgitude, and long enough to avoid being fooled by time differences between machines. */ if (stat (lockname, &st) >= 0) { time_t now = time (0); if (st.st_ctime < now - 300) unlink (lockname); } } delete_lockname = lockname; } #endif /* not MAIL_USE_SYSTEM_LOCK */ #endif /* not MAIL_USE_MMDF */ if (fork () == 0) { int lockcount = 0; int status = 0; #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK) time_t touched_lock; # ifdef lint touched_lock = 0; # endif #endif if (setuid (getuid ()) < 0 || setregid (-1, real_gid) < 0) fatal ("Failed to drop privileges", 0, 0); #ifndef MAIL_USE_MMDF #ifdef MAIL_USE_SYSTEM_LOCK indesc = open (inname, O_RDWR); #else /* if not MAIL_USE_SYSTEM_LOCK */ indesc = open (inname, O_RDONLY); #endif /* not MAIL_USE_SYSTEM_LOCK */ #else /* MAIL_USE_MMDF */ indesc = lk_open (inname, O_RDONLY, 0, 0, 10); #endif /* MAIL_USE_MMDF */ if (indesc < 0) pfatal_with_name (inname); #ifdef BSD_SYSTEM /* In case movemail is setuid to root, make sure the user can read the output file. */ /* This is desirable for all systems but I don't want to assume all have the umask system call */ umask (umask (0) & 0333); #endif /* BSD_SYSTEM */ outdesc = open (outname, O_WRONLY | O_CREAT | O_EXCL, 0666); if (outdesc < 0) pfatal_with_name (outname); if (setregid (-1, priv_gid) < 0) fatal ("Failed to regain privileges", 0, 0); /* This label exists so we can retry locking after a delay, if it got EAGAIN or EBUSY. */ retry_lock: /* Try to lock it. */ #ifdef MAIL_USE_MAILLOCK if (spool_name) { /* The "0 - " is to make it a negative number if maillock returns non-zero. */ status = 0 - maillock (spool_name, 1); #ifdef HAVE_TOUCHLOCK touched_lock = time (0); #endif lockcount = 5; } else #endif /* MAIL_USE_MAILLOCK */ { #ifdef MAIL_USE_SYSTEM_LOCK #ifdef MAIL_USE_LOCKF status = lockf (indesc, F_LOCK, 0); #else /* not MAIL_USE_LOCKF */ #ifdef WINDOWSNT status = locking (indesc, LK_RLCK, -1L); #else status = flock (indesc, LOCK_EX); #endif #endif /* not MAIL_USE_LOCKF */ #endif /* MAIL_USE_SYSTEM_LOCK */ } /* If it fails, retry up to 5 times for certain failure codes. */ if (status < 0) { if (++lockcount <= 5) { #ifdef EAGAIN if (errno == EAGAIN) { sleep (1); goto retry_lock; } #endif #ifdef EBUSY if (errno == EBUSY) { sleep (1); goto retry_lock; } #endif } pfatal_with_name (inname); } { char buf[1024]; while (1) { nread = read (indesc, buf, sizeof buf); if (nread < 0) pfatal_with_name (inname); if (nread != write (outdesc, buf, nread)) { int saved_errno = errno; unlink (outname); errno = saved_errno; pfatal_with_name (outname); } if (nread < sizeof buf) break; #if defined (MAIL_USE_MAILLOCK) && defined (HAVE_TOUCHLOCK) if (spool_name) { time_t now = time (0); if (now - touched_lock > 60) { touchlock (); touched_lock = now; } } #endif /* MAIL_USE_MAILLOCK */ } } #ifdef BSD_SYSTEM if (fsync (outdesc) < 0) pfatal_and_delete (outname); #endif /* Prevent symlink attacks truncating other users' mailboxes */ if (setregid (-1, real_gid) < 0) fatal ("Failed to drop privileges", 0, 0); /* Check to make sure no errors before we zap the inbox. */ if (close (outdesc) != 0) pfatal_and_delete (outname); #ifdef MAIL_USE_SYSTEM_LOCK if (! preserve_mail) { if (ftruncate (indesc, 0L) != 0) pfatal_with_name (inname); } #endif /* MAIL_USE_SYSTEM_LOCK */ #ifdef MAIL_USE_MMDF lk_close (indesc, 0, 0, 0); #else close (indesc); #endif #ifndef MAIL_USE_SYSTEM_LOCK if (! preserve_mail) { /* Delete the input file; if we can't, at least get rid of its contents. */ #ifdef MAIL_UNLINK_SPOOL /* This is generally bad to do, because it destroys the permissions that were set on the file. Better to just empty the file. */ if (unlink (inname) < 0 && errno != ENOENT) #endif /* MAIL_UNLINK_SPOOL */ creat (inname, 0600); } #endif /* not MAIL_USE_SYSTEM_LOCK */ /* End of mailbox truncation */ if (setregid (-1, priv_gid) < 0) fatal ("Failed to regain privileges", 0, 0); #ifdef MAIL_USE_MAILLOCK /* This has to occur in the child, i.e., in the process that acquired the lock! */ if (spool_name) mailunlock (); #endif exit (EXIT_SUCCESS); } wait (&wait_status); if (!WIFEXITED (wait_status)) exit (EXIT_FAILURE); else if (WEXITSTATUS (wait_status) != 0) exit (WEXITSTATUS (wait_status)); #if !defined (MAIL_USE_MMDF) && !defined (MAIL_USE_SYSTEM_LOCK) #ifdef MAIL_USE_MAILLOCK if (! spool_name) #endif /* MAIL_USE_MAILLOCK */ unlink (lockname); #endif /* not MAIL_USE_MMDF and not MAIL_USE_SYSTEM_LOCK */ #endif /* ! DISABLE_DIRECT_ACCESS */ return EXIT_SUCCESS; }
static WIN32_FIND_DATA * mswindows_get_files (char *dirfile, int nowild, Lisp_Object pattern, int hide_dot, int hide_system, int *nfiles) { WIN32_FIND_DATA *files; int array_size; struct re_pattern_buffer *bufp = NULL; int findex, len; char win32pattern[MAXNAMLEN+3]; HANDLE fh; int errm; /* * Much of the following code and comments were taken from dired.c. * Yes, this is something of a waste, but we want speed, speed, SPEED. */ files = NULL; array_size = *nfiles = 0; while (1) { if (!NILP(pattern)) { /* PATTERN might be a flawed regular expression. Rather than catching and signalling our own errors, we just call compile_pattern to do the work for us. */ bufp = compile_pattern (pattern, 0, Qnil, 0, ERROR_ME); } /* Now *bufp is the compiled form of PATTERN; don't call anything which might compile a new regexp until we're done with the loop! */ /* Initialize file info array */ array_size = 100; /* initial size */ files = xmalloc(array_size * sizeof (WIN32_FIND_DATA)); /* for Win32, we need to insure that the pathname ends with "\*". */ strcpy (win32pattern, dirfile); if (!nowild) { len = strlen (win32pattern) - 1; if (!IS_DIRECTORY_SEP (win32pattern[len])) strcat (win32pattern, "\\"); strcat (win32pattern, "*"); } /* * Here, we use FindFirstFile()/FindNextFile() instead of opendir(), * xemacs_stat(), & friends, because xemacs_stat() is VERY expensive in * terms of time. Hence, we take the time to write complicated * Win32-specific code, instead of simple Unix-style stuff. */ findex = 0; fh = INVALID_HANDLE_VALUE; errm = SetErrorMode (SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); while (1) { int len; char *filename; int result; if (fh == INVALID_HANDLE_VALUE) { fh = FindFirstFile(win32pattern, &files[findex]); if (fh == INVALID_HANDLE_VALUE) { SetErrorMode (errm); report_file_error ("Opening directory", list1(build_string(dirfile))); } } else { if (!FindNextFile(fh, &files[findex])) { if (GetLastError() == ERROR_NO_MORE_FILES) { break; } FindClose(fh); SetErrorMode (errm); report_file_error ("Reading directory", list1(build_string(dirfile))); } } filename = files[findex].cFileName; if (!NILP(Vmswindows_downcase_file_names)) { strlwr(filename); } len = strlen(filename); result = (NILP(pattern) || (0 <= re_search (bufp, filename, len, 0, len, 0))); if (result) { if ( ! (filename[0] == '.' && ((hide_system && (filename[1] == '\0' || (filename[1] == '.' && filename[2] == '\0'))) || hide_dot))) { if (++findex >= array_size) { array_size = findex * 2; files = xrealloc(files, array_size * sizeof(WIN32_FIND_DATA)); } } } } if (fh != INVALID_HANDLE_VALUE) { FindClose (fh); } *nfiles = findex; break; } SetErrorMode (errm); return (files); }