comparison dep/animia/src/fd/proc.cc @ 233:0a5b6a088886

dep/animia: fix proc readlink
author Paper <mrpapersonic@gmail.com>
date Mon, 15 Jan 2024 08:10:58 -0500
parents 031a257ee019
children a7d0d543b334
comparison
equal deleted inserted replaced
232:ff0061e75f0f 233:0a5b6a088886
4 4
5 #include <filesystem> 5 #include <filesystem>
6 #include <fstream> 6 #include <fstream>
7 #include <sstream> 7 #include <sstream>
8 #include <string> 8 #include <string>
9
10 #include <iostream>
9 11
10 #include <sys/stat.h> 12 #include <sys/stat.h>
11 #include <fcntl.h> 13 #include <fcntl.h>
12 #include <unistd.h> 14 #include <unistd.h>
13 15
54 */ 56 */
55 constexpr size_t OUT_MAX = (1ul << 15); // 32KiB 57 constexpr size_t OUT_MAX = (1ul << 15); // 32KiB
56 out.resize(32); 58 out.resize(32);
57 59
58 ssize_t exe_used = 0; 60 ssize_t exe_used = 0;
59 while (out.length() < OUT_MAX && exe_used >= static_cast<ssize_t>(out.length())) { 61 do {
60 out.resize(out.length() * 2); 62 out.resize(out.length() * 2);
61 63
62 exe_used = readlink(link.c_str(), &out.front(), out.length()); 64 exe_used = readlink(link.c_str(), &out.front(), out.length());
63 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1) 65 if (exe_used == (ssize_t)-1 || exe_used < (ssize_t)1)
64 return false; // we got a bad result. SAD! 66 return false; // we got a bad result. SAD!
65 } 67 } while (out.length() < OUT_MAX && exe_used >= static_cast<ssize_t>(out.length()));
66 68
67 out.resize(out.find('\0')); 69 out.resize(out.find('\0'));
68 70
69 return true; 71 return true;
70 } 72 }