Skip to content

Instantly share code, notes, and snippets.

@qwerty12
Created March 14, 2013 12:51
Show Gist options
  • Save qwerty12/5161069 to your computer and use it in GitHub Desktop.
Save qwerty12/5161069 to your computer and use it in GitHub Desktop.
Butchered version of Night Productions' pidof utility for OS X
#import <sys/sysctl.h>
#import <stdlib.h>
#import <string.h>
int pidof(const char *argv)
{
struct kinfo_proc *kp;
int mib[4],nentries,i;
size_t bufSize=0;
mib[0]=CTL_KERN;
mib[1]=KERN_PROC;
mib[2]=KERN_PROC_ALL;
mib[3]=0;
if (sysctl(mib,4,NULL,&bufSize,NULL,0) < 0) {
return 1;
}
kp=(struct kinfo_proc *)malloc(bufSize);
if (sysctl(mib,4,kp,&bufSize,NULL,0) < 0) {
return 1;
}
nentries=bufSize/sizeof(struct kinfo_proc);
if (nentries == 0) {
return 1;
}
for (i=nentries; --i >= 0;) {
char *proc=((&(&kp[i])->kp_proc)->p_comm);
if (strcasestr(proc, argv) != NULL) {
return 0;
}
}
return 1;
}
int main(int argc, char *argv[])
{
if (argc != 2) {
return 1;
}
return pidof(argv[1]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment