* some new processes start up while we wait. The kernel will
* just ignore any extras if we give it too many, and will trunc.
* the list if we give it too few. */
- pid_array = (pid_t*) calloc( num_pids+10, sizeof(pid_t));
+ pid_array = (pid_t*) xcalloc( num_pids+10, sizeof(pid_t));
pid_array[0] = num_pids+10;
/* Now grab the pid list */
if ((strstr(info.command_line, pidName) != NULL)
&& (strlen(pidName) == strlen(info.command_line))) {
- pidList=realloc( pidList, sizeof(pid_t) * (j+2));
- if (pidList==NULL)
- fatalError(memory_exhausted);
+ pidList=xrealloc( pidList, sizeof(pid_t) * (j+2));
pidList[j++]=info.pid;
}
}
FILE *status;
char filename[256];
char buffer[256];
- char* p;
/* If it isn't a number, we don't want it */
if (!isdigit(*next->d_name))
continue;
- /* Now open the status file */
- sprintf(filename, "/proc/%s/status", next->d_name);
+ sprintf(filename, "/proc/%s/cmdline", next->d_name);
status = fopen(filename, "r");
if (!status) {
continue;
fgets(buffer, 256, status);
fclose(status);
- /* Make sure we only match on the process name */
- p=buffer+5; /* Skip the name */
- while ((p)++) {
- if (*p==0 || *p=='\n') {
- *p='\0';
- break;
- }
- }
- p=buffer+6; /* Skip the "Name:\t" */
-
- if ((strstr(p, pidName) != NULL)
- && (strlen(pidName) == strlen(p))) {
- pidList=realloc( pidList, sizeof(pid_t) * (i+2));
- if (pidList==NULL)
- fatalError(memory_exhausted);
+ if (strstr(get_last_path_component(buffer), pidName) != NULL) {
+ pidList=xrealloc( pidList, sizeof(pid_t) * (i+2));
pidList[i++]=strtol(next->d_name, NULL, 0);
}
}
+
if (pidList)
pidList[i]=0;
return pidList;
break;
/* grow the line buffer as necessary */
if (idx > linebufsz-2)
- linebuf = realloc(linebuf, linebufsz += GROWBY);
+ linebuf = xrealloc(linebuf, linebufsz += GROWBY);
linebuf[idx++] = (char)ch;
if ((char)ch == '\n')
break;