00001 /* 00002 Display STREAMS modules 00003 AUP2, Sec. 4.09 00004 00005 Copyright 2003 by Marc J. Rochkind. All rights reserved. 00006 May be copied only for purposes and under conditions described 00007 on the Web page www.basepath.com/aup/copyright.htm. 00008 00009 The Example Files are provided "as is," without any warranty; 00010 without even the implied warranty of merchantability or fitness 00011 for a particular purpose. The author and his publisher are not 00012 responsible for any damages, direct or incidental, resulting 00013 from the use or non-use of these Example Files. 00014 00015 The Example Files may contain defects, and some contain deliberate 00016 coding mistakes that were included for educational reasons. 00017 You are responsible for determining if and how the Example Files 00018 are to be used. 00019 00020 */ 00021 #include "defs.h" 00022 00023 #define WANT_TEST 00024 00025 #if _XOPEN_STREAMS > 0 00026 00027 #include "streamlist.h" 00028 00029 #include <stropts.h> 00030 #include <sys/conf.h> 00031 00032 bool stream_list(int fd) 00033 { 00034 int num_modules, i; 00035 struct str_list *list; 00036 00037 ec_neg1( num_modules = ioctl(fd, I_LIST, NULL) ) 00038 printf("Stream has %d modules\n", num_modules); 00039 if (num_modules > 0) { 00040 ec_null( list = malloc(sizeof(struct str_list)) ) 00041 list->sl_nmods = num_modules; 00042 ec_null( list->sl_modlist = malloc(num_modules * 00043 sizeof(struct str_mlist)) ) 00044 ec_neg1( ioctl(fd, I_LIST, list) ) 00045 for (i = 0; i < list->sl_nmods; i++) 00046 printf("Module %d: \"%s\"\n", i, list->sl_modlist[i].l_name); 00047 } 00048 return true; 00049 00050 EC_CLEANUP_BGN 00051 return false; 00052 EC_CLEANUP_END 00053 } 00054 00055 #ifdef WANT_TEST 00056 00057 int main(void) 00058 { 00059 int fds[2], fd; 00060 00061 printf("Standard input:\n"); 00062 ec_false( stream_list(STDIN_FILENO) ) 00063 00064 ec_neg1( pipe(fds) ) 00065 printf("\nPipe:\n"); 00066 ec_false( stream_list(fds[0]) ) 00067 00068 ec_neg1( fd = open("/dev/console", O_WRONLY) ) 00069 printf("\n/dev/console:\n"); 00070 ec_false( stream_list(fd) ) 00071 00072 ec_neg1( fd = open("/dev/tty", O_WRONLY) ) 00073 printf("\n/dev/tty:\n"); 00074 ec_false( stream_list(fd) ) 00075 00076 exit(EXIT_SUCCESS); 00077 00078 EC_CLEANUP_BGN 00079 exit(EXIT_FAILURE); 00080 EC_CLEANUP_END 00081 } 00082 00083 #endif /* WANT_TEST */ 00084 00085 /* 00086 00087 I_LIST 00088 Allows the user to list all the module names on the stream, up to and including the topmost driver name. If arg is NULL, the return value is the number of modules, including the driver, that are on the STREAM pointed to by fildes. This allows the user to allocate enough space for the module names. If arg is non-null, it should point to an str_list structure that has the following members: 00089 00090 int sl_nmods; 00091 struct str_mlist *sl_modlist; 00092 The str_mlist structure has the following member: 00093 00094 char l_name[FMNAMESZ+1]; 00095 The sl_nmods member indicates the number of entries the process has allocated in the array. Upon return, the sl_modlist member of the str_list structure contains the list of module names, and the number of entries that have been filled into the sl_modlist array is found in the sl_nmods member (the number includes the number of modules including the driver). The return value from ioctl() is 0. The entries are filled in starting at the top of the STREAM and continuing downstream until either the end of the STREAM is reached, or the number of requested modules (sl_nmods) is satisfied. On failure, errno may be set to one of the following values: 00096 00097 EINVAL 00098 The sl_nmods member is less than 1. 00099 00100 EAGAIN 00101 Unable to allocate buffers 00102 00103 */ 00104 00105 #else /* _XOPEN_STREAMS */ 00106 00107 #ifdef WANT_TEST 00108 00109 int main(void) 00110 { 00111 printf("STREAMS not supported.\n"); 00112 exit(EXIT_FAILURE); 00113 } 00114 00115 #endif /* WANT_TEST */ 00116 00117 #endif /* _XOPEN_STREAMS */