Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

275 lines
7.1 KiB

  1. /* Copyright (C) 2011-2022 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* foncs.c liste des fonctions systeme */
  14. #include "conf.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <signal.h>
  19. #include <unistd.h>
  20. #include <time.h>
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23. #include <sys/types.h>
  24. #include <sys/wait.h>
  25. #include "nife.h"
  26. #include "mth.h"
  27. #include "err.h"
  28. #include "debug.h"
  29. #include "foncs.h"
  30. #include "histo.h"
  31. #include "stackN.h"
  32. #include "stackC.h"
  33. void IF_exit(void) { _MODIF_RUN_(0); }
  34. long long Nife_time(struct timeval tv)
  35. {
  36. long long v;
  37. /* il y a 10958 jours entre le 1/1/1970 et le 1/1/2000 */
  38. v=(long long)((long)tv.tv_usec) +
  39. ((long long)(tv.tv_sec - 946771200L)*(long long)1000000);
  40. return v;
  41. }
  42. void IF_time(void)
  43. {
  44. struct timeval tv;
  45. long long v;
  46. gettimeofday(&tv,NULL);
  47. v=Nife_time(tv);
  48. putLong(v);
  49. }
  50. void IF_sleep(void)
  51. {
  52. long t;
  53. if (getParLong(&t)) sleep((unsigned int)t);
  54. }
  55. void IF_sh (void)
  56. {
  57. int pid;
  58. char * Sh;
  59. _MODIF_WAITPID_(1);
  60. if ((pid = fork()) == -1) stopErr("IF_sh","fork");
  61. if (pid == 0) { /* fils */
  62. _MODIF_inSonProc_(1);
  63. termReset();
  64. dup2(1,2);
  65. if ((Sh=getenv("SHELL")) == NULL) execlp("sh", "nife-sh",NULL);
  66. else execlp(Sh,"nife-sh",NULL);
  67. perror("sh");
  68. exit(1);
  69. }
  70. waitpid(pid,NULL,0);
  71. _MODIF_WAITPID_(0);
  72. termInit();
  73. printf("Come back to nife !\n");
  74. }
  75. void runCommand (char * com)
  76. {
  77. int pid;
  78. char * Sh;
  79. _MODIF_WAITPID_(1);
  80. if ((pid = fork()) == -1) stopErr("runComm","fork");
  81. if (pid == 0) { /* fils */
  82. _MODIF_inSonProc_(1);
  83. if ((Sh=getenv("SHELL")) == NULL) execlp("sh","sh","-c",com,NULL);
  84. else execlp(Sh,"sh","-c",com,NULL);
  85. perror("sh");
  86. exit(1);
  87. }
  88. waitpid(pid,NULL,0);
  89. _MODIF_WAITPID_(0);
  90. }
  91. void runCommandT (char * com)
  92. {
  93. D_Trace("$"); D_Tracenl(com);
  94. runCommand(com);
  95. }
  96. void IF_toCsv(void)
  97. {
  98. int i, lib=0;
  99. long t;
  100. char *f, *s, *e;
  101. void *M;
  102. FILE * fd;
  103. if (getParLong(&t)) {
  104. if (!isNTabSameDim((int)t)) {
  105. messErr(3);
  106. return;
  107. }
  108. if (!isNString(1)) {
  109. messErr(6);
  110. return;
  111. }
  112. f = getString();
  113. s = f;
  114. e = f + strlen(f);
  115. while (s < e) {
  116. if (*s == ';') { *s= '\0'; s++; break; }
  117. s++;
  118. }
  119. if (strlen(s) > 0) lib=1;
  120. if ((M = malloc(strlen(f)+5)) == NULL) stopErr("IF_toCsv","malloc");
  121. sprintf((char*)M,"%s.csv",f);
  122. fd = fopen((char*)M,"w+");
  123. free(M);
  124. for (i=0; i<(int)t; i++) {
  125. if (lib) {
  126. if (strlen(s) > 0) {
  127. f = s;
  128. while (s < e) {
  129. if (*s == ';') { *s= '\0'; s++; break; }
  130. s++;
  131. }
  132. fprintf(fd,"%s;",f);
  133. } else
  134. fprintf(fd,"lig%d;",i+1);
  135. }
  136. IF_inFile_1d(fd, ';', 1);
  137. }
  138. fclose(fd);
  139. free((void*)f);
  140. }
  141. }
  142. static void lanceXgraph(int mode, int tit)
  143. {
  144. FILE * fd;
  145. char nf[30];
  146. sprintf(nf,".nife/Nife_%d.gx",getpid());
  147. fd = fopen(nf,"w+");
  148. fprintf(fd,"Device: Postscript\nDisposition: To File\nTitleText: ");
  149. if (tit) fprintf(fd,"%s\n",getString());
  150. else fprintf(fd,"Data from Nife %s\n",VERSION);
  151. if (mode) IF_inFile_2(fd); else IF_inFile_1(fd);
  152. fclose(fd);
  153. execlp("xgraph","xgraph",nf,NULL);
  154. }
  155. static void gen_Xgraph (int m, int t)
  156. {
  157. int pid;
  158. if (m) {
  159. if (!isNTabSameDim(2)) {
  160. messErr(3);
  161. return;
  162. }
  163. } else {
  164. if (!is1Tab()) {
  165. messErr(12);
  166. return;
  167. }
  168. }
  169. if (t) {
  170. if (!isNString(1)) {
  171. messErr(6);
  172. return;
  173. }
  174. }
  175. if ((pid = fork()) == -1) stopErr("IF_yXgraph","fork");
  176. if (pid == 0) { /* fils */
  177. _MODIF_inSonProc_(1);
  178. setsid();
  179. lanceXgraph(m,t);
  180. perror("xgraph");
  181. exit(1);
  182. }
  183. IF_drop();
  184. if (m) IF_drop();
  185. if (t) IF_dropC();
  186. /* test if xgraph is executed */
  187. if (kill(pid,SIGWINCH) == -1)
  188. if (errno == ESRCH) messErr(10);
  189. }
  190. void IF_yXgraph (void)
  191. {
  192. gen_Xgraph(0,0);
  193. }
  194. void IF_ytXgraph (void)
  195. {
  196. gen_Xgraph(0,1);
  197. }
  198. void IF_xyXgraph (void)
  199. {
  200. gen_Xgraph(1,0);
  201. }
  202. void IF_xytXgraph (void)
  203. {
  204. gen_Xgraph(1,1);
  205. }
  206. static void printLimits(char * M,char *U, struct rlimit * L)
  207. {
  208. printf("Limites %-10s : ",M);
  209. if (L->rlim_cur == RLIM_INFINITY) printf("infini");
  210. else printf("%ld",L->rlim_cur);
  211. if (L->rlim_cur == RLIM_INFINITY) printf("/infini");
  212. else printf("/%ld",L->rlim_cur);
  213. printf(" %s\n",U);
  214. }
  215. void IF_resUsage(void)
  216. {
  217. struct rusage R;
  218. struct rlimit L;
  219. if (getrusage(RUSAGE_SELF,&R) == 0) {
  220. printf("Temps processus : %ld.%.6ld\n",R.ru_utime.tv_sec,R.ru_utime.tv_usec);
  221. printf("Temps système : %ld.%.6ld\n",R.ru_utime.tv_sec,R.ru_utime.tv_usec);
  222. /* non significatif **************
  223. printf("Taille résidente maximale : %ld\n",R.ru_maxrss);
  224. printf("Taille des données non partagées : %ld\n",R.ru_idrss);
  225. printf("Taille de Pile : %ld\n",R.ru_isrss);
  226. **********************************/
  227. printf("Demandes de pages : %ld\n",R.ru_minflt);
  228. printf("Nombre de fautes de pages : %ld\n",R.ru_majflt);
  229. printf("Changts de contexte volontaires : %ld\n",R.ru_nvcsw);
  230. printf("Changts de contexte involontaires: %ld\n",R.ru_nivcsw);
  231. } else perror("getrusage");
  232. /*
  233. if (getrlimit(RLIMIT_AS,&L) == 0) { printLimits("AS","octets",&L);
  234. } else perror("getrlimit AS");
  235. ***************/
  236. if (getrlimit(RLIMIT_CORE,&L) == 0) { printLimits("CORE","octets",&L);
  237. } else perror("getrlimit CORE");
  238. if (getrlimit(RLIMIT_CPU,&L) == 0) { printLimits("CPU","sec.",&L);
  239. } else perror("getrlimit CPU");
  240. /* not in Solaris ***************
  241. if (getrlimit(RLIMIT_RSS,&L) == 0) { printLimits("RSS","pages",&L);
  242. } else perror("getrlimit RSS"); */
  243. if (getrlimit(RLIMIT_DATA,&L) == 0) { printLimits("DATA","pages",&L);
  244. } else perror("getrlimit DATA");
  245. if (getrlimit(RLIMIT_STACK,&L) == 0) { printLimits("STACK","octets",&L);
  246. } else perror("getrlimit STACK");
  247. /* not in Solaris ***************
  248. if (getrlimit(RLIMIT_NPROC,&L) == 0) { printLimits("NPROC","processus",&L);
  249. } else perror("getrlimit NPROC"); */
  250. if (getrlimit(RLIMIT_NOFILE,&L) == 0) { printLimits("NOFILE","file desc.",&L);
  251. } else perror("getrlimit NOFILE");
  252. }