static int SwapBookmarkFiles(void) { char pidStr[32]; char pathName[256], path2[256]; (void) OurDirectoryPath(path2, sizeof(path2), kBookmarkFileName); (void) OurDirectoryPath(pathName, sizeof(pathName), kTmpBookmarkFileName); (void) sprintf(pidStr, "-%u.txt", (unsigned int) getpid()); (void) STRNCAT(pathName, pidStr); (void) remove(path2); if (rename(pathName, path2) < 0) { return (-1); } return (0); } /* SwapBookmarkFiles */
void LoadHistory(void) { char pathName[256]; if (gOurDirectoryPath[0] == '\0') return; (void) OurDirectoryPath(pathName, sizeof(pathName), kHistoryFileName); gl_histloadfile(pathName); } /* LoadHistory */
void CloseTrace(void) { char pathName[256]; char pathName2[256]; char tName[32]; if ((gOurDirectoryPath[0] == '\0') || (gTraceFile == NULL)) return; (void) sprintf(tName, "trace.%u", (unsigned int) getpid()); (void) OurDirectoryPath(pathName, sizeof(pathName), tName); (void) OurDirectoryPath(pathName2, sizeof(pathName2), kTraceFileName); (void) time(&gTraceTime); (void) fprintf(gTraceFile, "SESSION ENDED at: %s", ctime(&gTraceTime)); (void) fclose(gTraceFile); (void) unlink(pathName2); (void) rename(pathName, pathName2); } /* CloseTrace */
/* Saves a Bookmark structure into the bookmarks file. */ FILE * OpenTmpBookmarkFile(int nb) { FILE *outfp; char pidStr[32]; char pathName[256], path2[256]; if (gOurDirectoryPath[0] == '\0') return (NULL); /* Don't create in root directory. */ (void) OurDirectoryPath(path2, sizeof(path2), kBookmarkFileName); (void) OurDirectoryPath(pathName, sizeof(pathName), kTmpBookmarkFileName); (void) sprintf(pidStr, "-%u.txt", (unsigned int) getpid()); (void) STRNCAT(pathName, pidStr); outfp = fopen(pathName, FOPEN_WRITE_TEXT); if (outfp == NULL) { (void) fprintf(stderr, "Could not save bookmark.\n"); perror(pathName); return (NULL); } (void) _chmod(pathName, 00600); if (nb > 0) { if (fprintf(outfp, "NcFTP bookmark-file version: %d\nNumber of bookmarks: %d\n", kBookmarkVersion, nb) < 0) { (void) fprintf(stderr, "Could not save bookmark.\n"); perror(pathName); (void) fclose(outfp); return (NULL); } } else { if (fprintf(outfp, "NcFTP bookmark-file version: %d\nNumber of bookmarks: ??\n", kBookmarkVersion) < 0) { (void) fprintf(stderr, "Could not save bookmark.\n"); perror(pathName); (void) fclose(outfp); return (NULL); } } return (outfp); } /* OpenTmpBookmarkFile */
/* Save the commands they typed in a history file, then they can use * readline to go through them again next time. */ void SaveHistory(void) { char pathName[256]; if (gOurDirectoryPath[0] == '\0') return; /* Don't create in root directory. */ (void) OurDirectoryPath(pathName, sizeof(pathName), kHistoryFileName); gl_strlen = Vt100VisibleStrlen; gl_histsavefile(pathName); (void) _chmod(pathName, 00600); } /* SaveHistory */
void OpenTrace(void) { FILE *fp; char pathName[256]; char tName[32]; int pid; const char *cp; #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME) struct utsname u; #endif if (gOurDirectoryPath[0] == '\0') return; /* Don't create in root directory. */ (void) sprintf(tName, "trace.%u", (unsigned int) (pid = getpid())); (void) OurDirectoryPath(pathName, sizeof(pathName), tName); fp = fopen(pathName, FOPEN_WRITE_TEXT); if (fp != NULL) { (void) _chmod(pathName, 00600); #ifdef HAVE_SETVBUF (void) setvbuf(fp, gTraceLBuf, _IOLBF, sizeof(gTraceLBuf)); #endif /* HAVE_SETVBUF */ /* Opened the trace file. */ (void) time(&gTraceTime); (void) fprintf(fp, "SESSION STARTED at: %s", ctime(&gTraceTime)); (void) fprintf(fp, " Program Version: %s\n", gVersion + 5); (void) fprintf(fp, " Library Version: %s\n", gLibNcFTPVersion + 5); (void) fprintf(fp, " Process ID: %u\n", pid); if (gOS[0] != '\0') (void) fprintf(fp, " Platform: %s\n", gOS); #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME) if (uname(&u) == 0) { (void) fprintf(fp, " Uname: %.63s|%.63s|%.63s|%.63s|%.63s\r\n", u.sysname, u.nodename, u.release, u.version, u.machine); } #endif /* UNAME */ FTPInitializeOurHostName(&gLib); (void) fprintf(fp, " Hostname: %s (rc=%d)\n", gLib.ourHostName, gLib.hresult); cp = (const char *) getenv("TERM"); if (cp == NULL) cp = "unknown?"; (void) fprintf(fp, " Terminal: %s\n", cp); gTraceFile = fp; } } /* OpenTrace */
int MkSpoolDir(char *sdir, size_t size) { struct stat st; *sdir = '\0'; /* Don't create in root directory. */ if (gOurDirectoryPath[0] != '\0') { (void) OurDirectoryPath(sdir, size, kSpoolDir); if ((stat(sdir, &st) < 0) && (MkDirs(sdir, 00700) < 0)) { perror(sdir); return (-1); } else { return (0); } } return (-1); } /* MkSpoolDir */
void TruncBatchLog(void) { char f[256]; struct stat st; time_t t; int fd; if (gOurDirectoryPath[0] != '\0') { time(&t); t -= 86400; (void) OurDirectoryPath(f, sizeof(f), kSpoolLog); if ((stat(f, &st) == 0) && (st.st_mtime < t)) { /* Truncate old log file. * Do not remove it, since a process * could still conceivably be going. */ fd = open(f, O_WRONLY|O_TRUNC, 00600); if (fd >= 0) close(fd); } } } /* TruncBatchLog */
void InitLog(void) { OurDirectoryPath(gLogFileName, sizeof(gLogFileName), kLogFileName); } /* InitLog */
/* Opens a NcFTP 2.x or 3.x style bookmarks file, and sets the file pointer * so that it is ready to read the first data line. */ FILE * OpenBookmarkFile(int *numBookmarks0) { char pathName[256], path2[256]; char line[256]; FILE *fp; int version; int numBookmarks; Bookmark junkbm; if (gOurDirectoryPath[0] == '\0') return NULL; /* Don't create in root directory. */ (void) OurDirectoryPath(pathName, sizeof(pathName), kBookmarkFileName); fp = fopen(pathName, FOPEN_READ_TEXT); if (fp == NULL) { /* See if it exists under the old name. */ (void) OurDirectoryPath(path2, sizeof(path2), kOldBookmarkFileName); if (rename(path2, pathName) == 0) { /* Rename succeeded, now open it. */ fp = fopen(pathName, FOPEN_READ_TEXT); if (fp == NULL) return NULL; } return NULL; /* Okay to not have one yet. */ } (void) _chmod(pathName, 00600); if (FGets(line, sizeof(line), fp) == NULL) { (void) fprintf(stderr, "%s: invalid format.\n", pathName); (void) fclose(fp); return NULL; } /* Sample line we're looking for: * "NcFTP bookmark-file version: 8" */ version = -1; (void) sscanf(line, "%*s %*s %*s %d", &version); if (version < kBookmarkMinVersion) { if (version < 0) { (void) fprintf(stderr, "%s: invalid format, or bad version.\n", pathName); (void) fclose(fp); return NULL; } (void) STRNCPY(path2, pathName); (void) sprintf(line, ".v%d", version); (void) STRNCAT(path2, line); (void) rename(pathName, path2); (void) fprintf(stderr, "%s: old version.\n", pathName); (void) fclose(fp); return NULL; } /* Sample line we're looking for: * "Number of entries: 28" or "# # # 1" */ numBookmarks = -1; /* At the moment, we can't trust the number stored in the * file. It's there for future use. */ if (FGets(line, sizeof(line), fp) == NULL) { (void) fprintf(stderr, "%s: invalid format.\n", pathName); (void) fclose(fp); return NULL; } if (numBookmarks0 == (int *) 0) { /* If the caller doesn't care how many bookmarks are *really* * in the file, then we can return now. */ return(fp); } /* Otherwise, we have to read through the whole file because * unfortunately the header line can't be trusted. */ for (numBookmarks = 0; ; numBookmarks++) { if (GetNextBookmark(fp, &junkbm) < 0) break; } /* Now we have to re-open and re-position the file. * We don't use rewind() because it doesn't always work. * This introduces a race condition, but the bookmark * functionality wasn't designed to be air-tight. */ CloseBookmarkFile(fp); fp = fopen(pathName, FOPEN_READ_TEXT); if (fp == NULL) return (NULL); if (FGets(line, sizeof(line), fp) == NULL) { (void) fprintf(stderr, "%s: invalid format.\n", pathName); (void) fclose(fp); return NULL; } if (FGets(line, sizeof(line), fp) == NULL) { (void) fprintf(stderr, "%s: invalid format.\n", pathName); (void) fclose(fp); return NULL; } /* NOW we're done. */ *numBookmarks0 = numBookmarks; return (fp); } /* OpenBookmarkFile */