From 1c72116a84bd303e0b57d9ad40e3c2465af2bf66 Mon Sep 17 00:00:00 2001 From: evsign Date: Mon, 28 Jan 2019 12:55:17 +0300 Subject: [PATCH] fix ccat freezing when file is not exists --- bin/ccat | Bin 22024 -> 13776 bytes utils/ccat/ccat.cpp | 36 +++++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/bin/ccat b/bin/ccat index 35998b2f19179cdbbd925f68160cdbcc1b828fdc..68fbb7f6411dccddd7938a0168487227d03af47b 100755 GIT binary patch literal 13776 zcmeHOeQ;dWb-$|*TNrz_7Pb+_j-QFm*bpz0zmV~`NU~Ob0@+qA*)hcOW3{`IwqEUO zcHi1Ejj)Lb(~S^S389`8rnE^KN*UrXX-ZO(h?ubr>7*j1i3i$_%0Rn|^C3#1Dly*v z&VBc+-ahSXQ>K6P4`0pObIQ%5jr@lHIjuzwC+3@Klk{v`mCid+myoPu%ylcYoD>;Hn?5 z{Ki8s)Lc9ID#>1ttFUgUYr^uH@U;UAcY?TneITXm zf5OGiNf&&(3;szLyv+rF!^NKg7yMxtJ2$!DD_!tcT<||~!4od{cU=5j1zf}BET)T{ z=Ui}#9L;G2{B{@oOD=XOnoF(wtH2kFTA^tgYC`y+FAMIIlo<})d^6n}TZLit4J6`5 z$_yq=!w^PW*KQ*mNk;mjDKnDn+PyQDh)24DJ+X+@R=VZxJ)LGlG?6lTf~m;nws_QR zsSg|#2Jl2I73gT)*O}-v0suPA&5?LG))wkC>+3`P!K7g(gHbcp777IR0ow!%(U6fy znaN0Spsma3sxtzeTjANT9F&K%9 zK^RNG98F5-h!N^PV)O>1F)^4(MTbxgAC0Ck8%Ag-Xavp3P}CIgHk2BS1B7loVZ;)l zpczfX1qKloeGzjo*cVAf?~RCHEZP^3gpFVfGNLyci^LNm6idJ&#ISd<<=$uv2AIoc+6kt15ia)=~!s(gB&nOa{ zwBh#W_yrrT9^oV|rfm2!iC7<58-A?~pSIz(Hhjj0FSp@*zmhNXT;Wou4ozpD@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,10 +1,11 @@ #include -#include #include #include #include #include #include +#include +#include # define ZERO_BYTE_TRANSFER_ERRNO 0 @@ -33,14 +34,14 @@ size_t safe_write (int fd, void const *buf, size_t count) } } -size_t full_rw (int fd, const void *buf, size_t count) +size_t writeToStdOut (const void *buf, size_t count) { size_t total = 0; const char *ptr = (const char *) buf; while (count > 0) { - size_t n_rw = safe_write (fd, ptr, count); + size_t n_rw = safe_write (1, ptr, count); if (n_rw == (size_t) -1) break; if (n_rw == 0) @@ -74,6 +75,8 @@ int main(int argc, char const *argv[]) if (!file) { std::cerr << "failed to open " << argv[1] << std::endl; + + return EXIT_FAILURE; } int fileDescriptor = fileno(file); @@ -81,12 +84,8 @@ int main(int argc, char const *argv[]) //Notify the system that we will sequentially read the file on that file descriptor posix_fadvise(fileDescriptor, 0, 0, POSIX_FADV_SEQUENTIAL); - //Make c++ istream from file descriptor - __gnu_cxx::stdio_filebuf filebuf(fileDescriptor, std::ios::in); - - std::istream is(&filebuf); - - std::string fileName; + char * fileName = NULL; + size_t len = 0; char *inbuf; @@ -97,12 +96,19 @@ int main(int argc, char const *argv[]) /* Main loop with fast buffered read of files though syscals and output to stdout. */ - while(std::getline(is, fileName)) { + while(getline(&fileName, &len, file) != -1) { - int fileWithDataDescriptor = open(fileName.c_str(), O_RDONLY); + //remove \n character for opening file. + fileName[strcspn(fileName, "\n")] = 0; + + int fileWithDataDescriptor = open(fileName, O_RDONLY); + + if (fileWithDataDescriptor == -1) { + + perror("fopen"); + std::cerr << fileName << std::endl; + close(fileDescriptor); - if (!fileWithDataDescriptor) { - std::cerr << "failed to open " << fileName << std::endl; return EXIT_FAILURE; } @@ -113,15 +119,15 @@ int main(int argc, char const *argv[]) while(n_read = read(fileWithDataDescriptor, inbuf, insize)) { - full_rw(1, inbuf, n_read); + writeToStdOut(inbuf, n_read); } free(inbuf); close(fileWithDataDescriptor); } - filebuf.close(); close(fileDescriptor); return EXIT_SUCCESS; } +