1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/tools/tail.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,134 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "prio.h" 1.10 +#include "prprf.h" 1.11 +#include "prinit.h" 1.12 +#include "prthread.h" 1.13 +#include "prinrval.h" 1.14 + 1.15 +#include "plerror.h" 1.16 +#include "plgetopt.h" 1.17 + 1.18 +#include <stdlib.h> 1.19 + 1.20 +#define BUFFER_SIZE 500 1.21 + 1.22 +static PRFileDesc *out = NULL, *err = NULL; 1.23 + 1.24 +static void Help(void) 1.25 +{ 1.26 + PR_fprintf(err, "Usage: tail [-n <n>] [-f] [-h] <filename>\n"); 1.27 + PR_fprintf(err, "\t-t <n> Dally time in milliseconds\n"); 1.28 + PR_fprintf(err, "\t-n <n> Number of bytes before <eof>\n"); 1.29 + PR_fprintf(err, "\t-f Follow the <eof>\n"); 1.30 + PR_fprintf(err, "\t-h This message and nothing else\n"); 1.31 +} /* Help */ 1.32 + 1.33 +PRIntn main(PRIntn argc, char **argv) 1.34 +{ 1.35 + PRIntn rv = 0; 1.36 + PLOptStatus os; 1.37 + PRStatus status; 1.38 + PRFileDesc *file; 1.39 + PRFileInfo fileInfo; 1.40 + PRIntervalTime dally; 1.41 + char buffer[BUFFER_SIZE]; 1.42 + PRBool follow = PR_FALSE; 1.43 + const char *filename = NULL; 1.44 + PRUint32 position = 0, seek = 0, time = 0; 1.45 + PLOptState *opt = PL_CreateOptState(argc, argv, "hfn:"); 1.46 + 1.47 + out = PR_GetSpecialFD(PR_StandardOutput); 1.48 + err = PR_GetSpecialFD(PR_StandardError); 1.49 + 1.50 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.51 + { 1.52 + if (PL_OPT_BAD == os) continue; 1.53 + switch (opt->option) 1.54 + { 1.55 + case 0: /* it's the filename */ 1.56 + filename = opt->value; 1.57 + break; 1.58 + case 'n': /* bytes before end of file */ 1.59 + seek = atoi(opt->value); 1.60 + break; 1.61 + case 't': /* dally time */ 1.62 + time = atoi(opt->value); 1.63 + break; 1.64 + case 'f': /* follow the end of file */ 1.65 + follow = PR_TRUE; 1.66 + break; 1.67 + case 'h': /* user wants some guidance */ 1.68 + Help(); /* so give him an earful */ 1.69 + return 2; /* but not a lot else */ 1.70 + break; 1.71 + default: 1.72 + break; 1.73 + } 1.74 + } 1.75 + PL_DestroyOptState(opt); 1.76 + 1.77 + if (0 == time) time = 1000; 1.78 + dally = PR_MillisecondsToInterval(time); 1.79 + 1.80 + if (NULL == filename) 1.81 + { 1.82 + (void)PR_fprintf(out, "Input file not specified\n"); 1.83 + rv = 1; goto done; 1.84 + } 1.85 + file = PR_Open(filename, PR_RDONLY, 0); 1.86 + if (NULL == file) 1.87 + { 1.88 + PL_FPrintError(err, "File cannot be opened for reading"); 1.89 + return 1; 1.90 + } 1.91 + 1.92 + status = PR_GetOpenFileInfo(file, &fileInfo); 1.93 + if (PR_FAILURE == status) 1.94 + { 1.95 + PL_FPrintError(err, "Cannot acquire status of file"); 1.96 + rv = 1; goto done; 1.97 + } 1.98 + if (seek > 0) 1.99 + { 1.100 + if (seek > fileInfo.size) seek = 0; 1.101 + position = PR_Seek(file, (fileInfo.size - seek), PR_SEEK_SET); 1.102 + if (-1 == (PRInt32)position) 1.103 + PL_FPrintError(err, "Cannot seek to starting position"); 1.104 + } 1.105 + 1.106 + do 1.107 + { 1.108 + while (position < fileInfo.size) 1.109 + { 1.110 + PRInt32 read, bytes = fileInfo.size - position; 1.111 + if (bytes > sizeof(buffer)) bytes = sizeof(buffer); 1.112 + read = PR_Read(file, buffer, bytes); 1.113 + if (read != bytes) 1.114 + PL_FPrintError(err, "Cannot read to eof"); 1.115 + position += read; 1.116 + PR_Write(out, buffer, read); 1.117 + } 1.118 + 1.119 + if (follow) 1.120 + { 1.121 + PR_Sleep(dally); 1.122 + status = PR_GetOpenFileInfo(file, &fileInfo); 1.123 + if (PR_FAILURE == status) 1.124 + { 1.125 + PL_FPrintError(err, "Cannot acquire status of file"); 1.126 + rv = 1; goto done; 1.127 + } 1.128 + } 1.129 + } while (follow); 1.130 + 1.131 +done: 1.132 + PR_Close(file); 1.133 + 1.134 + return rv; 1.135 +} /* main */ 1.136 + 1.137 +/* tail.c */