1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2022 Huawei Device Co., Ltd. 4 * 5 * Description: get af ninet. 6 * 7 * Author: Yang Yanjun <yangyanjun@huawei.com> 8 * 9 * Data: 2022-09-06 10 */ 11 #include <stdlib.h> 12 #include <string.h> 13 #include <stdio.h> 14 #include <unistd.h> 15 16 #define AF_NINET_PATH ("/sys/module/newip/parameters/af_ninet") 17 #define AF_NINET_LENTH (5) 18 19 int g_af_ninet; 20 _get_af_ninet(void)21 void _get_af_ninet(void) 22 { 23 char tmp[AF_NINET_LENTH]; 24 FILE *fn = fopen(AF_NINET_PATH, "r"); 25 26 if (!fn) { 27 printf("fail to open %s\n\n", AF_NINET_PATH); 28 return; 29 } 30 31 if (fgets(tmp, AF_NINET_LENTH, fn) == NULL) { 32 printf("fail to gets %s\n\n", AF_NINET_PATH); 33 fclose(fn); 34 return; 35 } 36 37 if (fclose(fn) == EOF) { 38 printf("fclose failed\n"); 39 return; 40 } 41 g_af_ninet = atoi(tmp); 42 } 43 get_af_ninet(void)44 int get_af_ninet(void) 45 { 46 if (g_af_ninet == 0) 47 _get_af_ninet(); 48 49 return g_af_ninet; 50 } 51 main(int argc,char ** argv)52 int main(int argc, char **argv) 53 { 54 printf("af_ninet=%d\n\n", get_af_ninet()); 55 return 0; 56 } 57 58