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.
 
 
 
 

271 lines
7.0 KiB

  1. /* Copyright (C) 2011-2014 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 "foncs.h"
  29. #include "histo.h"
  30. #include "stackN.h"
  31. #include "stackC.h"
  32. void IF_exit(void) { _MODIF_RUN_(0); }
  33. long long Nife_time(struct timeval tv)
  34. {
  35. long long v;
  36. /* il y a 10958 jours entre le 1/1/1970 et le 1/1/2000 */
  37. v=(long long)((long)tv.tv_usec) +
  38. ((long long)(tv.tv_sec - 946771200L)*(long long)1000000);
  39. return v;
  40. }
  41. void IF_time(void)
  42. {
  43. struct timeval tv;
  44. long long v;
  45. gettimeofday(&tv,NULL);
  46. v=Nife_time(tv);
  47. putLong(v);
  48. }
  49. void IF_sleep(void)
  50. {
  51. long t;
  52. if (getParLong(&t)) sleep((unsigned int)t);
  53. }
  54. void IF_sh (void)
  55. {
  56. int pid;
  57. char * Sh;
  58. _MODIF_WAITPID_(1);
  59. if ((pid = fork()) == -1) stopErr("IF_sh","fork");
  60. if (pid == 0) { /* fils */
  61. _MODIF_inSonProc_(1);
  62. termReset();
  63. dup2(1,2);
  64. if ((Sh=getenv("SHELL")) == NULL) execlp("sh", "nife-sh",NULL);
  65. else execlp(Sh,"nife-sh",NULL);
  66. perror("sh");
  67. exit(1);
  68. }
  69. waitpid(pid,NULL,0);
  70. _MODIF_WAITPID_(0);
  71. termInit();
  72. printf("Come back to nife !\n");
  73. }
  74. void runCommand (char * com)
  75. {
  76. int pid;
  77. char * Sh;
  78. _MODIF_WAITPID_(1);
  79. if ((pid = fork()) == -1) stopErr("runComm","fork");
  80. if (pid == 0) { /* fils */
  81. _MODIF_inSonProc_(1);
  82. termReset();
  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. termInit();
  91. printf("\n");
  92. }
  93. void IF_toCsv(void)
  94. {
  95. int i, lib=0;
  96. long t;
  97. char *f, *s, *e;
  98. void *M;
  99. FILE * fd;
  100. if (getParLong(&t)) {
  101. if (!isNTabSameDim((int)t)) {
  102. messErr(3);
  103. return;
  104. }
  105. if (!isNString(1)) {
  106. messErr(6);
  107. return;
  108. }
  109. f = getString();
  110. s = f;
  111. e = f + strlen(f);
  112. while (s < e) {
  113. if (*s == ';') { *s= '\0'; s++; break; }
  114. s++;
  115. }
  116. if (strlen(s) > 0) lib=1;
  117. if ((M = malloc(strlen(f)+5)) == NULL) stopErr("IF_toCsv","malloc");
  118. sprintf((char*)M,"%s.csv",f);
  119. fd = fopen((char*)M,"w+");
  120. free(M);
  121. for (i=0; i<(int)t; i++) {
  122. if (lib) {
  123. if (strlen(s) > 0) {
  124. f = s;
  125. while (s < e) {
  126. if (*s == ';') { *s= '\0'; s++; break; }
  127. s++;
  128. }
  129. fprintf(fd,"%s;",f);
  130. } else
  131. fprintf(fd,"lig%d;",i+1);
  132. }
  133. IF_inFile_1d(fd, ';', 1);
  134. }
  135. fclose(fd);
  136. free((void*)f);
  137. }
  138. }
  139. static void lanceXgraph(int mode, int tit)
  140. {
  141. FILE * fd;
  142. char nf[30];
  143. sprintf(nf,".nife/Nife_%d.gx",getpid());
  144. fd = fopen(nf,"w+");
  145. fprintf(fd,"Device: Postscript\nDisposition: To File\nTitleText: ");
  146. if (tit) fprintf(fd,"%s\n",getString());
  147. else fprintf(fd,"Data from Nife %s\n",VERSION);
  148. if (mode) IF_inFile_2(fd); else IF_inFile_1(fd);
  149. fclose(fd);
  150. execlp("xgraph","xgraph",nf,NULL);
  151. }
  152. static void gen_Xgraph (int m, int t)
  153. {
  154. int pid;
  155. if (m) {
  156. if (!isNTabSameDim(2)) {
  157. messErr(3);
  158. return;
  159. }
  160. } else {
  161. if (!is1Tab()) {
  162. messErr(12);
  163. return;
  164. }
  165. }
  166. if (t) {
  167. if (!isNString(1)) {
  168. messErr(6);
  169. return;
  170. }
  171. }
  172. if ((pid = fork()) == -1) stopErr("IF_yXgraph","fork");
  173. if (pid == 0) { /* fils */
  174. _MODIF_inSonProc_(1);
  175. setsid();
  176. lanceXgraph(m,t);
  177. perror("xgraph");
  178. exit(1);
  179. }
  180. IF_drop();
  181. if (m) IF_drop();
  182. if (t) IF_dropC();
  183. /* test if xgraph is executed */
  184. if (kill(pid,SIGWINCH) == -1)
  185. if (errno == ESRCH) messErr(10);
  186. }
  187. void IF_yXgraph (void)
  188. {
  189. gen_Xgraph(0,0);
  190. }
  191. void IF_ytXgraph (void)
  192. {
  193. gen_Xgraph(0,1);
  194. }
  195. void IF_xyXgraph (void)
  196. {
  197. gen_Xgraph(1,0);
  198. }
  199. void IF_xytXgraph (void)
  200. {
  201. gen_Xgraph(1,1);
  202. }
  203. static void printLimits(char * M,char *U, struct rlimit * L)
  204. {
  205. printf("Limites %-10s : ",M);
  206. if (L->rlim_cur == RLIM_INFINITY) printf("infini");
  207. else printf("%ld",L->rlim_cur);
  208. if (L->rlim_cur == RLIM_INFINITY) printf("/infini");
  209. else printf("/%ld",L->rlim_cur);
  210. printf(" %s\n",U);
  211. }
  212. void IF_resUsage(void)
  213. {
  214. struct rusage R;
  215. struct rlimit L;
  216. if (getrusage(RUSAGE_SELF,&R) == 0) {
  217. printf("Temps processus : %ld.%.6ld\n",R.ru_utime.tv_sec,R.ru_utime.tv_usec);
  218. printf("Temps système : %ld.%.6ld\n",R.ru_utime.tv_sec,R.ru_utime.tv_usec);
  219. /* non significatif **************
  220. printf("Taille résidente maximale : %ld\n",R.ru_maxrss);
  221. printf("Taille des données non partagées : %ld\n",R.ru_idrss);
  222. printf("Taille de Pile : %ld\n",R.ru_isrss);
  223. **********************************/
  224. printf("Demandes de pages : %ld\n",R.ru_minflt);
  225. printf("Nombre de fautes de pages : %ld\n",R.ru_majflt);
  226. printf("Changts de contexte volontaires : %ld\n",R.ru_nvcsw);
  227. printf("Changts de contexte involontaires: %ld\n",R.ru_nivcsw);
  228. } else perror("getrusage");
  229. /*
  230. if (getrlimit(RLIMIT_AS,&L) == 0) { printLimits("AS","octets",&L);
  231. } else perror("getrlimit AS");
  232. ***************/
  233. if (getrlimit(RLIMIT_CORE,&L) == 0) { printLimits("CORE","octets",&L);
  234. } else perror("getrlimit CORE");
  235. if (getrlimit(RLIMIT_CPU,&L) == 0) { printLimits("CPU","sec.",&L);
  236. } else perror("getrlimit CPU");
  237. /* not in Solaris ***************
  238. if (getrlimit(RLIMIT_RSS,&L) == 0) { printLimits("RSS","pages",&L);
  239. } else perror("getrlimit RSS"); */
  240. if (getrlimit(RLIMIT_DATA,&L) == 0) { printLimits("DATA","pages",&L);
  241. } else perror("getrlimit DATA");
  242. if (getrlimit(RLIMIT_STACK,&L) == 0) { printLimits("STACK","octets",&L);
  243. } else perror("getrlimit STACK");
  244. /* not in Solaris ***************
  245. if (getrlimit(RLIMIT_NPROC,&L) == 0) { printLimits("NPROC","processus",&L);
  246. } else perror("getrlimit NPROC"); */
  247. if (getrlimit(RLIMIT_NOFILE,&L) == 0) { printLimits("NOFILE","file desc.",&L);
  248. } else perror("getrlimit NOFILE");
  249. }