Home | History | Annotate | Line # | Download | only in c
readlink2.c revision 1.1
      1 /*
      2 #progos: linux
      3 */
      4 
      5 #include <unistd.h>
      6 #include <errno.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 
     11 int main (int argc, char *argv[])
     12 {
     13   char buf[1024];
     14   char buf2[1024];
     15   int err;
     16 
     17   /* This is a special feature handled in the simulator.  The "42"
     18      should be formed from getpid () if this was a real program.  */
     19   err = readlink ("/proc/42/exe", buf, sizeof (buf));
     20   if (err < 0)
     21     {
     22       if (err == -1 && errno == ENOSYS)
     23 	printf ("ENOSYS\n");
     24       printf ("xyzzy\n");
     25       exit (0);
     26     }
     27 
     28   /* Don't use an abort in the following; it might cause the printf to
     29      not make it all the way to output and make debugging more
     30      difficult.  */
     31 
     32   /* We assume the program is called with no path, so we might need to
     33      prepend it.  */
     34   if (getcwd (buf2, sizeof (buf2)) != buf2)
     35     {
     36       perror ("getcwd");
     37       exit (1);
     38     }
     39 
     40   if (argv[0][0] == '/')
     41     {
     42 #ifdef SYSROOTED
     43       if (strchr (argv[0] + 1, '/') != NULL)
     44 	{
     45 	  printf ("%s != %s\n", argv[0], strrchr (argv[0] + 1, '/'));
     46 	  exit (1);
     47 	}
     48 #endif
     49       if (strcmp (argv[0], buf) != 0)
     50 	{
     51 	  printf ("%s != %s\n", buf, argv[0]);
     52 	  exit (1);
     53 	}
     54     }
     55   else if (argv[0][0] != '.')
     56     {
     57       if (buf2[strlen (buf2) - 1] != '/')
     58 	strcat (buf2, "/");
     59       strcat (buf2, argv[0]);
     60       if (strcmp (buf2, buf) != 0)
     61 	{
     62 	  printf ("%s != %s\n", buf, buf2);
     63 	  exit (1);
     64 	}
     65     }
     66   else
     67     {
     68       strcat (buf2, argv[0] + 1);
     69       if (strcmp (buf, buf2) != 0)
     70 	{
     71 	  printf ("%s != %s\n", buf, buf2);
     72 	  exit (1);
     73 	}
     74     }
     75 
     76   printf ("pass\n");
     77   exit (0);
     78 }
     79 
     80 
     81