Mercurial > hg > toybox
annotate toys/pending/lspci.c @ 968:b2b4bd208b65
I've written an lspci implementation.
Currently it supports emkn; e is an extension ("class" is a 24bit number,
but lspci only shows 16 bits; one person on the Puppy forums mentioned
that they need those last 8 bits).
n is a noop for compatability with standard lspci.
author  Isaac Dunham <idunham@lavabit.com> 

date  Fri, 26 Jul 2013 15:41:31 0500 
parents  
children  a47c6658210c 
rev  line source 

968
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

1 /* 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

2 * lspci  written by Isaac Dunham 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

3 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

4 USE_LSPCI(NEWTOY(lspci, "emkn", TOYFLAG_USRTOYFLAG_BIN)) 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

5 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

6 config LSPCI 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

7 bool "lspci" 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

8 default n 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

9 help 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

10 usage: lspci [ekmn] 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

11 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

12 List PCI devices. 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

13 e Output all 6 digits in class (like elspci) 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

14 k Print kernel driver 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

15 m Machine parseable format 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

16 n Numeric output (default) 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

17 */ 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

18 #define FOR_lspci 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

19 #include "toys.h" 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

20 char * preadat_name(int dirfd, char *fname, size_t nbyte, off_t offset) 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

21 { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

22 int fd; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

23 char *buf = malloc(nbyte+1); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

24 memset(buf, 0, sizeof(buf)); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

25 fd = openat(dirfd, fname, O_RDONLY); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

26 if (fd < 0) { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

27 return NULL; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

28 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

29 lseek(fd, offset, SEEK_SET); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

30 read(fd, buf, nbyte); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

31 close(fd); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

32 buf[nbyte +1] = '\0'; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

33 return buf; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

34 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

35 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

36 int do_lspci(struct dirtree *new) 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

37 { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

38 int alen = 8; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

39 char *dname = dirtree_path(new, &alen); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

40 errno = 0; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

41 int dirfd = open(dname, O_RDONLY); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

42 if (dirfd > 0) { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

43 char *class = preadat_name(dirfd, "class", 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

44 (toys.optflags & FLAG_e) ? 6 :4, 2); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

45 char *vendor = preadat_name(dirfd, "vendor", 4, 2); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

46 char *device = preadat_name(dirfd, "device", 4, 2); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

47 close(dirfd); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

48 if (!errno) { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

49 char *driver = ""; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

50 if (toys.optflags & FLAG_k) { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

51 char module[256] = ""; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

52 strcat(dname, "/driver"); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

53 readlink(dname, module, sizeof(module)); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

54 driver = basename(module); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

55 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

56 if (toys.optflags & FLAG_m) { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

57 printf("%s, \"%s\" \"%s\" \"%s\" \"%s\"\n",new>name + 5, class, 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

58 vendor, device, driver); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

59 } else { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

60 printf("%s Class %s: %s:%s %s\n", new>name + 5, class, vendor, device, 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

61 driver); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

62 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

63 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

64 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

65 if (!strcmp("/sys/bus/pci/devices", new>name)) { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

66 return DIRTREE_RECURSE; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

67 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

68 return 0; 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

69 } 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

70 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

71 void lspci_main(void) 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

72 { 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

73 sprintf(toybuf, "/sys/bus/pci/devices"); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

74 dirtree_read(toybuf, do_lspci); 
b2b4bd208b65
I've written an lspci implementation.
Isaac Dunham <idunham@lavabit.com>
parents:
diff
changeset

75 } 