ps 명령어 흉내내기

Programming/Unix/Linux 2007/05/08 17:30 장인정신
span class=postbodyUNIX의 ps 명령을 흉내낸 예제입니다. br / br / /spantable align=center border=0 cellpadding=3 cellspacing=1 width=90%tbodytr tdspan class=genmedb코드:/b/span/td /tr tr td class=code br / /* br / Copyright (c) 2002 Information Equipment co.,LTD. br / All Right Reserved. br / br / Code by JaeHyuk Cho lt;minzkn@infoeq.co.krgt; br / br / This example !!! br / nbsp; nbsp;- Simple is best. br / */ br / br / #include lt;stdio.hgt; br / #include lt;stdlib.hgt; br / #include lt;string.hgt; br / #include lt;sys/types.hgt; br / #include lt;sys/stat.hgt; br / #include lt;unistd.hgt; br / #include lt;fcntl.hgt; br / #include lt;dirent.hgt; br / #include lt;ctype.hgt; br / br / static int MZ_ps_LoadFile(char *s_FileName, void **s_Buffer) br / { br / int s_Return = (-1), s_Handle, s_Length; br / *(s_Buffer) = (void *)0; br / s_Handle = open(s_FileName, O_RDONLY); br / if(s_Handle gt;= 0) br / { br / nbsp; s_Length = 32 lt;lt; 10;nbsp; nbsp; br / nbsp; *(s_Buffer) = (void *)malloc(s_Length); br / nbsp; if(*(s_Buffer))s_Return = read(s_Handle, *(s_Buffer), s_Length); br / nbsp; else fprintf(stderr, Not enough memory !!!\n); br / nbsp; close(s_Handle); br / } br / else fprintf(stderr, Can not open file \%s\ !!!\n, s_FileName); br / return(s_Return); br / } br / br / static void MZ_ps(void) br / { br / char *s_ProcDirectory = /proc; br / DIRnbsp; nbsp; nbsp; nbsp; nbsp; nbsp;*s_Directory; br / struct dirent *s_DirectoryEntry; br / charnbsp; nbsp; nbsp; nbsp; nbsp; nbsp;s_TempPath[32]; br / s_Directory = opendir(s_ProcDirectory); br / if(s_Directory) br / { br / nbsp; fprintf(stdout, %5s %5s %5s %5s %s\n, PPID, PID, UID, State, Name); br / nbsp; do br / nbsp; { br / nbsp; nbsp;s_DirectoryEntry = readdir(s_Directory); br / nbsp; nbsp;if(s_DirectoryEntry) br / nbsp; nbsp;{ br / nbsp; nbsp; void *s_Buffer, *s_Ptr; br / nbsp; nbsp; int s_Length;nbsp; nbsp; br / nbsp; nbsp; char s_Name[16]; br / nbsp; nbsp; intnbsp; s_State, s_PID, s_PPID, s_UID; br / nbsp; nbsp; if(isdigit(*((char *)(s_DirectoryEntry-gt;d_name))) == 0)continue; br / nbsp; nbsp; s_Name[0] = s_Name[15] = '\0'; br / nbsp; nbsp; s_Statenbsp; nbsp;= '?'; br / nbsp; nbsp; s_PIDnbsp; nbsp; nbsp;= (-1); br / nbsp; nbsp; s_PPIDnbsp; nbsp; = (-1); br / nbsp; nbsp; s_UIDnbsp; nbsp; nbsp;= (-1); br / nbsp; nbsp; sprintf(amp;s_TempPath[0], %s/%s/status, s_ProcDirectory, (char *)s_DirectoryEntry-gt;d_name);nbsp; nbsp; nbsp; br / nbsp; nbsp; s_Length = MZ_ps_LoadFile(amp;s_TempPath[0], amp;s_Buffer); br / nbsp; nbsp; if(s_Length gt; 0 amp;amp; s_Buffer) br / nbsp; nbsp; { br / nbsp; nbsp; nbsp;s_Ptr = strstr(s_Buffer, Name:);nbsp; nbsp;nbsp; nbsp; br / nbsp; nbsp; nbsp;if(s_Ptr)sscanf(s_Ptr, Name:\t%15s, amp;s_Name[0]); br / nbsp; nbsp; nbsp;s_Ptr = strstr(s_Buffer, State:); br / nbsp; nbsp; nbsp;if(s_Ptr)sscanf(s_Ptr, State:\t%c, (char *)amp;s_State); br / nbsp; nbsp; nbsp;s_Ptr = strstr(s_Buffer, Pid:); br / nbsp; nbsp; nbsp;if(s_Ptr)sscanf(s_Ptr, Pid:\t%d, amp;s_PID); br / nbsp; nbsp; nbsp;s_Ptr = strstr(s_Buffer, PPid:); br / nbsp; nbsp; nbsp;if(s_Ptr)sscanf(s_Ptr, PPid:\t%d, amp;s_PPID); br / nbsp; nbsp; nbsp;s_Ptr = strstr(s_Buffer, Uid:); br / nbsp; nbsp; nbsp;if(s_Ptr)sscanf(s_Ptr, Uid:\t%d, amp;s_UID); br / nbsp; nbsp; nbsp;free(s_Buffer);nbsp; br / nbsp; nbsp; } br / nbsp; nbsp; sprintf(amp;s_TempPath[0], %s/%s/cmdline, s_ProcDirectory, (char *)s_DirectoryEntry-gt;d_name);nbsp; nbsp; br / nbsp; nbsp; s_Length = MZ_ps_LoadFile(amp;s_TempPath[0], amp;s_Buffer); br / nbsp; nbsp; if(s_Length gt; 0 amp;amp; s_Buffer) br / nbsp; nbsp; { br / nbsp; nbsp; nbsp;fprintf(stdout, %5d %5d %5dnbsp; nbsp;%cnbsp; nbsp;%s\n, s_PPID, s_PID, s_UID, s_State, s_Name);nbsp; nbsp;nbsp; nbsp; br / nbsp; nbsp; nbsp;free(s_Buffer);nbsp; br / nbsp; nbsp; } br / nbsp; nbsp;} br / nbsp; }while(s_DirectoryEntry); br / nbsp; closedir(s_Directory); br / } br / else fprintf(stderr, Can not open directory \/proc\\n); br / } br / br / int main(int s_Argc, char **s_Argv) br / { br / fprintf(stdout, MZ_ps v1.0.0 - Copyright(c)InfoEQ co.,LTD - %s %s\n, __DATE__, __TIME__); br / fprintf(stdout, Code by JaeHyuk Cho - lt;minzkn@infoeq.co.krgt;\n\n); br / MZ_ps(); br / return(0);nbsp; nbsp; br / } br / br / /* End of source */ /td/tr/tbody/table
크리에이티브 커먼즈 라이센스
Creative Commons License
2007/05/08 17:30 2007/05/08 17:30
받은 트랙백이 없고, 댓글이 없습니다.

댓글+트랙백 RSS :: http://blog.minzkn.com/rss/response/73

댓글+트랙백 ATOM :: http://blog.minzkn.com/atom/response/73

트랙백 주소 :: http://blog.minzkn.com/trackback/73

트랙백 RSS :: http://blog.minzkn.com/rss/trackback/73

트랙백 ATOM :: http://blog.minzkn.com/atom/trackback/73

댓글을 달아 주세요

댓글 RSS 주소 : http://blog.minzkn.com/rss/comment/73
댓글 ATOM 주소 : http://blog.minzkn.com/atom/comment/73