comparison lib/lib.c @ 949:59d4d453296b

New stuff added to lib.c needs review too, so make a lib/pending.c and move several functions to it.
author Rob Landley <rob@landley.net>
date Sun, 14 Jul 2013 22:12:22 -0500
parents 55e587acefa9
children 62d59b8aea34
comparison
equal deleted inserted replaced
948:55e587acefa9 949:59d4d453296b
1032 } 1032 }
1033 1033
1034 return def; 1034 return def;
1035 } 1035 }
1036 1036
1037 // Execute a callback for each PID that matches a process name from a list.
1038 void for_each_pid_with_name_in(char **names, int (*callback)(pid_t pid, char *name))
1039 {
1040 DIR *dp;
1041 struct dirent *entry;
1042 char cmd[sizeof(toybuf)], path[64];
1043 char **curname;
1044
1045 if (!(dp = opendir("/proc"))) perror_exit("opendir");
1046
1047 while ((entry = readdir(dp))) {
1048 int fd, n;
1049
1050 if (!isdigit(*entry->d_name)) continue;
1051
1052 if (sizeof(path) <= snprintf(path, sizeof(path), "/proc/%s/cmdline",
1053 entry->d_name)) continue;
1054
1055 if (-1 == (fd=open(path, O_RDONLY))) continue;
1056 n = read(fd, cmd, sizeof(cmd));
1057 close(fd);
1058 if (n<1) continue;
1059
1060 for (curname = names; *curname; curname++)
1061 if (!strcmp(basename(cmd), *curname))
1062 if (!callback(atol(entry->d_name), *curname)) goto done;
1063 }
1064 done:
1065 closedir(dp);
1066 }
1067
1068 struct signame { 1037 struct signame {
1069 int num; 1038 int num;
1070 char *name; 1039 char *name;
1071 }; 1040 };
1072 1041
1233 else if (S_ISFIFO(mode)) c = 'p'; 1202 else if (S_ISFIFO(mode)) c = 'p';
1234 else if (S_ISSOCK(mode)) c = 's'; 1203 else if (S_ISSOCK(mode)) c = 's';
1235 else c = '-'; 1204 else c = '-';
1236 *buf = c; 1205 *buf = c;
1237 } 1206 }
1238
1239 char* make_human_readable(unsigned long long size, unsigned long unit)
1240 {
1241 unsigned int frac = 0;
1242 if(unit) {
1243 size = (size/(unit)) + (size%(unit)?1:0);
1244 return xmsprintf("%llu", size);
1245 }
1246 else {
1247 static char units[] = {'\0', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'};
1248 int index = 0;
1249 while(size >= 1024) {
1250 frac = size%1024;
1251 size /= 1024;
1252 index++;
1253 }
1254 frac = (frac/102) + ((frac%102)?1:0);
1255 if(frac >= 10) {
1256 size += 1;
1257 frac = 0;
1258 }
1259 if(frac) return xmsprintf("%llu.%u%c", size, frac, units[index]);
1260 else return xmsprintf("%llu%c", size, units[index]);
1261 }
1262 return NULL; //not reached
1263 }
1264
1265 // strtoul with exit on error
1266 unsigned long xstrtoul(const char *nptr, char **endptr, int base)
1267 {
1268 unsigned long l;
1269 errno = 0;
1270 l = strtoul(nptr, endptr, base);
1271 if (errno)
1272 perror_exit("xstrtoul");
1273 return l;
1274 }
1275
1276 /*
1277 * used to get the interger value.
1278 */
1279 unsigned long get_int_value(const char *numstr, unsigned lowrange, unsigned highrange)
1280 {
1281 unsigned long rvalue = 0;
1282 char *ptr;
1283 if(*numstr == '-' || *numstr == '+' || isspace(*numstr)) perror_exit("invalid number '%s'", numstr);
1284 errno = 0;
1285 rvalue = strtoul(numstr, &ptr, 10);
1286 if(errno || numstr == ptr) perror_exit("invalid number '%s'", numstr);
1287 if(*ptr) perror_exit("invalid number '%s'", numstr);
1288 if(rvalue >= lowrange && rvalue <= highrange) return rvalue;
1289 else {
1290 perror_exit("invalid number '%s'", numstr);
1291 return rvalue; //Not reachable; to avoid waring message.
1292 }
1293 }
1294
1295 /*
1296 * strcat to mallocated buffer
1297 * reallocate if need be
1298 */
1299 char *astrcat (char *x, char *y) {
1300 char *z;
1301 z = x;
1302 x = realloc (x, (x ? strlen (x) : 0) + strlen (y) + 1);
1303 if (!x) return 0;
1304 (z ? strcat : strcpy) (x, y);
1305 return x;
1306 }
1307
1308 /*
1309 * astrcat, but die on failure
1310 */
1311 char *xastrcat (char *x, char *y) {
1312 x = astrcat (x, y);
1313 if (!x) error_exit ("xastrcat");
1314 return x;
1315 }